Login or Sign up Help | Site Map
Connecting Tech Pros Worldwide

Constant member of a class

Question posted by: Anarki (Guest) on July 4th, 2008 09:15 AM
#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?
Would you like to answer this question?
Sign up for a free account, or Login (if you're already a member).
utab's Avatar
utab
Guest
n/a Posts
July 4th, 2008
09:35 AM
#2

Re: Constant member of a class
On Fri, 04 Jul 2008 02:08:13 -0700, Anarki wrote:
Quote:
Originally Posted by
#include <iostream>
using namespace std;
>
class ConstantMember
{
const int m_const;
public:
ConstantMember(int cons = 10):m_const(cons){} int getConst() const

{
Quote:
Originally Posted by
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

utab's Avatar
utab
Guest
n/a Posts
July 4th, 2008
09:35 AM
#3

Re: Constant member of a class
Quote:
Originally Posted by
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

Salt_Peter's Avatar
Salt_Peter
Guest
n/a Posts
July 4th, 2008
09:35 PM
#4

Re: Constant member of a class
On Jul 4, 5:08 am, Anarki <Deepchan...@gmail.comwrote:
Quote:
Originally Posted by
#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
*/

Anarki's Avatar
Anarki
Guest
n/a Posts
July 14th, 2008
10:15 AM
#5

Re: Constant member of a class
#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() )

Anarki's Avatar
Anarki
Guest
n/a Posts
July 14th, 2008
10:25 AM
#6

Re: Constant member of a class
On Jul 14, 3:07*pm, Anarki <Deepchan...@gmail.comwrote:
Quote:
Originally Posted by
#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?

Ian Collins's Avatar
Ian Collins
Guest
n/a Posts
July 14th, 2008
10:25 AM
#7

Re: Constant member of a class
Anarki wrote:
Quote:
Originally Posted by
#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.
Quote:
Originally Posted by
What is the difference between the
constructor i gave in first post and this ( ConstantMember() and
ConstantMember1() )


You used an initialiser list.

--
Ian Collins.

Ian Collins's Avatar
Ian Collins
Guest
n/a Posts
July 14th, 2008
10:25 AM
#8

Re: Constant member of a class
Anarki wrote:
Quote:
Originally Posted by
>
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.

--
Ian Collins.

Anarki's Avatar
Anarki
Guest
n/a Posts
July 14th, 2008
02:15 PM
#9

Re: Constant member of a class
On Jul 14, 3:16*pm, Ian Collins <ian-n...@hotmail.comwrote:
Quote:
Originally Posted by
Anarki wrote:
Quote:
Originally Posted by
#include <iostream>
using namespace std;

>
Quote:
Originally Posted by
class ConstantMember1
{
* * * * const int m_const;
* * * * public:
* * * * ConstantMember1(int cons = 20){m_const = cons;}
* * * * int getConst() const{ return m_const;}

>
Quote:
Originally Posted by
};

>
Quote:
Originally Posted by
int main()
{
* * * * ConstantMember1 Y;
* * * * cout << X.getConst();
* * * * return 0;

>
Quote:
Originally Posted by
}

>
Quote:
Originally Posted by
Why doesn't this program compile?

>
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.
Quote:
Originally Posted by
>
The only way to initialise a const member is with an initialiser list.
>
Quote:
Originally Posted by
What is the difference between the
constructor i gave in first post and this ( ConstantMember() and
ConstantMember1() )

>
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.
Quote:
Originally Posted by
>
--
Ian Collins.


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......

Puppet_Sock's Avatar
Puppet_Sock
Guest
n/a Posts
July 14th, 2008
02:35 PM
#10

Re: Constant member of a class
On Jul 14, 10:07*am, Anarki <Deepchan...@gmail.comwrote:
[snip]
Quote:
Originally Posted by
Quote:
Originally Posted by
Quote:
Originally Posted by
Why doesn't this program compile?

>
Quote:
Originally Posted by
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.


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

Ian Collins's Avatar
Ian Collins
Guest
n/a Posts
July 14th, 2008
08:15 PM
#11

Re: Constant member of a class
Anarki wrote:
Quote:
Originally Posted by
On Jul 14, 3:16 pm, Ian Collins <ian-n...@hotmail.comwrote:

Quote:
Originally Posted by
Quote:
Originally Posted by
>You used an initialiser list.

Quote:
Originally Posted by
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.

--
Ian Collins.

Anarki's Avatar
Anarki
Guest
n/a Posts
July 15th, 2008
06:45 AM
#12

Re: Constant member of a class
On Jul 15, 1:08*am, Ian Collins <ian-n...@hotmail.comwrote:
Quote:
Originally Posted by
Anarki wrote:
Quote:
Originally Posted by
On Jul 14, 3:16 pm, Ian Collins <ian-n...@hotmail.comwrote:
Quote:
Originally Posted by
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.

>
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.
>
--
Ian Collins.


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.

Anarki's Avatar
Anarki
Guest
n/a Posts
July 15th, 2008
07:05 AM
#13

Re: Constant member of a class
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) :)

Ian Collins's Avatar
Ian Collins
Guest
n/a Posts
July 15th, 2008
08:35 AM
#14

Re: Constant member of a class
Anarki wrote:
Quote:
Originally Posted by
Did u mean his post in this thread? if so
>

Please don't used txt speak on Usenet.
Quote:
Originally Posted by
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.
Quote:
Originally Posted by
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.

--
Ian Collins.

 
Not the answer you were looking for? Post your question . . .
183,969 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Latest Articles: Read & Comment
  • Didn't find the answer you were looking for?
    Post Your Question
  • Top Community Contributors