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

后端 未结 5 1424
长发绾君心
长发绾君心 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:56

    If the type has a default constructor, you can default-construct an object, immediately destruct it, and then construct it again with the appropriate constructor via placement-new:

    A a;
    a.~A();
    if (isTrue())
    {
        new(&a) A("string");
    }
    else
    {
        new(&a) A(10);
    }
    

    The C++ standard has several examples similar to the above, just search for .~ and ->~.

    Note that this is ultra evil. If your code ever gets reviewed, you are probably going to get fired.

    0 讨论(0)
  • 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;
    
    0 讨论(0)
  • 2021-01-24 20:04

    You can use the template class:

    template<class type> class A
    {
    protected:
        type    T;
    public:
    
        void A(type t_curr) {T = t_curr;};//e.g.---
    
        void check() {}
    };
    
    0 讨论(0)
  • 2021-01-24 20:08

    A a = isTrue() ? A("string") : A(10);

    And if a.check() is a const member function, an alternative may be better:

    const A& a = isTrue() ? A("string") : A(10);

    The object will be destroyed when the reference a go out of scope.

    Note since C++17, according to the rule of copy elision the copy/move constructor is not required to be accessible for this case; copy elision is guaranteed here.

    And since C++17 you can use std::optional, which doesn't cause any dynamic memory allocation. e.g.

    std::optional<A> a;
    if (isTrue())
    {
        a.emplace("string");
    }
    else
    {
        a.emplace(10);
    }
    (*a).check();
    

    BTW: A(char* string) is supposed to be A(const char* string).

    0 讨论(0)
  • 2021-01-24 20:13

    I had the exact same question a while ago and this is what google helped me find:

    unique_ptr<A> foo;
    
    if(isTrue())
        foo = std::unique_ptr<A>(new A("10"));
    else
        foo = std::unique_ptr<A>(new A(10));
    

    Its probably too late for the OP but someone else might hopefully find this useful.

    0 讨论(0)
提交回复
热议问题