c++ Using overloaded constructor on object composition

拈花ヽ惹草 提交于 2019-12-24 13:12:15

问题


may I know how do i use a overloaded constructor for object composition in another class, here's an example code:

class A {
    int a;

    A( int inputA ) { a = inputA; }
};

class B {
    A objectA;

    B( A inputObjectA ) { objectA = inputObjectA; }
};

The error with the compiler was there is no default constructor A::A() ?

Is there any way to edit the code in the parameter of B's constructor to accept the overloaded constructor in A?


回答1:


If a child has no default constructor, parent's constructor should pass parameters.

class A {
public:
    int a;
    A( int inputA ) { a = inputA; }
};

class B {
    A objectA;
public:
    B( A inputObjectA ): objectA(inputObjectA.a) { }
};

http://ideone.com/QavhfY

In your case it would be appropriate to use a copy constructor for A, instead of making its member public:

class A {
    int a;
public:
    A( int inputA ) { a = inputA; }
};

class B {
    A objectA;
public:
    B(A inputObjectA ): objectA(inputObjectA) { }
};

http://ideone.com/wX1clr




回答2:


In your class B object you have

A objectA;

That is when the default constructor is being called, so in there you need to either have a designated default constructor, or you can alternatively use a default parameter, such as

A( int inputA = 0 ) { a = inputA; }

This will then take the role that set's the integer a to 0, when you call the default constructor, it really would achieve the same as

A() { a = 0; } 

though you don't have to set a if you don't want to, but since you declared a non default constructor, it no longer implicitly creates one for you. So you will need at minimum of

A(){ }  

Another way you could do it is

class A {
    int a;
    void setSomething(int val) { a = val; }
};

class B {
    A objectA;
    B( A inputObjectA ) { objectA = inputObjectA; }
};

This works because you never declared any constructor, so it will implicitly create a default one for you

Lastly, this is one other way you could do it, probably closest to your original code with only 3 characters added, should fix everything:

class A {
    int a;

    A( int inputA ) { a = inputA; }
};

class B {
    A objectA(0);

    B( A inputObjectA ) { objectA = inputObjectA; }
};



回答3:


Just pass A()'s parameters to constructor of B(). This solution works for A which is neither default constructable, nor copy or move constructable/assignable.

class A 
{
    int a;

    // Just to demonstrate non copy-move-ness (can be removed)
    A() = delete;
    A( A& ) = delete;
    A( A&& ) = delete;
    A& operator=( A& ) = delete;
    A& operator=( A&& ) = delete;
public:
    A( int inputA ): a(inputA ) {}
};

class B 
{
    A objectA;
public:
    B( int inputA ): objectA(inputA ) { }
};


int main() 
{
    B(42);
}

Proof

Tip: don't use assignment in constructor body, use constructor init lists instead.

All this info you could get from any modern C++ book.




回答4:


You need a default constructor, because you have an instance of class A inside class B. If you read a bit about the order in which constructors are called, you'll notice that first there are called default constructors for members, and then the main class constructor. So - in your example - first the compiler tries to init objectA with a default value, and it fails, because there is no default constructor.



来源:https://stackoverflow.com/questions/19886588/c-using-overloaded-constructor-on-object-composition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!