简体   繁体   English

C ++基础和派生类方法的麻烦

[英]C++ base and derived class method trouble

I have 1 base class named AddOns, this class has 9 derived classes. 我有1个名为AddOns的基类,该类有9个派生类。 Also I have vector defined as shared_ptr 我也将向量定义为shared_ptr

AddOnsContainer.h AddOnsContainer.h

std::vector<std::shared_ptr<AddOns>> container;

Now my problem is that I have defined a method in AddOns 现在我的问题是我在AddOns中定义了一个方法

AddOns.h 插件

virtual void ChangeDefaultSearchProvider();

that I want to override in only one of the derived classes (SearchProviders). 我只想在一个派生类(SearchProviders)中覆盖。 For every other derived class I want to use the code implemented in AddOns.h. 对于每个其他派生类,我想使用AddOns.h中实现的代码。 The method is defined as virtual, which should have helped me, but my code still runs the base method for every one derived class even for the one that I have overrode. 该方法被定义为虚拟方法,这应该对我有所帮助,但是我的代码仍然为每个派生类(甚至对于我覆盖的那个类)都运行基本方法。

Here is the definition from the method from the derived class that should do something different. 这是派生类中方法的定义,应该做一些不同的事情。

SearchProviders.h SearchProviders.h

virtual void ChangeDefaultSearchProvider() override final;

From here I call the method: 从这里我调用方法:

AddOnsContainer.cpp AddOnsContainer.cpp

void AddOnsContainer::ChangeDefaultSearchProvider(int pId)
{
    container[pId]->ChangeDefaultSearchProvider();
}

So could someone help me to understand how should I define the method in my base class, so that later on I can override it in only one of my derived classes. 因此有人可以帮助我了解如何在基类中定义该方法,以便以后可以在仅一个派生类中重写该方法。

Otherwise I should implement this method in each derived class, but as I know this is not the idea of OOP and inheritance. 否则,我应该在每个派生类中实现此方法,但是据我所知,这不是OOP和继承的概念。

Thank you all in advance! 谢谢大家!

Best Regards, Mario P. 此致,Mario P.

PS PS

This is the code where I add items(SearchProviders) to the vector: 这是我在矢量中添加item(SearchProviders)的代码:

SearchProvider searchProviders;

while (searchProviders.FindNext())
{
    add_item(searchProviders);
}

Thank you all for the help! 谢谢大家的帮助!

I found the problem in my code during the process of creating minimal code. 在创建最少的代码的过程中,我在代码中发现了问题。

The problem was in: AddOnsContainer.cpp 问题出在:AddOnsContainer.cpp

void AddOnsContainer::ChangeDefaultSearchProvider(int pId)
{
    container[pId]->ChangeDefaultSearchProvider();
}

Because I pass pId directly, and I need to subtract 1, because currently I do not use the Graphic interface and I use only the console variant. 因为我直接传递了pId ,所以我需要减去1,因为当前我不使用图形界面,而仅使用控制台变量。

However, as it took me some time to create some minimal variation of my original code I decided to upload it to GitHub with link here in Stackoverflow. 但是,由于花了一些时间来创建原始代码的一些最小变体,因此我决定通过Stackoverflow中的链接将其上传到GitHub。

The code can be access via: https://github.com/mariopavlov/C/tree/master/InheritanceExample 可以通过以下代码访问代码: https : //github.com/mariopavlov/C/tree/master/InheritanceExample

Thank you again all! 再次谢谢大家! Best, Mario P. 最好,马里奥·P。

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

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