C++ 无法访问具有基类的共享\u ptr的派生类的成员
我尝试使用C++ 无法访问具有基类的共享\u ptr的派生类的成员,c++,visual-studio-2013,polymorphism,dynamic-memory-allocation,C++,Visual Studio 2013,Polymorphism,Dynamic Memory Allocation,我尝试使用shared\u ptr以多态形式使用以下代码 using namespace std; class Base { public: int ClassID; // Used only by derived classes string Name; shared_ptr<Base> Parent; // For TreeCtrl-like hierarchy } class DerivedA : public Base { public: d
shared\u ptr
以多态形式使用以下代码
using namespace std;
class Base
{
public:
int ClassID; // Used only by derived classes
string Name;
shared_ptr<Base> Parent; // For TreeCtrl-like hierarchy
}
class DerivedA : public Base
{
public:
double Length;
double Width;
double Height;
}
class DerivedB: public Base
{
public:
double Radius;
}
class Controller
{
public:
vector<shared_ptr<Base>> Objects;
void CreateObject(int class_id, string name, shared_ptr<Base> parent)
{
shared_ptr<Base> temp;
switch(class_id)
{
case DERIVEDA: // enum definition assumed
temp = make_shared<DerivedA>(name, parent); // constructors assumed
break;
case DERIVEDB:
temp = make_shared<DerivedB>(name, parent);
break;
}
Objects.push_back(temp);
}
}
使用名称空间std;
阶级基础
{
公众:
int ClassID;//仅由派生类使用
字符串名;
shared_ptr Parent;//用于类似树的层次结构
}
DerivedA类:公共基础
{
公众:
双倍长度;
双倍宽度;
双高;
}
B类:公共基
{
公众:
双半径;
}
类控制器
{
公众:
矢量对象;
void CreateObject(int类\u id、字符串名称、共享\u ptr父级)
{
共享ptr温度;
开关(类别号)
{
case-DERIVEDA://假定枚举定义
temp=make_shared(name,parent);//假定构造函数
打破
案例B:
temp=使共享(名称、父级);
打破
}
对象。推回(临时);
}
}
我的问题是,当我检查多态性是否正常工作时(使用VS2013的调试器),我只得到
Base
类的成员(我的研究表明,我的函数也会有同样的问题)。使用动态指针\u cast
似乎不起作用。如何才能访问派生类的所有成员?dynamic\u pointer\u cast
依靠dynamic\u cast
正常工作。如果dynamic\u cast
不起作用,dynamic\u pointer\u cast
也不起作用
在您的情况下,dynamic\u cast
不起作用,因为Base
没有任何virtual
成员函数。将virtual
成员函数添加到Base
,或使Base
的析构函数成为虚拟析构函数。在此之后,动态指针\u cast
应该可以工作
class Base
{
public:
// Add this.
virtual ~Base() {}
int ClassID; // Used only by derived classes
string Name;
shared_ptr<Base> Parent; // For TreeCtrl-like hierarchy
};
类基
{
公众:
//加上这个。
虚拟~Base(){}
int ClassID;//仅由派生类使用
字符串名;
shared_ptr Parent;//用于类似树的层次结构
};
dynamic\u pointer\u cast
依靠dynamic\u cast
正常工作。如果dynamic\u cast
不起作用,dynamic\u pointer\u cast
也不起作用
在您的情况下,dynamic\u cast
不起作用,因为Base
没有任何virtual
成员函数。将virtual
成员函数添加到Base
,或使Base
的析构函数成为虚拟析构函数。在此之后,动态指针\u cast
应该可以工作
class Base
{
public:
// Add this.
virtual ~Base() {}
int ClassID; // Used only by derived classes
string Name;
shared_ptr<Base> Parent; // For TreeCtrl-like hierarchy
};
类基
{
公众:
//加上这个。
虚拟~Base(){}
int ClassID;//仅由派生类使用
字符串名;
shared_ptr Parent;//用于类似树的层次结构
};
dynamic\u pointer\u cast
依靠dynamic\u cast
正常工作。如果dynamic\u cast
不起作用,dynamic\u pointer\u cast
也不起作用
在您的情况下,dynamic\u cast
不起作用,因为Base
没有任何virtual
成员函数。将virtual
成员函数添加到Base
,或使Base
的析构函数成为虚拟析构函数。在此之后,动态指针\u cast
应该可以工作
class Base
{
public:
// Add this.
virtual ~Base() {}
int ClassID; // Used only by derived classes
string Name;
shared_ptr<Base> Parent; // For TreeCtrl-like hierarchy
};
类基
{
公众:
//加上这个。
虚拟~Base(){}
int ClassID;//仅由派生类使用
字符串名;
shared_ptr Parent;//用于类似树的层次结构
};
dynamic\u pointer\u cast
依靠dynamic\u cast
正常工作。如果dynamic\u cast
不起作用,dynamic\u pointer\u cast
也不起作用
在您的情况下,dynamic\u cast
不起作用,因为Base
没有任何virtual
成员函数。将virtual
成员函数添加到Base
,或使Base
的析构函数成为虚拟析构函数。在此之后,动态指针\u cast
应该可以工作
class Base
{
public:
// Add this.
virtual ~Base() {}
int ClassID; // Used only by derived classes
string Name;
shared_ptr<Base> Parent; // For TreeCtrl-like hierarchy
};
类基
{
公众:
//加上这个。
虚拟~Base(){}
int ClassID;//仅由派生类使用
字符串名;
shared_ptr Parent;//用于类似树的层次结构
};
您应该使用dyncamic\u pointer\u cast将其转换为基类,并将所有相关函数都设置为虚拟函数-您记得这样做吗?EWW ClassID。这几乎总是最糟糕的解决方案。@DavidHaim-我在使用dynamic\u pointer\u cast
时遇到了问题。现在,没有任何函数。只关注会员@Puppy-主要用于保存到文件或从文件加载。您应该使用dyncamic_pointer_cast将其转换到基类,并使所有相关函数虚拟化-您记得这样做吗?EWW ClassID。这几乎总是最糟糕的解决方案。@DavidHaim-我在使用dynamic\u pointer\u cast
时遇到了问题。现在,没有任何函数。只关注会员@Puppy-主要用于保存到文件或从文件加载。您应该使用dyncamic_pointer_cast将其转换到基类,并使所有相关函数虚拟化-您记得这样做吗?EWW ClassID。这几乎总是最糟糕的解决方案。@DavidHaim-我在使用dynamic\u pointer\u cast
时遇到了问题。现在,没有任何函数。只关注会员@Puppy-主要用于保存到文件或从文件加载。您应该使用dyncamic_pointer_cast将其转换到基类,并使所有相关函数虚拟化-您记得这样做吗?EWW ClassID。这几乎总是最糟糕的解决方案。@DavidHaim-我在使用dynamic\u pointer\u cast
时遇到了问题。现在,没有任何函数。只关注会员@Puppy-主要用于保存到文件或从文件加载。