![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Jun 2005
Location: far..far..away
Posts: 25
Rep Power: 0
![]() |
preamble to constructor
ok its been a while since ive coded in C++ but im trying to learn about classes agian, and ive been reading a book ive had, just never had the time to read.(well till now). It talks about to initialize a data member(private) you have to make it the preamble to the constructor of the class..
For example class Whatever
{
: _xin (x) //HERES WHERE MY QUESTIONS AT.
public:
Construct(int xin) {std::cout<<xin;}
...
private:
const int _xin;
}The question is can I just assign an integer to the _xin like this : _xin = 1; lame question i know but i dont have a compiler to test it right now..thnx. |
|
|
|
|
|
#2 |
|
PFO God In Training
![]() Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 600
Rep Power: 4
![]() |
Throw that book away immediately! :eek: just because someting is private does not mean it has to be initialized like that.
class MyClass
{
public:
MyClass() {m_x = 0;}
private:
int m_x;
};The only time I initialize class variables is when it is a reference to something. class MyClass
{
public:
MyClass(int n) : m_r(n)
{
m_x = 0;
}
private:
int m_x;
int& m_r;
};
int main(int argc, char* argv[])
{
int x = 0;
MyClass m(x);
return 0;
} |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Jun 2005
Location: far..far..away
Posts: 25
Rep Power: 0
![]() |
opps sorry, this is what it actualy looks like in the book
class Whatever
{
public:
Construct(int xin)
: _xin (x) //HERES WHERE MY QUESTIONS AT.
{
std::cout<<xin;
}
...
private:
const int _xin;
} |
|
|
|
|
|
#4 |
|
Newbie
Join Date: Jun 2005
Location: far..far..away
Posts: 25
Rep Power: 0
![]() |
Whats wrong with initializing it like this?? Security flaw?? what?
|
|
|
|
|
|
#5 |
|
Programmer
Join Date: Jun 2005
Posts: 99
Rep Power: 4
![]() |
Nothing it wrong with it. Using an initialiser list is usually the preferred way to intialise members inside the class as it gives the greatest posible performance (members only get constructed once instead of a posible two times).
|
|
|
|
|
|
#6 |
|
PFO God In Training
![]() Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 600
Rep Power: 4
![]() |
nothing "wrong" with it -- you said your book told you have to do it that way. That's not true. If you have a bunch of class members it would be too diffucult to read if you initialized them like that. Put the initializers inside the constructor's function is cleaner and easier to read.
|
|
|
|
|
|
#7 | |
|
PFO God In Training
![]() Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 600
Rep Power: 4
![]() |
Quote:
I have a class that has over 40 member data objects. I'm certainly not going to use an initializer list! :eek: |
|
|
|
|
|
|
#8 | |
|
Newbie
Join Date: Jun 2005
Location: far..far..away
Posts: 25
Rep Power: 0
![]() |
Quote:
|
|
|
|
|
|
|
#9 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,261
Rep Power: 5
![]() |
What you (and your book) are describing as the "preamble" is more usually described in C++ as the initialiser (or initializer) list. The word "preamble" literally means "preliminary material".
As others have said, there is nothing wrong with initialising class members using the initialiser list, but there are alternatives should you wish to use them. |
|
|
|
|
|
#10 |
|
Newbie
Join Date: Jun 2005
Location: far..far..away
Posts: 25
Rep Power: 0
![]() |
problem Solved
oh ok..thank you. I understand, I'll make a note in my book
Last edited by clearbit; Jul 8th, 2005 at 10:32 PM. Reason: ~SOLVED~ |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|