Thread: Why can a class not do this?

  1. #1
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688

    Why can a class not do this?

    Hi this isn't really a problem as there are ways around this.

    I was wondering why this:

    Code:
    private:
       std::string m_Name;
       std::vector<std::string> nameList;
       std::vector<int> gradeList;
    };
    
    GradeBook::GradeBook ( std::string name )
    {
       setName ( name );
       nameList ( 10, "" ); // this is illegal
    }
    Is not allowed in C++? A constructor initalzes data members to starting values so why are you not allowed to tell the constructor I want the vector to hold 10 names and then make all the elements empty? ( "" )

    It allowed me to declare the vector as a private member and name it but the only way to do what I need in the constructor is to remove it and create it in new function like this:

    Code:
     std::vector<std::string> nameList ( 10, "" );
    This of course would work. I am not moaning I am only wondering why you cannot do such a simple thing inside a class constructor. Any help appreciated.
    Double Helix STL

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Code:
    GradeBook::GradeBook ( std::string name ) : nameList(10, "")
    {
    }
    Should accomplish what you want.

  3. #3
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Thank you! I thought there was a way of doing it but I could find nothing to get around the error. I appreicate that.
    Double Helix STL

  4. #4
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Yeah. Member initialisation lists are how I generally initialise my variables. Not sure if it's good practise or faster, or slower or anything but I tend to do it.
    For your initialiser I'd do something like:

    Code:
    GradeBook::GradeBook( std::string name )
      : nameList( 10, "" ),
        m_Name( name ) {
    }
    It probably is better to do it this way because it sets values on construction rather than afterwards... might be more efficient to do it there... I really don't know.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by twomers View Post
    Yeah. Member initialisation lists are how I generally initialise my variables. Not sure if it's good practise or faster, or slower or anything but I tend to do it.
    It is good practice, and it is faster in some cases, as for example means using copy-construction instead of default construction followed by assignment. Furthurmore when it comes to references it's actually a necessity.

    In debug builds in particular it is faster because it stops the compiler from filling the variable with dummy values like 0xCCCCCCCC prior to them being assigned real values. Instead they get set directly to what you tell them to be.

    Constructor initialisation lists are very much a good thing to use.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 09:10 AM
  2. Two conceptual questions
    By AntiScience in forum C++ Programming
    Replies: 3
    Last Post: 11-01-2007, 11:36 AM
  3. Defining derivated class problem
    By mikahell in forum C++ Programming
    Replies: 9
    Last Post: 08-22-2007, 02:46 PM
  4. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM