Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jul 6th, 2005, 1:31 PM   #1
clearbit
Newbie
 
clearbit's Avatar
 
Join Date: Jun 2005
Location: far..far..away
Posts: 25
Rep Power: 0 clearbit is on a distinguished road
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.
clearbit is offline   Reply With Quote
Old Jul 6th, 2005, 2:28 PM   #2
Ancient Dragon
PFO God In Training
 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 598
Rep Power: 4 Ancient Dragon is on a distinguished road
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;
}
Ancient Dragon is offline   Reply With Quote
Old Jul 6th, 2005, 2:41 PM   #3
clearbit
Newbie
 
clearbit's Avatar
 
Join Date: Jun 2005
Location: far..far..away
Posts: 25
Rep Power: 0 clearbit is on a distinguished road
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;
}
clearbit is offline   Reply With Quote
Old Jul 6th, 2005, 2:42 PM   #4
clearbit
Newbie
 
clearbit's Avatar
 
Join Date: Jun 2005
Location: far..far..away
Posts: 25
Rep Power: 0 clearbit is on a distinguished road
Whats wrong with initializing it like this?? Security flaw?? what?
clearbit is offline   Reply With Quote
Old Jul 6th, 2005, 2:51 PM   #5
Animatronic
Programmer
 
Join Date: Jun 2005
Posts: 99
Rep Power: 4 Animatronic is on a distinguished road
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).
Animatronic is offline   Reply With Quote
Old Jul 6th, 2005, 2:53 PM   #6
Ancient Dragon
PFO God In Training
 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 598
Rep Power: 4 Ancient Dragon is on a distinguished road
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.
Ancient Dragon is offline   Reply With Quote
Old Jul 6th, 2005, 2:55 PM   #7
Ancient Dragon
PFO God In Training
 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 598
Rep Power: 4 Ancient Dragon is on a distinguished road
Quote:
Originally Posted by Animatronic
Using an initialiser list is usually the preferred way
depends on who's doing the preferring I have a class that has over 40 member data objects. I'm certainly not going to use an initializer list! :eek:
Ancient Dragon is offline   Reply With Quote
Old Jul 6th, 2005, 6:22 PM   #8
clearbit
Newbie
 
clearbit's Avatar
 
Join Date: Jun 2005
Location: far..far..away
Posts: 25
Rep Power: 0 clearbit is on a distinguished road
Quote:
Originally Posted by Ancient Dragon
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.
By initializers do you mean the preamble? and sorry, my book just said thats one way to do it, my bad for not specifying
clearbit is offline   Reply With Quote
Old Jul 7th, 2005, 5:23 AM   #9
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,254
Rep Power: 5 grumpy will become famous soon enough
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.
grumpy is offline   Reply With Quote
Old Jul 8th, 2005, 10:30 PM   #10
clearbit
Newbie
 
clearbit's Avatar
 
Join Date: Jun 2005
Location: far..far..away
Posts: 25
Rep Power: 0 clearbit is on a distinguished road
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~
clearbit is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:48 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC