从基类中选择子类...可能吗?
我正在学习 C++,但我遇到了一个问题。我需要一种在基类中使用特定子类的方法。这有意义还是我使用了错误的方法? SelectBrand应该选择子类,我该怎么做?
下面是我的简化课程:
-----
class Protocol {
public:
Protocol() {};
~Protocol() {};
int openPort();
int readPort(char *buffer);
.....
private:
Protocol (const Protocol&);
};
int Protocol::openPort() {......};
int Protocol::readPort() {.........};
/***********************************************************************************/
class Device{
public:
Device(Protocol& port):_protocol(port){}
~Device();
virtual int getEvent(char *buffer) { return -1; }
int Device::selectBrand();
..............
protected:
Protocol& _protocol;
private:
int brand;
Device(const Device&orig);
};
Device::~Device() {}
int Device::selectBrand() {
......
switch (X)
case 1:
"use subclass Brand_B"
case 2:
"use subclass Brand_B"
.......
}
/***********************************************************************************/
class Brand_A:public Device {
public:
Brand_A(Protocol& port);
~Brand_A();
int getEvent(void *rawData);
private:
Brand_A(const Brand_A&);
};
Brand_A::Brand_A(Protocol& port):Device(port) {}
Brand_A::~Brand_A() {}
int Brand_A::getEvent(void *rawData) {
.... readPort(......);
}
/***********************************************************************************/
class Brand_B:public Device {
public:
Brand_B(Protocol& port);
~Brand_B();
int getEvent(void *rawData);
private:
Brand_B(const Brand_B&);
};
Brand_B::Brand_B(Protocol& port):Device(port) {}
Brand_B::~Brand_B() {}
int Brand_B::getEvent(void *rawData) {
.... readPort(......);
}
/* main **********************************************************/
int main(int argc, char **argv) {
Device *mydev;
char *buffer;
..............
mydev->selectBrand();
..........
mydev->getEvent(buffer);
...........
}
I am learning C++ and I am stuck with a problem. I need a way to use a specific subclass within base class. Does it make sense or I am using a wrong approach? SelectBrand should select the subclass, how can I do it?
Here below my simplified classes:
-----
class Protocol {
public:
Protocol() {};
~Protocol() {};
int openPort();
int readPort(char *buffer);
.....
private:
Protocol (const Protocol&);
};
int Protocol::openPort() {......};
int Protocol::readPort() {.........};
/***********************************************************************************/
class Device{
public:
Device(Protocol& port):_protocol(port){}
~Device();
virtual int getEvent(char *buffer) { return -1; }
int Device::selectBrand();
..............
protected:
Protocol& _protocol;
private:
int brand;
Device(const Device&orig);
};
Device::~Device() {}
int Device::selectBrand() {
......
switch (X)
case 1:
"use subclass Brand_B"
case 2:
"use subclass Brand_B"
.......
}
/***********************************************************************************/
class Brand_A:public Device {
public:
Brand_A(Protocol& port);
~Brand_A();
int getEvent(void *rawData);
private:
Brand_A(const Brand_A&);
};
Brand_A::Brand_A(Protocol& port):Device(port) {}
Brand_A::~Brand_A() {}
int Brand_A::getEvent(void *rawData) {
.... readPort(......);
}
/***********************************************************************************/
class Brand_B:public Device {
public:
Brand_B(Protocol& port);
~Brand_B();
int getEvent(void *rawData);
private:
Brand_B(const Brand_B&);
};
Brand_B::Brand_B(Protocol& port):Device(port) {}
Brand_B::~Brand_B() {}
int Brand_B::getEvent(void *rawData) {
.... readPort(......);
}
/* main **********************************************************/
int main(int argc, char **argv) {
Device *mydev;
char *buffer;
..............
mydev->selectBrand();
..........
mydev->getEvent(buffer);
...........
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我想我应该充实我上面的评论。首先,您可以查看维基百科页面以获取有关抽象工厂模式的更多信息。基本上,它允许您访问接口的不同实现,并在运行时确定所使用的实现。但是,您仍然不知道您将获得哪个实现,因为这是在返回接口实现的工厂方法中决定的。因此,您只能使用接口中的成员,而不能使用特定的实现。使用上面的类的示例如下:
I figured I should flesh out the comment I made above. First of all, you can check out the Wikipedia page for more information on the abstract factory pattern. Basically it allows you to access different implementations of an interface, with the implementation used determined at runtime. However, you still don't know which implementation you're getting as that is decided in the factory method that returns the implementation of the interface. As a result, you can only ever use the members in the interface and not a specific implementation. An example that uses your classes above would be something like:
看起来您可能正在尝试实现多态性之类的东西,而 C++ 会为您做到这一点。如果您在基类中定义虚拟方法并在子类中重写它们,则对指针或对基类型的引用调用这些方法应该会导致调用子类的实现。
例如:
创建时必须知道要使用什么派生类型(子类的类型),以便实例具有派生类型的附加功能。如果不这样做,您获得的只是基类的功能,并且您不能将其视为除基类之外的任何内容(或者如果您的基类继承自某个对象,则不能将其视为继承层次结构中更高的任何内容)。
如果不同的实例实际上没有做任何不同的事情,您甚至可能想使用成员来区分它们。从代码示例中很难准确判断您想要做什么。也许一个更具体的例子来说明你想要实现什么而不是你如何实现它会有所帮助。
It looks like you might be trying to implement something like polymorphism when C++ will do that for you. If you define virtual methods in your base class and override them in your sub classes, calls to those methods on a pointer or reference to the base type should result in the sub class' implementation being called.
For example:
You have to know what derived type (type of subclass) you want to use when you create it so that the instance has the added functionality of the derived type. If you don't, all you get is the functionality of the base class, and you cannot treat it as anything but the base class (or anything further up the inheritance hierarchy if your base class inherits from something).
You may even want to use a member to differentiate between different instances if they're not actually doing anything different. It's hard to tell from the code example exactly what you want to do. Maybe a more specific example of what you're trying to achieve rather than how you're trying to achieve it would help.
请让我重新表述这个问题。我有 1 个基类和一些子类; Brand_A ....Brand_N
现在,在 main() 中,我事先不知道我将使用哪个子类;这个选择需要基类中的一个函数,我称之为 selectBrand。我需要的是一种根据内部条件选择和使用正确子类的机制。我想伪装到 main() 所选的子类。如何得到这个?
please, let me reformulate the problem. I have 1 baseClass and some subclasses; Brand_A....Brand_N
Now, in the main() I don't know in advance which subclass I will use; this selection is demanded to a function in the baseClass which I called selectBrand. What I need is a mechanism to select and use the right subclass based on internal conditions. I want to masquerade to the main() the selected subclass. How to get this?
我实现并测试了这段代码;效果很好。这是好的设计还是可以做得更好?
I implemented and tested this code; it works fine. Is it good design or can be done better?
这不是一个好主意。
一般来说,答案是
dynamic_cast
,但是从基类调用后代的特定行为通常是一个糟糕的设计标志。您可以尝试反转类层次结构并使用模板。
This is not a good idea.
Generally the answer is
dynamic_cast
, but invoking specific behavior of descendants from a base class is usually a bad design sign.You can try inverting the class hierarchy and using templates.