Results 1 to 4 of 4

Thread: initialization lists

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2001
    Posts
    3
    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:

    Code:
    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

  2. #2
    Frenzied Member Technocrat's Avatar
    Join Date
    Jan 2000
    Location
    I live in the 1s and 0s of everyones data streams
    Posts
    1,024
    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:

    PHP Code:
    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
    MSVS 6, .NET & .NET 2003 Pro
    I HATE MSDN with .NET & .NET 2003!!!

    Check out my sites:
    http://www.filthyhands.com
    http://www.techno-coding.com


  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2001
    Posts
    3

    Smile

    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:

    Code:
    StudentType::StudentType()
      : Grades(10, -1)
    {
       Gender = female;
       Class = 0;
    }
    Hope someone can give me more help.

  4. #4
    Frenzied Member Technocrat's Avatar
    Join Date
    Jan 2000
    Location
    I live in the 1s and 0s of everyones data streams
    Posts
    1,024
    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.
    MSVS 6, .NET & .NET 2003 Pro
    I HATE MSDN with .NET & .NET 2003!!!

    Check out my sites:
    http://www.filthyhands.com
    http://www.techno-coding.com


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width