简体   繁体   English

c ++继承和共享/非共享函数

[英]c++ inheritance and shared / non shared functions

Thank you in advance for your help. 预先感谢您的帮助。 Here's my problem : 这是我的问题:

I've got sub-class (x, y, z) included in a class (A). 我在(A)类中包含了子类(x,y,z)。 Some functions are shared (declared in A) and other not (declared in the subclass). 某些功能是共享的(在A中声明),而其他功能则不是(在子类中声明)。

All the objects are stored in one map map<string,A>Groups . 所有对象都存储在一个map map<string,A>Groups

Then, I want to do a loop for all the functions using an iterator, but there come the problem of the functions that belonged only to one class, it says that class A doesn't contain functions ... 然后,我想使用迭代器对所有函数进行循环,但是出现了仅属于一个类的函数的问题,它表示类A不包含函数...

I would like to say for(it=Groups.begin(); it!=Groups.end(); ++it) { it->second.functionShared1() if objects1 belongs to class x : it->second.functionsOfClassX and it understand it has to find the function in class x. 我想说for(it=Groups.begin(); it!=Groups.end(); ++it) { it->second.functionShared1() 如果 for(it=Groups.begin(); it!=Groups.end(); ++it) { it->second.functionShared1() 属于类x:it-> second.functionsOfClassX并且它知道必须在类x中找到该函数。

I suppose this will be impossible but if you have an idea of how I can resolve this problem I will be really grateful. 我想这将是不可能的,但是如果您对如何解决这个问题有个想法,我将不胜感激。

I though about creating virtual functions but it will be a mess, or to create a map for each class and a big map containing all the maps. 我虽然要创建虚函数,但是会很混乱,或者为每个类创建一个映射,并创建一个包含所有映射的大映射。 But then I don't know how to iterate it and to declared outermap["x"]=innermap[x]. 但是,然后我不知道如何迭代它并声明externalmap [“ x”] = innermap [x]。 So that's it, sorry I just began C++, I hope I explained well. 就这样,对不起,我才刚开始C ++,希望我能很好地解释。

Why not just make a virtual function: 为什么不做一个虚函数:

class A
{
    //...
    virtual void executeMyJunk();
};

for( it=Groups.begin(); it!=Groups.end(); ++it ) it->second.executeMyJunk();

Provide an implementation for that in your subclasses. 在您的子类中提供该实现。 You can also provide an implementation in A to call functions that are common to all classes. 您还可以在A中提供一个实现,以调用所有类共有的函数。

void A::executeMyJunk()
{
    EveryoneHasThisFunction();
}

void x::executeMyJunk()
{
    // Call common functions
    A::executeMyJunk();

    // Call functions specific to this class
    DoExxyStuff();
}

One thing I should point out is that if you plan to have virtual methods, you will need to store A* (or a smart pointer, eg std::unique_ptr<A> ) in your map, not just an instance of A . 我应该指出的一件事是,如果您打算使用虚方法,则需要在地图中存储A* (或智能指针, 例如 std::unique_ptr<A> ),而不仅仅是A的实例。

I'm not sure if I interpreted your question correctly though. 我不确定我是否正确解释了您的问题。 Perhaps this is not useful to you. 也许这对您没有用。 If you actually meant that you want to execute a specific function only if a class supports that function, then you can use dynamic_cast . 如果您实际上只是想在某个类支持某个功能的情况下才执行该功能,则可以使用dynamic_cast Here I assume that your map stores pointers: 在这里,我假设您的地图存储了指针:

for( it=Groups.begin(); it!=Groups.end(); ++it ) {
    x *xinst = dynamic_cast<x*>(it->second);
    if( x ) x->DoExxyStuff();
}

First off, when you put your objects into you std::map<std::string, A> the objects get sliced : Even if you objects of type x , y , or z into the map, only the base part of the object actually gets copied and stored. 首先,将对象放入std::map<std::string, A> ,对象会被切片 :即使将类型为xyz放入地图中,也仅是对象的基础部分实际上被复制并存储。 If you really mean to store objects of potentially derived objects in the map, you'll need to store some sort of pointer, probably some sort of managed pointer, into the map. 如果您确实打算在地图中存储潜在派生对象的对象,则需要在地图中存储某种指针(可能是某种托管指针)。 For example, you could use 例如,您可以使用

std::map<std::string, std::shared_ptr<A>> Group;

Assuming your class A has at least one virtual function, you could detect if any type is of a specific derived type, eg, using 假设您的A类至少具有一个virtual函数,则可以检测是否有任何类型是特定的派生类型,例如,使用

if (x* ptr = dynamic_cast<x*>(it->second.get())) {
    ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM