[英]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
,它具有一些字段(但不是全部),包含在派生类DerivedClass1
和DerivedClass2
。
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将是dmember1
和dmember2
。
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.