Thread: initializer list help

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    75

    initializer list help

    HI, im just getting a grasp on initializer lists, and learnt that const should be iniatilized in there, how do i go about doing that.
    How do i iniatilize dateofbirth,
    i tried birthdate=dateOfbirth, and got a lot of error, so im guessing thats wrong.

    Code:
    class Brain
    {
    public:
    	Brain(int iq);
    	void setIQ(int iq);
    };
    
    class String
    {
    public:
    	String(const char* str);
    	String(const String& other);
    	const String& operator=(const String& other);
    	const String& operator=(const char* str);
    	~String();
    };
    
    class Car {};
    
    class Person
    {
    public:
    	Person(const char* rName, long dateOfBirth, int iq, int rIncome):brain(iq),name(rName)
    	{
    		income=rIncome;
    		
    	}
    
    private:
    	String name;
    	Brain brain;
    	const long birthdate;
    	Car* pCar;
    	double income;
    };

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    161
    The same way you did brain and name:

    Code:
    Person(const char* rName, long dateOfBirth, int iq, int rIncome)
    : brain(iq),name(rName), birthdate(dateOfBirth)

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    75
    thanks, but brain and name were objects and had constructors, how does this workout?

  4. #4
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    const objects can be assigned to only through their constructors (or similar syntax if you're not using objects.)

    ie:
    Object o;
    const Object p = o; //this is legal because C++ considers o an argument of the p constructor
    const Object q; //this cannot be assigned to
    const Object r(p); //this is also legal

    Code:
    class Base {
    public:
      const Object o;
      Base();
      Base(const Object& opt) : o(opt) {
        //the above is legal since opt is assigned to o using its constructor
        o = opt; //the object is already constructed, so this is illegal
        //even if you don't explicitly use the initializer list
      } 
    };
    //edit: the same goes for non-objects like int's and doubles

  5. #5
    Registered User
    Join Date
    Mar 2003
    Posts
    75
    so that means when in a class i do this

    Code:
    class X
    {
       public:
        some constructor(values)
    { 
    }
    
    private:
    const int test;
    }
    I can initialize it in the initializer list as test(whatever) ??? It was not a const object, but a member variable

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Anyone good with linked list.....I am not....
    By chadsxe in forum C++ Programming
    Replies: 11
    Last Post: 11-10-2005, 02:48 PM
  2. instantiated from here: errors...
    By advocation in forum C++ Programming
    Replies: 5
    Last Post: 03-27-2005, 09:01 AM
  3. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 06:13 AM
  4. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM