创建一个变量来保存不同类型的对象 C++
Create a variable to hold objects of different types C++
我有 3 个不同的对象 A
、B
和 C
。根据给定的参数,我想在这些不同的对象中进行选择。在编程中,
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;
}
我考虑过模板和结构对象。但到目前为止,其中 none 有效。
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;
}
在 JAVA
中创建 Object o;
很容易,因为 Java 中的每个对象都是 Object
class 的子class。但是我如何在 C++ 中实现这一点?
编辑。
我无法更改 class A
、B
和 C
。我得到了这些 classes 可以使用,我的目标是实现 main
方法。所以,inheritance
对我来说是不可能的。抱歉造成任何混乱。
感谢任何帮助。
您正在寻找 variant
类型。在 C++17 中有即将推出的 std::variant
,在 boost
和网络上有 C++11 兼容版本。 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"
}
在我看来,您应该使用虚拟公共基 class/struct 和指向该基的指针 class/struct。
以下是一个完整的工作示例
#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;
}
你可以做类似
if (key == "a") obj=new A();
else if (key == "b") obj=new B();
我有 3 个不同的对象 A
、B
和 C
。根据给定的参数,我想在这些不同的对象中进行选择。在编程中,
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;
}
我考虑过模板和结构对象。但到目前为止,其中 none 有效。
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;
}
在 JAVA
中创建 Object o;
很容易,因为 Java 中的每个对象都是 Object
class 的子class。但是我如何在 C++ 中实现这一点?
编辑。
我无法更改 class A
、B
和 C
。我得到了这些 classes 可以使用,我的目标是实现 main
方法。所以,inheritance
对我来说是不可能的。抱歉造成任何混乱。
感谢任何帮助。
您正在寻找 variant
类型。在 C++17 中有即将推出的 std::variant
,在 boost
和网络上有 C++11 兼容版本。 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"
}
在我看来,您应该使用虚拟公共基 class/struct 和指向该基的指针 class/struct。
以下是一个完整的工作示例
#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;
}
你可以做类似
if (key == "a") obj=new A();
else if (key == "b") obj=new B();