0

I am try to make a class containing another class as member variable. And I encounter the follow problem:

class SubClass{
    ....
};

class MainClass{
   public:
    MainClass(SubClass const & subClass_);
   private:
    SubClass subClass
};

and in the .cpp files for the constructor

MainClass::MainClass(SubClass const & subClass_){
   subClass =  subClass_;
}

This gives out compiler errors. But the following works:

MainClass::MainClass(SubClass const & subClass_):
   subClass(subClass_) {}

Could anyone tell me what is the difference of these two?

Cole Tobin
  • 9,206
  • 15
  • 49
  • 74
Mark5907
  • 411
  • 1
  • 5
  • 16
  • 1
    One is assigning after construction, the other is copy constructing. The declaration of `SubClass` is key to understanding why one compiles and why one does not. – johnsyweb Feb 25 '13 at 02:28
  • 1
    Please fill in the content of `....` in the declaration of `SubClass`. – Sergey Kalinichenko Feb 25 '13 at 02:29
  • 2
    [This one](http://stackoverflow.com/questions/1711990/what-is-this-weird-colon-member-syntax-in-the-constructor), anyone? There's a big heading titled **What is the difference between Initializing and Assignment inside constructor?**. – chris Feb 25 '13 at 02:32
  • What are the compiler errors? – Nate Hekman Feb 25 '13 at 02:33
  • 1
    @Nate, I suspect there's no matching copy assignment operator or no default constructor. – chris Feb 25 '13 at 02:34

3 Answers3

0

This most likely indicates that SubClass does not support assignment operator (which is required in your former code). The latter code does not invoke assignment but rather copy initializes subClass member from subClass_

More can be said once you put in the definition of SubClass

Chubsdad
  • 24,777
  • 4
  • 73
  • 129
  • That is not the only possibility. It would also fail to compile if `SubClass`'s default constructor cannot be called. – Max May 08 '13 at 22:52
0

You cannot copy an object by simply using '=' unless this operator is "overloaded" to somehow clone the object. Here though, subClass has a constructor that copies the input object into the new one, that is why you could use the second code.

0
  • in the first case operator= is used, it is applied to the already created instance of the class (first default instance of the class is created and then operator= invoked)
  • in the second case using member initialization allows to use copy constructor for creation of the class instance therefore initializing it initially with needed value.

Difference:
2-nd case is considered more efficient, as it takes less operations to perform.
1-st case - demands class to have default constructor.

You problem is likely result of absence of implementation of default constructor for MainClass, as long as you have implemented you own version of constructor - compiler no longer creates special member function (default constructor)

Solution: implement default constructor / use member initialization syntax (choice variant according to the logic of you application)

spin_eight
  • 3,925
  • 10
  • 39
  • 61