Create a variable to store objects of different C ++ types

I have 3 different objects A

, B

and C

. Depending on the parameter given, I would like to select one of these different objects. In programming

class A { 
  public: 
    void printHello() { cout << "HELLO A" << endl; }
}; 

class B { 
  public: 
    void printHello() { cout << "HELLO B" << endl; }  
};

class C { 
   public: 
     void printHello() { cout << "HELLO C" << endl; }  
}; 

int main () { 
    string key = "c"; 
    A a; 
    B b; 
    C c; 
    Object obj; // I don't know how to declare Object. 

    if (key == "a") obj = a; 
    else if (key == "b") obj = b; 
    else obj = c;
    obj.printHello(); // print Hello C.
    return 0; 
} 

      

I was thinking about templates and structure objects. But none of them still work.

template<typename T1, T2, T3> 
T1 getType(string key, T1 t1, T2 t2, T3 t3) { // this is problem coz return types are different.
    if (key == "a") return t1; 
    else if (key == "b") return t2; 
    else return t3; 
} 

      

Easy to create Object o;

in JAVA

, because every object in Java is a subclass of a class Object

. But how do I achieve this in C ++?

Edit. I cannot change the class A

, B

and C

. I was given these classes to work with and my goal is to implement the method main

. So, inheritance

for me there was no question. Sorry for any confusion.

Any help is appreciated.

+3


source to share


3 answers


You are looking for a type variant

. There are upcoming std::variant

C ++ 17 and C ++ 11 compatible versions on boost

and on the web. Example with : boost::variant



struct visitor
{
    void operator()(const A&){ cout << "HELLO A" << endl; }
    void operator()(const B&){ cout << "HELLO B" << endl; }
    void operator()(const C&){ cout << "HELLO C" << endl; }
};

int main()
{
    visitor v;

    // `obj` is either an `A`, a `B` or a `C` at any given moment.
    boost::variant<A, B, C> obj{B{}};
    //                         ^^^^^
    //                   Initialize with `B`.

    boost::apply_visitor(v, obj); // prints "HELLO B"

    obj = A{};
    boost::apply_visitor(v, obj); // prints "HELLO A"
}

      

+8


source


It seems to me that you should be using a virtual generic base class / struct and a pointer to that base class / struct.

Below is a complete working example



#include <iostream>

struct Base
 { virtual void printHello () = 0; };

class A : public Base { 
  public: 
    void printHello() { std::cout << "HELLO A" << std::endl; }
}; 

class B  : public Base{ 
  public: 
    void printHello() { std::cout << "HELLO B" << std::endl; }  
};

class C  : public Base{ 
   public: 
     void printHello() { std::cout << "HELLO C" << std::endl; }  
}; 

int main () { 
    std::string key = "c"; 
    A a; 
    B b; 
    C c; 
    Base * obj; 

    if (key == "a") obj = &a; 
    else if (key == "b") obj = &b; 
    else obj = &c;
    obj->printHello(); // print Hello C.
    return 0; 
} 

      

+3


source


you can do something like

if (key == "a") obj=new A();
else if (key == "b")  obj=new B();

      

-4


source







All Articles