C++ C++;两个类相互转换并调用它们的函数
我有两个类。其中一个没有返回值和抛出值,另一个有返回类型,不抛出任何东西。我想要实现的是在彼此之间转换类并调用它们的函数,我不能使用静态方法调用函数C++ C++;两个类相互转换并调用它们的函数,c++,class,oop,casting,C++,Class,Oop,Casting,我有两个类。其中一个没有返回值和抛出值,另一个有返回类型,不抛出任何东西。我想要实现的是在彼此之间转换类并调用它们的函数,我不能使用静态方法调用函数 class ThrowableA{ public: void getvalue(); } class ReturnTypeA{ public: int getvalue(); } class BaseTypeA{//this w
class ThrowableA{
public:
void getvalue();
}
class ReturnTypeA{
public:
int getvalue();
}
class BaseTypeA{//this will accept ThrowableA and ReturnTypeA
...
};
BaseTypeA * d = new ThrowableA();
d->getvalue(); // this will call ThrowableA::getvalue
(do something with d)->getvalue();//this will call ReturnTypeA::getvalue
BaseTypeA * f = new ReturnTypeA();
f->getvalue();//this will call ReturnTypeA::getvalue
(do something with f)->getvalue();//this will call ThrowableA::getvalue
我认为您需要定义复制构造函数(或移动构造函数);或者,您可以定义转换运算符:
class ThrowableA : public BaseTypeA
{
public:
explicit ThrowableA(const ReturnTypeA& rt);
void getvalue();
// or explicit operator ReturnTypeA() const;
}
class ReturnTypeA : public BaseTypeA
{
public:
explicit ReturnTypeA(const ThrowableA& ta);
int getvalue();
// or explicit operator ThrowableA() const;
}
explicit
关键字意味着只有在代码明确请求时才会进行转换。如果没有该关键字,该语言将在必要时自动执行最多一次转换,这可能会导致意外。提到显式转换运算符;这显然是一个C++11特性(而显式构造函数来自C++03)
来回转换不需要继承,但似乎与所需的设置相匹配
然后,要从一个对象转换为另一个对象,只需创建另一个类型的对象:
ThrowableA ta;
// Work with ta, add data, etc.
ReturnTypeA ra(ta);
// Use ra.
您需要以这种方式重新定义代码:
class BaseTypeA;
class ReturnTypeA;
class ThrowableA : public BaseTypeA{
public:
explicit ThrowableA(ReturnTypeA const & rhs);
explicit operator (ReturnTypeA const)(); // explicit conversion operators is a C++11 feature
void getvalue();
};
class ReturnTypeA : public BaseTypeA{
public:
explicit ReturnTypeA(ThrowableA const & rhs);
explicit operator (ThrowableA const)();
int getvalue();
};
class BaseTypeA{//this will accept ThrowableA and ReturnTypeA
...
};
BaseTypeA * d = new ThrowableA();
dynamic_cast<ThrowableA *>(d)->getvalue(); // getvalue isn't in scope of base class.
//(do something with d)->getvalue();//this will call ReturnTypeA::getvalue
BaseTypeA * f = new ReturnTypeA();
dynamic_cast<ReturnTypeA *>(f)->getvalue();//getvalue isn't in scope of base class.
//(do something with f)->getvalue();//this will call ThrowableA::get value
classbasetypea;
A类;
类丢弃A:公共基类型A{
公众:
显式一次性(返回类型A常量和rhs);
显式运算符(ReturnTypeA const)(;//显式转换运算符是C++11的一个特性
void getvalue();
};
类ReturnTypeA:公共基类型A{
公众:
显式返回类型A(一次性常量和rhs);
显式运算符(throwableaconst)();
int getvalue();
};
类BaseTypeA{//这将接受ThrowableA和ReturnTypeA
...
};
BaseTypeA*d=新的一次性软件();
动态_cast(d)->getvalue();//getvalue不在基类的范围内。
//(使用d执行操作)->getvalue()//这将调用ReturnTypeA::getvalue
BaseTypeA*f=新返回类型A();
动态_cast(f)->getvalue()//getvalue不在基类的范围内。
//(使用f执行操作)->getvalue()//这将调用ThrowableA::get value
可以考虑将<代码> GETValue>代码>转换为基类并使其为纯虚拟的,但它们的返回类型不同,因此不可能使返回类型相同,也不可能移动到基类范围。<>代码> BaseType < /C>不具有至少一个虚拟函数,
static\u cast
是最简单的方法。在基类中定义虚拟Destructor。这可以做到,但为什么要dynamic\u cast
呢?因为您正在进行向下转换,如果您的转换不正确,它将返回null ptr。不过,您需要转换它,因为您的基类中没有getvalue
。