【发布时间】:2015-12-25 19:58:05
【问题描述】:
我的类结构如下所示:
class A {
public:
A();
virtual void doSomething() {
qDebug() << "Hello from class A";
}
};
class B : public A {
public:
B();
void doSomething() {
qDebug() << "Hello from class B";
}
};
class C : public A {
public:
C();
void doSomething() {
qDebug() << "Hello from class C";
}
};
在其他地方我有这样的方法:
void doSomethingElse(const A argument = A()) {
argument.doSomething();
}
每次调用doSomethingElse 时,我都会得到“来自A 类的Hello”输出,即使我将B 类或C 类的实例作为参数传递。
我在这里做错了什么?
【问题讨论】:
-
通过引用传递。
标签: c++ inheritance subclass