easy question about constructors......

Started by
10 comments, last by graveyard filla 20 years, 2 months ago
quote:Original post by Agony
Yes, a prototype is needed in the header so that other files can use your class. [Edit - Unless everything is in the header, and thus most likely inlined. Either way, other files need to know the form of your class, and its member functions, including the constructor, and they are told what its form is by #include-ing the header file, which should contain all relavent information.]

As for calling a function with default values, you can specify none of them, all of them, or even just the first n parameters. For example, all of these should be fine:

Player playerone();Player playerone(20);Player playerone(20, 30);Player playerone(20, 30, 100);

If a parameter isn't specified, it's default value is taken. But if I remember right, you only specify the default value in the prototype, if you have both a prototype and a declaration.

And as for using the initializer list over just putting the initializations into the body of the constructor, I have learned to do it the correct way (thanks to GameDev, actually), but it's not a habit yet. I apologize for suggesting the obviously less acceptable form.

[edited by - Agony on February 20, 2004 2:41:17 PM]



thanks for the response, just one last question :

Player playerone(); //will init all default valuesPlayer playerone(20); //will init first value to 20 and the other 2 will be default???Player playerone(20, 30); //ill init first 2 and the other will be default???Player playerone(20, 30, 100); //init all to these 3 values, dont use any default



so am i right? and what if i want to init the last 2 or 3 but want the first one by default. not possible? thanks for your help!!!






[edited by - graveyard filla on February 20, 2004 4:08:55 PM]

[edited by - graveyard filla on February 20, 2004 4:09:14 PM]

[edited by - graveyard filla on February 20, 2004 4:09:29 PM]

[edited by - graveyard filla on February 20, 2004 4:09:55 PM]
FTA, my 2D futuristic action MMORPG
Advertisement
quote:Original post by graveyard filla
Player playerone(); //will init all default valuesPlayer playerone(20); //will init first value to 20 and the other 2 will be default???Player playerone(20, 30); //ill init first 2 and the other will be default???Player playerone(20, 30, 100); //init all to these 3 values, dont use any default 
so am i right?

Correct.
quote:and what if i want to init the last 2 or 3 but want the first one by default. not possible?

Correct, it isn''t possible, unfortunately. Other languages can do this, but depending on how it''s implemented, it can cause function calls to be less efficient, and C++ strives for efficiency. Although I do remember in VB you can say Method(, 5, 3, , 4) and those empy parameters, indicated by the blank commas, will be filled in with default values, and it seems that a compiler could make that just as efficient as what C++ does. But C++ won''t do this.
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke

This topic is closed to new replies.

Advertisement