C++: how to choose the constructor depending on the condition?

后端 未结 5 1425
长发绾君心
长发绾君心 2021-01-24 19:30

Assume I have a class with different constructors:

class A
{
public:
    A(char* string)
    {
        //...
    }

    A(int value)
    {
        //..
    }

           


        
5条回答
  •  滥情空心
    2021-01-24 19:58

    You can't satisfy all your stated requirements.

    If you can get rid of the requirement for the object to be on stack, you could use a pointer.

    A *a;
    if (isTrue())
        a = new A("string");
    else
        a = new A(10);
    a->check();
    delete a;
    

提交回复
热议问题