2

Suppose I have a class as follows:

class A{
    int x ; 
    A( int i ){
        x = i ; 
    }
}

And I have another class B which has an instance of class A as an member object.

class B{
    int y ; 
    A obj_a ; 
    B( int j , A a ){
        y = j ; 
        obj_a = a ; 
    }
}

When I do the following :

int main(){
        A obj1( 1 ) ; // obj.x has value 1
        B obj2( 2 , obj1 ) ; 
    }

The 2nd line throws an error saying no function call of the form A::A(). I know this means that a default style constructor is missing , but why do I need this ? obj1 is created using the defined constructor so that isn't an issue.

My current line of thought is that A a and obj_a = a would invoke the copy constructor which is implicitly defined.

Note: I have excluded private,public etc for brevity.

2
  • 1
    Post the actual error. Your ctor is probably complaining that it should be const int i;
    – stark
    Oct 19, 2016 at 17:59
  • 1
    You explicitly asked for it by not using the initializer list.
    – kfsone
    Oct 19, 2016 at 18:01

2 Answers 2

4

An object must be fully consistent, all of its members constructed, by the time it enters the body of the constructor. So in

B( int j , A a ){
    y = j ; 
    obj_a = a ; 
}

before

{
    y = j ; 
    obj_a = a ; 
}

gets a chance to do anything, obj_a must have been constructed.

Since no instruction on how to construct obj_a has been provided with the Member Initializer List, obj_a will be built with the default constructor for class A. Class A has no default constructor, no A::A(), so the error is raised.

The solution is to use the Member Initializer List instead of assignment inside the body of the function. Not only does this save you construction of an object that will promptly be over written, the compiler also has more lee-way to optimize and you may get some other small improvements.

Class B should be:

class B{
    int y ; 
    A obj_a ; 
    B( int j , A a ): y(j), obj_a(a)
    {
    }
}
2
  • is the object a created by value here ? Is it possible to pass by reference ? Oct 19, 2016 at 19:15
  • @AbhinavVishak a is currently passed by value and probably requires a copy. Yes you can pass a by reference. Oct 19, 2016 at 21:33
3

obj_a = a calls operator=, not the copy constructor. The thing is, obj_a is initialized using the default constructor, because you haven't specified which constructor to call in the initializer list.

B( int j , A a ) /*: obj_a{}*/ { /*...*/ }
                 ^^^^^^^^^^^^^
          implicit call to default constructor

You have to explictly call the constructor with one argument in the member initialization list:

B( int j , A a ) : obj_a{ a } { /*...*/ }
3
  • Well the issue is not so much with obj_a = a . Its the A a inside the parameter list. How do I fix that ? Oct 19, 2016 at 18:08
  • 1
    @AbhinavVishak A a is not the problem when it's in the parameter list. The problem is the lack of obj_a in the mem-initialiser list (the list following the :). Oct 19, 2016 at 18:11
  • @agnew I did the changes you suggested, and it compiles. Thanks ! I'm still a bit confused about the use of initializer lists, wil do some reading on that. I assume that after using the ` : obj_a{a} ` , i wont need the obj_a = a ?? Oct 19, 2016 at 18:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.