C++;是否在数组中存储任何子结构/类? 在C++中,我定义了一系列结构……/p> struct Component {}; struct SceneGraphNode : Component {}; struct Renderable : Component {}; struct Health : Component {}; 这些很容易是类,我已经被告知,C++中的差别不大。
在Java中,可以声明类型为C++;是否在数组中存储任何子结构/类? 在C++中,我定义了一系列结构……/p> struct Component {}; struct SceneGraphNode : Component {}; struct Renderable : Component {}; struct Health : Component {}; 这些很容易是类,我已经被告知,C++中的差别不大。,c++,C++,在Java中,可以声明类型为Component的数组,并将从Component扩展的任何类放入其中。Java将它们视为所有组件,因为所有东西都使用智能指针,所以Java的“数组”实际上只是一个大小相同的智能指针列表 但是我理解java处理C++的数组有很大的不同。当我检查每个结构的大小时,我得到了以下结果 Component // 4 SceneGraphNode : Component // 8 Renderable : Component //
Component
的数组,并将从Component
扩展的任何类放入其中。Java将它们视为所有组件,因为所有东西都使用智能指针,所以Java的“数组”实际上只是一个大小相同的智能指针列表
<>但是我理解java处理C++的数组有很大的不同。当我检查每个结构的大小时,我得到了以下结果
Component // 4
SceneGraphNode : Component // 8
Renderable : Component // 8
Health : Component // 20
这并不奇怪。现在,当我创建一个组件数组时,块的大小显然将是4(字节),这将不包含任何其他结构
所以我的问题是,如何存储
组件的松散列表(即可以存储从组件继承的任何类/结构的列表)?在Java中,这是非常简单的。在C++中肯定有一个简单的方法来做。 您可以按如下方式存储它们
std::vector<Component *> components;
Component * c = new Health;
components.push_back( c );
std::向量分量;
组件*c=新的运行状况;
组件。推回(c);
调用组件[0]->method()代码>将调用运行状况的方法()
<>这是C++中多态性的实现。
还要确保组件的method()
是virtual
您存储它们如下
std::vector<Component *> components;
Component * c = new Health;
components.push_back( c );
std::向量分量;
组件*c=新的运行状况;
组件。推回(c);
调用组件[0]->method()代码>将调用运行状况的方法()
<>这是C++中多态性的实现。
同时确保组件的method()
是virtual
您可以存储指向基类对象的智能指针的向量,然后可以将派生类对象添加到其中
例如:
std::vector<std::unique_ptr<Component> > base;
Component.push_back( std_unique_ptr<Component>(new SceneGraphNode()) );
//^^use correct constructor, this example just show the methodology
Component.push_back( std_unique_ptr<Component>(new Renderable()) );
std::向量基;
Component.push_back(std_unique_ptr(new SceneGraphNode());
//^^使用正确的构造函数,此示例仅显示方法
Component.push_back(std_unique_ptr(newrenderable());
您可以存储指向基类对象的智能指针的向量,然后可以向其中添加派生类对象
例如:
std::vector<std::unique_ptr<Component> > base;
Component.push_back( std_unique_ptr<Component>(new SceneGraphNode()) );
//^^use correct constructor, this example just show the methodology
Component.push_back( std_unique_ptr<Component>(new Renderable()) );
std::向量基;
Component.push_back(std_unique_ptr(new SceneGraphNode());
//^^使用正确的构造函数,此示例仅显示方法
Component.push_back(std_unique_ptr(newrenderable());
您可以拥有指向子类对象的基类指针,即
Component*sgn=newscenegraphnode
因此,分配一个Component*
s数组(或者一个向量,如果它的大小需要变化的话),并将每个入口点设置为一个派生对象
Component * components[100];
components[0] = new SceneGraphNode;
components[1] = new Renderable;
// so on and so on
除此之外,对于要在子类中定义的任何成员函数,必须在组件中具有虚拟函数
class Component {
public:
virtual void method() = 0;
virtual int method2(int arg) = 0;
};
class SceneGraphNode : public Component {
public:
virtual void method(){
//body
}
virtual int method2(int arg){
return arg*2;
}
};
virtual
关键字使其在运行时查看指向对象的实际类型并调用其方法,而不是调用指针类型方法。这就是java正常工作的方式。=0
使函数“纯虚拟”意味着子类必须定义该方法。使用上面定义的数组
components[0]->method();
compenents[2]->method2(1);
如果您更喜欢矢量而不是数组,则可以将数组版本替换为:
#include <vector>;
//...
std::vector<Component* > components;
components.push_back(new SceneGraphNode);
components.push_back(new Renderable);
#包括;
//...
std::向量分量;
组件。推回(新的SceneGraphNode);
组件。推回(新可渲染);
您可以拥有指向子类对象的基类指针,即
Component*sgn=newscenegraphnode
因此,分配一个Component*
s数组(或者一个向量,如果它的大小需要变化的话),并将每个入口点设置为一个派生对象
Component * components[100];
components[0] = new SceneGraphNode;
components[1] = new Renderable;
// so on and so on
除此之外,对于要在子类中定义的任何成员函数,必须在组件中具有虚拟函数
class Component {
public:
virtual void method() = 0;
virtual int method2(int arg) = 0;
};
class SceneGraphNode : public Component {
public:
virtual void method(){
//body
}
virtual int method2(int arg){
return arg*2;
}
};
virtual
关键字使其在运行时查看指向对象的实际类型并调用其方法,而不是调用指针类型方法。这就是java正常工作的方式。=0
使函数“纯虚拟”意味着子类必须定义该方法。使用上面定义的数组
components[0]->method();
compenents[2]->method2(1);
如果您更喜欢矢量而不是数组,则可以将数组版本替换为:
#include <vector>;
//...
std::vector<Component* > components;
components.push_back(new SceneGraphNode);
components.push_back(new Renderable);
#包括;
//...
std::向量分量;
组件。推回(新的SceneGraphNode);
组件。推回(新可渲染);
Awesome,我不知道需要将这些方法定义为虚拟的。谢谢太棒了,我不知道有必要将这些方法定义为虚拟的。谢谢<代码>标准::向量分量代码>将泄漏内存,使用智能指针,共享或唯一\u ptrstd::vector组件代码>将泄漏内存,请使用智能指针(共享\u ptr或唯一\u ptr)