C++继承-子类构造函数调用?
我的主要内容如下:
Sum *sum = new Sum(Identifier("aNum1"), Identifier("aNum2"));
我的类是:
class Table {
private:
static map<string, int> m;
public:
static int lookup(string ident)
{
return m.find(ident)->second;
}
static void insert(string ident, int aValue)
{
m.insert(pair<string, int>(ident, aValue));
}
};
class Expression {
public:
virtual int const getValue() = 0;
};
class Identifier : Expression {
private:
string ident;
public:
Identifier(string _ident) { ident = _ident; }
int const getValue() { return Table::lookup(ident); }
};
class BinaryExpression : public Expression {
protected:
Expression *firstExp;
Expression *secondExp;
public:
BinaryExpression(Expression &_firstExp, Expression &_secondExp) {
firstExp = &_firstExp;
secondExp = &_secondExp;
}
};
class Sum : BinaryExpression {
public:
Sum(Expression &first, Expression &second) : BinaryExpression (first, second) {}
int const getValue()
{
return firstExp->getValue() + secondExp->getValue();
}
};
当我编译它时,我收到以下错误:
没有调用“Sum::Sum(Identifier, Identifier)”的匹配函数
候选者是: Sum::Sum(Expression&) ;, Expression&)
Identifier 类继承自 Expression,那么为什么我会收到此错误?
I have the following in the main:
Sum *sum = new Sum(Identifier("aNum1"), Identifier("aNum2"));
And my classes are:
class Table {
private:
static map<string, int> m;
public:
static int lookup(string ident)
{
return m.find(ident)->second;
}
static void insert(string ident, int aValue)
{
m.insert(pair<string, int>(ident, aValue));
}
};
class Expression {
public:
virtual int const getValue() = 0;
};
class Identifier : Expression {
private:
string ident;
public:
Identifier(string _ident) { ident = _ident; }
int const getValue() { return Table::lookup(ident); }
};
class BinaryExpression : public Expression {
protected:
Expression *firstExp;
Expression *secondExp;
public:
BinaryExpression(Expression &_firstExp, Expression &_secondExp) {
firstExp = &_firstExp;
secondExp = &_secondExp;
}
};
class Sum : BinaryExpression {
public:
Sum(Expression &first, Expression &second) : BinaryExpression (first, second) {}
int const getValue()
{
return firstExp->getValue() + secondExp->getValue();
}
};
When I am compiling it I get the following error:
no matching function for call to 'Sum::Sum(Identifier, Identifier)'
candidates are: Sum::Sum(Expression&, Expression&)
The Identifier class is inheriting from Expression, so why am I getting this error?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
问题是您将临时变量传递给构造函数,但构造函数需要一个非-
const
引用,而临时变量只能绑定到const
引用。要修复此问题,请将参数类型更改为
Expression const&
。顺便说一句,这与继承和多态性完全无关(但也需要 digivampire 的修复;我怀疑这只是一个拼写错误)。The problem is that you are passing temporaries to your constructor but the constructor expects a non-
const
reference, while temporaries can only bind toconst
references.To fix it, change the parameter type to
Expression const&
. This, by the way, is completely unrelated to inheritance and polymorphism (but digivampire’s fix is also needed; I’m suspecting that was just a typo).您正在从表达式私有继承标识符更改为:
You are privately inheriting Identifier from Expression change to:
您需要
Sum(Expression const & first, Expression const & secondary)
。如果没有
const
,它将无法编译 - 非常量引用不会绑定到临时对象,例如临时Identifier
对象。但即便如此,它也不会起作用——你最终会得到指向标识符的悬空指针。您可能会更好地使用以下内容:
但是您需要弄清楚如何在正确的时间释放东西。
You need
Sum(Expression const & first, Expression const & second)
.Without the
const
s it won't compile - non-const references won't bind to a temporary, like your temporaryIdentifier
objects.But even then it won't work - you'll end up with dangling pointers to the Identifiers. You might be better of with something like:
But then you need to work out how to free things at the right time.
据我了解,向下转换(这就是您想要实现的目标)不是自动的,您应该强制它。
但是,要将标识符转换为表达式,您需要指向这些对象的指针。因此,如果您想让 Sum 类构造函数保持原样,您需要像这样调用它:
As I understand it, down-casting (which is what you're trying to achieve) is not automatic and you should force it.
However, to cast Identifier to Expression you need the pointer to those objects. Thus, if you want to keep your Sum class constructor the way it is, you need to call it like this:
继承并不重要。您应该传递表达式的实例而不是标识符。
The inheritance does not matters. You should pass the instances of Expression instead of Identifier.