Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to initialize multiple constants

Status
Not open for further replies.

titanandrews

Programmer
Feb 27, 2003
130
US
In "Practical C++ Programming" they show you how to initialize a constant in the constructor. What they don't say is how to initialize more than one constant. Can someone please provide an example of how this is done given the following code?

Code:
class MyClass
{
private:
   const int anInt;
   const int anInt2;

public:
   MyClass::MyClass();
};

MyClass::MyClass() : anInt(1)
{

}

Compiler complains that anInt2 is not initialized.


Thanks for your help!


Barry
 
After the colon, you can put a comma-separated list of initializers.

That's the type of question that you could probably answer a lot more quickly yourself by just playing around with the code and trying things that seem logical.
 
Ah yes! I did play around a bit... I thought that I had tried using comma to separate, but apparently not. Thanks!



cheers,

Barry
 
No problem.

BTW, I didn't mean to sound harsh about figuring it out yourself; I was just trying to encourage exploration before asking for help on stuff like that.

Too often I get people calling me over to their computer, then asking me things like "Will this code compile?" when they could have tried to compile it themselves three times while I was walking accross the room.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top