Click to See Complete Forum and Search --> : initialization lists
wassick
Mar 22nd, 2001, 11:07 AM
I was wondering if someone could explain initialization lists for structs and class member functions. I don't see why you would initialization in the initializer list instead of in the body of the program. For example:
StudentType::StudentType()
: Grades(10, -1), Gender(female), Class(0)
{
}
I am guessing there's a good reason but I really don't understand the point.
Thank you so much for any help :)
Technocrat
Mar 22nd, 2001, 01:12 PM
Well I know why you would use this in a class. In your example if you had the init list under the class constructor your functions in that list would run when the class is inited. You would do this if those functions set values needed in other functions in your class. This way you would not have to call these functions individualy, or inside each other function. So if it was done this way:
StudentType Students; //When this this called it would call those functions in the init list
But I cant think of a real good reason to do it as init list for a single function in a class. It might just be another way to call the functions
wassick
Mar 22nd, 2001, 04:54 PM
Sorry, that wasn't really what I meant to ask but thanks for your answer :)
Why don't you put the initialization statement in the body of the initializer? Why wouldn't you:
StudentType::StudentType()
: Grades(10, -1)
{
Gender = female;
Class = 0;
}
Hope someone can give me more help.
Technocrat
Mar 22nd, 2001, 04:59 PM
I think this might be one of those things where you could do it this way, or that way. It just depeneds on how you program. I honestly dont see a reason it would make a difference.
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.