Connecting Tech Pros Worldwide Help | Site Map

Constant member of a class

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 4th, 2008, 09:15 AM
Anarki
Guest
 
Posts: n/a
Default Constant member of a class

#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?

  #2  
Old July 4th, 2008, 09:35 AM
utab
Guest
 
Posts: n/a
Default Re: Constant member of a class

On Fri, 04 Jul 2008 02:08:13 -0700, Anarki wrote:
Quote:
#include <iostream>
using namespace std;
>
class ConstantMember
{
const int m_const;
public:
ConstantMember(int cons = 10):m_const(cons){} int getConst() const
{
Quote:
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
  #3  
Old July 4th, 2008, 09:35 AM
utab
Guest
 
Posts: n/a
Default Re: Constant member of a class

Quote:
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
  #4  
Old July 4th, 2008, 09:35 PM
Salt_Peter
Guest
 
Posts: n/a
Default Re: Constant member of a class

On Jul 4, 5:08 am, Anarki <Deepchan...@gmail.comwrote:
Quote:
#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
*/
  #5  
Old July 14th, 2008, 10:15 AM
Anarki
Guest
 
Posts: n/a
Default 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() )
  #6  
Old July 14th, 2008, 10:25 AM
Anarki
Guest
 
Posts: n/a
Default Re: Constant member of a class

On Jul 14, 3:07*pm, Anarki <Deepchan...@gmail.comwrote:
Quote:
#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?
  #7  
Old July 14th, 2008, 10:25 AM
Ian Collins
Guest
 
Posts: n/a
Default Re: Constant member of a class

Anarki wrote:
Quote:
#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:
What is the difference between the
constructor i gave in first post and this ( ConstantMember() and
ConstantMember1() )
You used an initialiser list.

--
Ian Collins.
  #8  
Old July 14th, 2008, 10:25 AM
Ian Collins
Guest
 
Posts: n/a
Default Re: Constant member of a class

Anarki wrote:
Quote:
>
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.
  #9  
Old July 14th, 2008, 02:15 PM
Anarki
Guest
 
Posts: n/a
Default Re: Constant member of a class

On Jul 14, 3:16*pm, Ian Collins <ian-n...@hotmail.comwrote:
Quote:
Anarki wrote:
Quote:
#include <iostream>
using namespace std;
>
Quote:
class ConstantMember1
{
* * * * const int m_const;
* * * * public:
* * * * ConstantMember1(int cons = 20){m_const = cons;}
* * * * int getConst() const{ return m_const;}
>
Quote:
};
>
Quote:
int main()
{
* * * * ConstantMember1 Y;
* * * * cout << X.getConst();
* * * * return 0;
>
Quote:
}
>
Quote:
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:
>
The only way to initialise a const member is with an initialiser list.
>
Quote:
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:
>
--
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......
  #10  
Old July 14th, 2008, 02:35 PM
Puppet_Sock
Guest
 
Posts: n/a
Default Re: Constant member of a class

On Jul 14, 10:07*am, Anarki <Deepchan...@gmail.comwrote:
[snip]
Quote:
Quote:
Quote:
Why doesn't this program compile?
>
Quote:
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
  #11  
Old July 14th, 2008, 08:15 PM
Ian Collins
Guest
 
Posts: n/a
Default Re: Constant member of a class

Anarki wrote:
Quote:
On Jul 14, 3:16 pm, Ian Collins <ian-n...@hotmail.comwrote:
Quote:
Quote:
>You used an initialiser list.
Quote:
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.
  #12  
Old July 15th, 2008, 06:45 AM
Anarki
Guest
 
Posts: n/a
Default Re: Constant member of a class

On Jul 15, 1:08*am, Ian Collins <ian-n...@hotmail.comwrote:
Quote:
Anarki wrote:
Quote:
On Jul 14, 3:16 pm, Ian Collins <ian-n...@hotmail.comwrote:
Quote:
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.
  #13  
Old July 15th, 2008, 07:05 AM
Anarki
Guest
 
Posts: n/a
Default 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) :)
  #14  
Old July 15th, 2008, 08:35 AM
Ian Collins
Guest
 
Posts: n/a
Default Re: Constant member of a class

Anarki wrote:
Quote:
Did u mean his post in this thread? if so
>
Please don't used txt speak on Usenet.
Quote:
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:
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.
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.