Thread: How do I call a class constructor from a member object of another class?

  1. #1
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827

    Question How do I call a class constructor from a member object of another class?

    Hello.
    I was wondering how to pass parameters to constructors (which require them) of class object members of another class?

    For example, say I have the following code:

    Code:
    class classA {
          public:
            classA(int i) { num = i; }
    
          private:
            int num;
            
    };
    
    class classB {
          public:
            classB();
    
          private:
            classA myObject; //this is the line I'm talking about;
                                    //how would I pass an integer to the
                                    //constructor of classA from the declaration
                                    //of classB, when "myObject" is an object of
                                    //classA?
    };
    Would I just call the classA object's constructor from classB's constructor, and pass the integer there? But I think I remember reading somewhere that any class object you make a member of another class causes that class's constructor to be automatically called (only without parameters, I'm sure). But how does it handle cases where the class constructor has a required parameter?

    Note that both classes are only hypothetical, and the real class I'm writing is making an object of a Qt class which happens to have a constructor with a required parameter.

    Thanks in advance.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Write the constructor like this:
    Code:
     classB(int foo):myObject(foo);

  3. #3
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by manasij7479 View Post
    Write the constructor like this:
    Code:
     classB(int foo):myObject(foo);
    Thanks. Forgot about initializations lists.

    However, the class I'm actually writing which uses this already passes an argument to a parent class's constructor. Do I just add a comma to that initialization list, followed by the above, separating each class object className(argumentName) with a comma (since I actually have multiple class object class members I need to pass arguments to their constructors)?

    EDIT: Note, I've already done this...just waiting for verification.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Yes, each member to be initialised via the constructor initialisation list is to be separated by a comma.
    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 call to constructor
    By -EquinoX- in forum C++ Programming
    Replies: 2
    Last Post: 11-04-2009, 07:31 AM
  2. Replies: 10
    Last Post: 05-22-2009, 11:12 PM
  3. Replies: 9
    Last Post: 06-20-2008, 02:41 AM
  4. Replies: 8
    Last Post: 03-19-2008, 03:04 AM
  5. To Call an Object Outside of Class
    By marQade in forum C++ Programming
    Replies: 18
    Last Post: 11-25-2007, 05:36 PM