C++ 在函数中生成派生类
考虑以下代码:C++ 在函数中生成派生类,c++,inheritance,C++,Inheritance,考虑以下代码: #include <iostream> class Base { public: Base() : _x(0), _y(0), _type(0){ ; } Base(int type) : _x(0), _y(0), _type(type){ ; } virtual ~Base() { ; } void setX(int x) { _x = x; } void setY(int y) { _y = y; } int
#include <iostream>
class Base {
public:
Base() : _x(0), _y(0), _type(0){ ; }
Base(int type) : _x(0), _y(0), _type(type){ ; }
virtual ~Base() { ; }
void setX(int x) { _x = x; }
void setY(int y) { _y = y; }
int type() { return _type; }
protected:
int _x;
int _y;
int _type;
};
class Derived1 : public Base {
public:
Derived1() : Base(1) { ; }
virtual ~Derived1() { ; }
int getXY1() {
return (_x * _y);
}
};
class Derived2 : public Base {
public:
Derived2() : Base(2) { ; }
virtual ~Derived2() { ; }
int getXY2() {
return (_x * _y);
}
};
void buildDerived(Base* base, int type)
{
if (type == 1)
base = new Derived1();
else if (type == 2)
base = new Derived2();
}
int main(void)
{
Base* base = 0;
buildDerived(base, 1);
if (base && base->type() == 1){
Derived1 *derived1 = dynamic_cast<Derived1 *>(base);
if (derived1)
cout << derived1->getXY1();
}
return 0;
}
#包括
阶级基础{
公众:
Base():x(0),y(0),type(0){;}
基(int类型):x(0),y(0),type(type){;}
虚拟~Base(){;}
void setX(int x){ux=x;}
void setY(int y){u y=y;}
int type(){return\u type;}
受保护的:
int_x;
国际;
int_型;
};
类Derived1:公共基{
公众:
Derived1():基(1){;}
虚拟~Derived1(){;}
int getXY1(){
返回值(x*y);
}
};
派生类2:公共基{
公众:
Derived2():基(2){;}
虚拟~Derived2(){;}
int getXY2(){
返回值(x*y);
}
};
void buildDerived(Base*Base,int类型)
{
如果(类型==1)
base=新的Derived1();
else if(类型==2)
base=新派生的2();
}
内部主(空)
{
基数*基数=0;
(基数1);
如果(基本和基本->类型()==1){
Derived1*Derived1=动态施法(基础);
如果(衍生1)
cout getXY1();
}
返回0;
}
我想要实现的是在传递基类指针作为参数的函数中构建一个派生类
我知道上面的示例可能看起来完全无用,但它是从我的应用程序代码中提取的简化代码。
事实上,在我的代码中,我从前面就不知道派生的类型:在这里将类型作为整数传递是显示我的情况的一种简化方法
我得到的是,派生类函数getXY1
从未被调用,因为base
对象为null。
这里发生了什么事?我如何修改上面的源代码来实现我想要的,从基类开始在一个单独的函数中构建一个派生类(因为我不知道在可能的派生类中会构建什么派生类)。通话后,我可以知道派生类型,然后进行向下广播。这:
void buildDerived(Base* base, int type)
{
if (type == 1)
base = new Derived1();
else if (type == 2)
base = new Derived2();
}
正在修改本地base
参数,但main
例程=>base
中的变量在主例程中仍然为NULL
我会这样做:
Base *buildDerived(int type)
{
Base* base = nullptr; // nullptr if you're using C++11
if (type == 1)
base = new Derived1();
else if (type == 2)
base = new Derived2();
return base;
}
在主菜单中
:
Base* base = buildDerived(1);
这:
正在修改本地base
参数,但main
例程=>base
中的变量在主例程中仍然为NULL
我会这样做:
Base *buildDerived(int type)
{
Base* base = nullptr; // nullptr if you're using C++11
if (type == 1)
base = new Derived1();
else if (type == 2)
base = new Derived2();
return base;
}
在主菜单中
:
Base* base = buildDerived(1);
不能将现有的
Base
对象转换为派生对象。C++不这样工作。< /P>
您可以做的是获取一个现有的基础对象,并构造一个新的派生对象,其基础对象是复制构造的
您需要将另一个构造函数添加到派生类中,让我们使用Derived1
(在您的示例中,Derived2
)的类似构造函数):
这将通过从现有基础对象复制来构造派生对象的基础。当然,这仅在基类具有默认或显式复制构造函数时有效。对于C++11,这也可以扩展为使用移动构造函数。您不能将现有的
基对象
转换为派生对象。C++不这样工作。< /P>
您可以做的是获取一个现有的基础对象,并构造一个新的派生对象,其基础对象是复制构造的
您需要将另一个构造函数添加到派生类中,让我们使用Derived1
(在您的示例中,Derived2
)的类似构造函数):
这将通过从现有基础对象复制来构造派生对象的基础。当然,这仅在基类具有默认或显式复制构造函数时有效。在C++11中,这也可以扩展为使用移动构造函数。
void buildDerived(Base*Base,int-type)
:您不能更改Base
!请执行``void buildDerived(Base*&Base,int-type):
或返回它。很抱歉,我没有收到您的建议。你的意思是通过基地作为参考?您同时键入了指针和引用请参阅我的答案。返回指针更好。void buildDerived(Base*Base,int-type)
:您没有更改Base
!请执行``void buildDerived(Base*&Base,int-type):
或返回它。很抱歉,我没有收到您的建议。你的意思是通过基地作为参考?您同时键入了指针和引用请参阅我的答案。返回指针更好。是否可以通过(以某种方式)传递基类作为buildDerived函数的参数来实现相同的结果?@ABCplus作为Sam应答状态,您可以使用Base
copy构造函数部分初始化对象数据,然后用特定构造函数填充特定参数。是否可以通过(以某种方式)传递基类作为buildDerived函数的参数来实现相同的结果?@ABCplus作为Sam应答状态,您可以使用Base
copy构造函数部分初始化对象数据,然后用一个特定的构造函数填充特定的参数。“你不能把一个现有的基础对象转换成一个派生对象。C++不这样工作。”:不是C++中的多面体是如何工作的吗?不,多态性是另外一回事。您已经有一个具有多态性的现有派生对象。@SamVarshavchik:Derived1(Base&p):Base(p){}不起作用吗?顺便说一句,你的答案回答了真正的OP问题。@Jean-Françoisfare,那也行。const Base&
参数甚至更好。OP指的是指针,所以这就是我使用的。const
就是方法!“不能使用现有的基础对象,将它转换为派生对象。C++不这样工作。”“难道不是C++中的多面体是如何工作的吗?不,多态性是别的东西。”您已经有一个具有多态性的现有派生对象。@SamVarshavchik:Derived1(Base&p):Base(p){}不起作用吗?顺便提一下