C++ 不需要从基指针到派生指针的动态转换。他们应该能够使用基指针。 无论如何,这是一个不同的问题,你还没有问过。我一直支持你,直到3点,为什么任何一个nessesitate在任何地方都有Base**对象,而不仅仅是std::map(或者更好的是std::map)
不需要从基指针到派生指针的动态转换。他们应该能够使用基指针。C++ 不需要从基指针到派生指针的动态转换。他们应该能够使用基指针。 无论如何,这是一个不同的问题,你还没有问过。我一直支持你,直到3点,为什么任何一个nessesitate在任何地方都有Base**对象,而不仅仅是std::map(或者更好的是std::map),c++,architecture,containers,C++,Architecture,Containers,不需要从基指针到派生指针的动态转换。他们应该能够使用基指针。 无论如何,这是一个不同的问题,你还没有问过。我一直支持你,直到3点,为什么任何一个nessesitate在任何地方都有Base**对象,而不仅仅是std::map(或者更好的是std::map)?它可以很优雅,但它要求您在抽象的阶梯上更上一层楼,并通过将代码基于Base指针来实际利用多态性。如有必要,利用访客模式进行类型识别。如果没有具体的用例,就不可能给出具体的建议。可能是主容器的整个概念妨碍了您。@Collin组件定义成员Deri
无论如何,这是一个不同的问题,你还没有问过。我一直支持你,直到3点,为什么任何一个nessesitate在任何地方都有
Base**
对象,而不仅仅是std::map
(或者更好的是std::map
)?它可以很优雅,但它要求您在抽象的阶梯上更上一层楼,并通过将代码基于Base
指针来实际利用多态性。如有必要,利用访客模式进行类型识别。如果没有具体的用例,就不可能给出具体的建议。可能是主容器的整个概念妨碍了您。@Collin组件定义成员Derived1*、Derived2*等,以指向对象实例。为了填充这些值,我需要把一个指针列表传递给这些成员(即基**)到顶层搜索。很少有任何很好的理由来实现自己的Type ID系统,C++是通过<代码> DyrimCysCase中构建的。@科兰,由于某种原因,我认为OP不想使用DyrimeSkyCask,并希望在强制转换之前确定确切的派生类型。让我们等待他的意见。我的口头描述明显不足,所以我编辑了问题,加入了示例代码。您(非常清楚)的示例中没有包括的主要内容是“主容器”在组件外部,而对象是组件的(指针)成员。因此,在我的设想中,组件需要传递此成员的位置(p-to-p)以及标识符,以便外部进程查找并填充适当的指针。这很有趣……从类声明中的映射赋值。如果直到运行时才填充ObjectStore会怎么样?
#include <map>
#include <vector>
#include <string>
using namespace std;
class BaseObj {};
class Der1Obj: public BaseObj {};
class Der2Obj: public BaseObj {};
typedef map<string, BaseObj**> ObjPtrDict;
typedef map<string, BaseObj*> ObjDict;
class BaseComp
{
public:
ObjPtrDict objs;
};
class DervComp
{
DervComp(){objs["d1"] = &d1; objs["d2"] = &d2; } // This wouldn't compile
Der1Obj* d1;
Der2Obj* d2;
}
typedef vector<BaseComp*> CompList;
void assign_objs(CompList comps, ObjDict objs)
{
for (auto c = comps.begin(); c != comps.end(); c++)
for (auto o = c.objs.begin(); o != c.objs.end(); o++)
*(o->second) = objs[o->first];
}
int main(int argc, char* argv[])
{
Der1Obj d, d1;
Der2Obj d2;
ObjDict objs;
objs["d"] = &d;
objs["d1"] = &d1;
objs["d2"] = &d2;
DervComp c;
vector<DervComp*> comps;
comps.push_back(&c);
assign_objs(comps, objs);
return 0;
}
#include <vector>
class Base
{
public:
enum eDerived
{
//name these whatever you like
DER1,//for first derived class
DER2,//for second derived class
DER3//for third derived class
};
virtual eDerived type() = 0;//this will return the class type.
};
class Derived1: public Base
{
public:
virtual eDerived type() { return DER1; }
};
class Derived2: public Base
{
public:
virtual eDerived type() { return DER2; }
};
class Derived3: public Base
{
public:
virtual eDerived type() { return DER3; }
};
int main()
{
std::vector<Base*> myList;//container for all elements
//You can store a pointer to any of the derived classes here like this:
Base * a = new Derived1();
Base * b = new Derived2();
Base * c = new Derived3();
myList.push_back(a);
myList.push_back(b);
myList.push_back(c);
//Iterating through the container
for( Base * tmp: myList)
{
//You can check the type of the item like this:
if( tmp->type() == Base::DER1 )
{
//and cast to a corresponding type.
//In this case you are sure that you are casting to the right type, since
//you've already checked it.
Derived1 * pointerToDerived1 = static_cast<Derived1 *>(tmp);
}
}
}
class Base
{
};
class Deriv : public Base
{
};
std::map< std::string, Base* > ObjectStore;
function Component1( ... )
{
Base* b = ObjectStore[ "MyObject" ];
b->DoSomeFancyStuff();
}
function ModifyObjectStore( )
{
delete ObjectStore[ "MyObject" ];
ObjectStore[ "MyObject" ] = new Derived();
}
class Container
{
void add(const string& aKey_in, Base* b)
{
myObjects[aKey_in] = b;
}
void getObjs(list<string> aKeys_in, map<string,Base*>& anObjMap_out)
{
for(all string s in the aKeys_in)
anObjMap_out[s] = myObjects[s];
}
private:
map<string, base*> myObjects;
};