简体   繁体   English

C ++使用抽象基类方法创建派生类

[英]C++ Create derived class with abstract base class method

I'm trying to create an abstract base class, BaseClass , which has some fields (but not all), that are included in the derived classes, DerivedClass1 and DerivedClass2 . 我正在尝试创建一个抽象基类BaseClass ,它具有一些字段(但不是全部),包含在派生类DerivedClass1DerivedClass2

BaseClass.h BaseClass.h

class BaseClass
{
private:
    int bmember1;
    int bmember2;

public:
    BaseClass();
    ~BaseClass();
};

For example, DerivedClass1 would not have additional members... 例如,DerivedClass1将没有其他成员...

DerivedClass1.h 派生类1.h

class DerivedClass1 : public BaseClass
{
public:
    DerivedClass1();
    ~DerivedClass1();
};

... but DerivedClass2 would, dmember1 and dmember2 . ...但是DerivedClass2将是dmember1dmember2

DerivedClass2.h DerivedClass2.h

class DerivedClass2 : public BaseClass
{
private:
    int dmember1;
    int dmember2;

public:
    DerivedClass1();
    ~DerivedClass1();
};

Now, I want some method in BaseClass that I could construct either derived class with, dynamically, using something arbitrary, such as an Enum: 现在,我想要BaseClass中的某个方法,可以使用任意的东西(例如Enum)动态地构造任一派生类:

SwitchEnum.h SwitchEnum.h

enum Switch_Enum
{
    DERIVED_1,
    DERIVED_2
};

I want to use parameters to initialize the fields in the BaseClass with, but I also want to give some default value to the ones present only in the derived classes, that I can later change with a method, again, called from the BaseClass . 我想使用参数来初始化BaseClass的字段,但我也想给仅在派生类中存在的字段提供一些默认值,以后我可以再次使用从BaseClass调用的方法进行更改。 So, something like 所以,像

//(BaseClass.h)

//BaseClass constructor, initializingthe BaseClass's fields with the parameters
BaseClass(int param1, int param2)
{
    //params to bmembers
}

//Construct the appropriate derived class
static BaseClass* createBase(int param1, int param2, Switch_Enum selector);
{
    switch(selector):
    {
    //DerivedClass1 if DERIVED_1, 2 if 2, etc. 
    //use BaseClass(int param1, int param2) to set the BaseClass fields that are present in all derived classes
    //If derived class has additional fields, give default values to those as well
    }
}

And finally, to set the possible additional fields, do something like: 最后,要设置可能的其他字段,请执行以下操作:

//(BaseClass.h)
virtual void changeDmembers(int dmember2val, int dmember2val)
{
    //if DerivedClass1, do nothing
    //if DerivedClass2, set dmember1&2 to dmember1&2val
}

Do you think this is at all feasible? 您认为这完全可行吗? Should I approach this differently? 我应该采取不同的方法吗? Should I have all possible fields already appear in the BaseClass and only use them in the derived classes that would have them (which doesn't seem too elegant to me)? 我是否应该让所有可能的字段都已经出现在BaseClass并且仅在具有它们的派生类中使用它们(对我来说似乎不太优雅)?

Thank you kindly in advance! 预先感谢您!

Do you think this is at all feasible? 您认为这完全可行吗?

Yes. 是。

Should I approach this differently? 我应该采取不同的方法吗?

Most certainly. 可以肯定。 Look up the factory pattern using your favorite search engine. 使用您最喜欢的搜索引擎查找工厂模式。

Should I have all possible fields already appear in the BaseClass and only use them in the derived classes that would have them (which doesn't seem too elegant to me)? 我是否应该让所有可能的字段都已经出现在BaseClass并且仅在具有它们的派生类中使用它们(对我来说似乎不太优雅)?

Your hunch is correct. 你的预感是正确的。 That's not a good idea. 那不是一个好主意。 A base class should not make any assumptions about classes derived from it. 基类不应对从其派生的类做出任何假设。 It should have exactly what it needs -- nothing less, nothing more. 它应该确切地具有所需的内容-没什么,也没有更多。

暂无
暂无

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

相关问题 从抽象基类派生的C ++包装器类,并在另一个也从抽象基类派生的类中调用方法 - C++ wrapper class that is derived from an abstract base class and calls a method in another class that is also derived from the abstract base class 在C ++中创建派生抽象类的实例 - Create instance of derived abstract class in c++ 使用抽象基类C ++中的变量派生的类 - Derived class using variables from abstract base class C++ C ++基础和派生类方法的麻烦 - C++ base and derived class method trouble 抽象类有什么需要? 为什么要通过其基类访问派生类方法? 在C ++中 - What is the need of abstract classes ? Why access a derived classes method through its base class ? in C++ 创建一个抽象 class 从中派生低音 class c++ - create an abstract class from which bass class will be derived c++ C ++重写基类本身的Base类的抽象方法? - C++ overriding abstract method of Base class in the base class itself? C ++:使用抽象类中的指针来访问派生类的方法; 派生类apears也是抽象的 - C++: Use pointer from abstract class for accessing method of derived classes; derived class apears to be abstract as well Abstract Base Class 创建派生 class 的实例,其中 Base class 作为类型 - Abstract Base Class create instance of derived class with Base class as type c ++ - 通过抽象模板基类接口指针访问派生类方法,在接口中没有显式类型 - c++ - accessing derived class method via abstract template base class interface pointer, without explicit type in interface
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM