Constant member of a class

A

Anarki

#include <iostream>
using namespace std;

class ConstantMember
{
const int m_const;
public:
ConstantMember(int cons = 10):m_const(cons){}
int getConst() const{ return m_const;}
};

int main()
{
ConstantMember X;//(20);
cout << X.getConst();
return 0;
}

the above is a program that was compiled & linked using g++. Is there
any other way to initialise a constant member variable using
constructor while creating objects?
 
U

utab

#include <iostream>
using namespace std;

class ConstantMember
{
const int m_const;
public:
ConstantMember(int cons = 10):m_const(cons){} int getConst() const {
return m_const;}
};

int main()
{
ConstantMember X;//(20);
cout << X.getConst();
return 0;
}

the above is a program that was compiled & linked using g++. Is there
any other way to initialise a constant member variable using constructor
while creating objects?

There are two points: initialization and constructor body execution, the
reason for const member functions to be initialized in the initializer is
that by the time the constructor body is executed, the initialization is
complete, and for const members, this is not possible because once you
assign the value you can not change it afterwards.

HTH
 
U

utab

There are two points: initialization and constructor body execution, the
reason for const member functions to be initialized in the initializer

Of course "const members" not const member functions
 
S

Salt_Peter

#include <iostream>
using namespace std;

class ConstantMember
{
const int m_const;
public:
ConstantMember(int cons = 10):m_const(cons){}
int getConst() const{ return m_const;}

};

int main()
{
ConstantMember X;//(20);
cout << X.getConst();
return 0;

}

the above is a program that was compiled & linked using g++. Is there
any other way to initialise a constant member variable using
constructor while creating objects?


well yes, copy ctor which is generated for you in the code above and
should look something like ...

class C
{
const int m;
public:
C(int n = 10) : m(n) { }
C(const C& copy) : m(copy.m) // ... this
{
std::cout << "copy\n";
}
int get() const { return m; }
};

int main()
{
C c;
std::cout << c.get() << std::endl;
C cop(c);
std::cout << cop.get() << std::endl;
}

/*
10
copy
10
*/
 
A

Anarki

#include <iostream>
using namespace std;

class ConstantMember1
{
const int m_const;
public:
ConstantMember1(int cons = 20){m_const = cons;}
int getConst() const{ return m_const;}

};

int main()
{
ConstantMember1 Y;
cout << X.getConst();
return 0;

}


Why doesn't this program compile? What is the difference between the
constructor i gave in first post and this ( ConstantMember() and
ConstantMember1() )
 
A

Anarki

#include <iostream>
using namespace std;

class ConstantMember1
{
        const int m_const;
        public:
        ConstantMember1(int cons = 20){m_const = cons;}
        int getConst() const{ return m_const;}

};

int main()
{
        ConstantMember1 Y;
        cout << X.getConst();
        return 0;

}

Why doesn't this program compile? What is the difference between the
constructor i gave in first post and this ( ConstantMember() and
ConstantMember1() )

sorry there is a typing mistake in the above code its Y.getConst();
but i have typed X.getConst();

Guys is there way to edit my post where i have made mistakes like the
above?
 
I

Ian Collins

Anarki said:
#include <iostream>
using namespace std;

class ConstantMember1
{
const int m_const;
public:
ConstantMember1(int cons = 20){m_const = cons;}
int getConst() const{ return m_const;}

};

int main()
{
ConstantMember1 Y;
cout << X.getConst();
return 0;

}


Why doesn't this program compile?

Too many bugs? Your compiler errors should have been clear enough.

The only way to initialise a const member is with an initialiser list.
What is the difference between the
constructor i gave in first post and this ( ConstantMember() and
ConstantMember1() )

You used an initialiser list.
 
I

Ian Collins

Anarki said:
Guys is there way to edit my post where i have made mistakes like the
above?

No. Cut and paste code you have tried rather than typing it into the
message.
 
A

Anarki

Too many bugs?  Your compiler errors should have been clear enough.
look friend i know the error is due to the constant variable in class.
But why should the compiler raise error? Am looking for more detailed
explanation.
The only way to initialise a const member is with an initialiser list.


You used an initialiser list.
i understand i used initialiser list but whats happening inside the
initialiser list? What does the compiler do when it see the
initialiser list. I think u didn't get what i meant. i explain a bit
more what's the difference between a normal constructor and
constructor that uses initialiser list.
The answer you gave me was just the technical term/terminology of the
example i posted. But what exactly is happening inside this
initialiser list?? Some one please help......
 
P

Puppet_Sock

look friend i know the error is due to the constant variable in class.
But why should the compiler raise error? Am looking for more detailed
explanation.

Did you read the post by utab that appeared here 10 days ago?
It's still on Google, so you could find it if you looked.
Socks
 
I

Ian Collins

i understand i used initialiser list but whats happening inside the
initialiser list? What does the compiler do when it see the
initialiser list. I think u didn't get what i meant. i explain a bit
more what's the difference between a normal constructor and
constructor that uses initialiser list.

What does you book tell you? This is pretty basic stuff. If you want
to see exactly what your compiler does, look at the generated assembler.
 
A

Anarki

What does you book tell you?  This is pretty basic stuff.  If you want
to see exactly what your compiler does, look at the generated assembler.

Sorry Ian am not a good programmer. I dunno how to get my hands on the
generated assembler and if at all i get my hands on it i dont think i
can understand it. Anyways i am gonna search for the stuff Puppet_sock
said about utab.
 
A

Anarki

Did u mean his post in this thread? if so

Does it really mean that initialization of constant member variable
occurs before the execution of constructor(using initialiser list).

Again, does that mean a member variable of a object get initialized
even before the object is constructed?

Clearing basics of C++ looks real chaos(for me) :)
 
I

Ian Collins

Anarki said:
Did u mean his post in this thread? if so
Please don't used txt speak on Usenet.
Does it really mean that initialization of constant member variable
occurs before the execution of constructor(using initialiser list).
Yes, members in the initialisation list are initialised before the body
of the constructor is run.
Again, does that mean a member variable of a object get initialized
even before the object is constructed?
No, the initialisation is one phase of the object's construction.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,160
Latest member
CollinStri
Top