C++继承-子类构造函数调用?

发布于 2024-12-13 02:04:46 字数 1387 浏览 5 评论 0原文

我的主要内容如下:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

姐不稀罕 2024-12-20 02:04:46

问题是您将临时变量传递给构造函数,但构造函数需要一个-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 to const 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).

偷得浮生 2024-12-20 02:04:46
class Identifier : Expression {
private:
    string ident;
public:
    Identifier(string _ident) { ident = _ident; }
    int const getValue() { return Table::lookup(ident); }    
};

您正在从表达式私有继承标识符更改为:

class Identifier : public Expression {
private:
    string ident;
public:
    Identifier(string _ident) { ident = _ident; }
    int const getValue() { return Table::lookup(ident); }    
};
class Identifier : Expression {
private:
    string ident;
public:
    Identifier(string _ident) { ident = _ident; }
    int const getValue() { return Table::lookup(ident); }    
};

You are privately inheriting Identifier from Expression change to:

class Identifier : public Expression {
private:
    string ident;
public:
    Identifier(string _ident) { ident = _ident; }
    int const getValue() { return Table::lookup(ident); }    
};
她比我温柔 2024-12-20 02:04:46

您需要 Sum(Expression const & first, Expression const & secondary)

如果没有 const ,它将无法编译 - 非常量引用不会绑定到临时对象,例如临时 Identifier 对象。

但即便如此,它也不会起作用——你最终会得到指向标识符的悬空指针。您可能会更好地使用以下内容:

new Sum(new Identifier("aNum1"), new Identifier("aNum2"));

但是您需要弄清楚如何在正确的时间释放东西。

You need Sum(Expression const & first, Expression const & second).

Without the consts it won't compile - non-const references won't bind to a temporary, like your temporary Identifier 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:

new Sum(new Identifier("aNum1"), new Identifier("aNum2"));

But then you need to work out how to free things at the right time.

喜爱纠缠 2024-12-20 02:04:46

据我了解,向下转换(这就是您想要实现的目标)不是自动的,您应该强制它。

但是,要将标识符转换为表达式,您需要指向这些对象的指针。因此,如果您想让 Sum 类构造函数保持原样,您需要像这样调用它:

Identifier a("aNum1");
Identifier b("aNum2");
Sum *sum = new Sum(*(Expression*) &a, *(Expression*) &b);

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:

Identifier a("aNum1");
Identifier b("aNum2");
Sum *sum = new Sum(*(Expression*) &a, *(Expression*) &b);
二智少女猫性小仙女 2024-12-20 02:04:46

继承并不重要。您应该传递表达式的实例而不是标识符。

Sum *sum = new Sum(Expression("aNum1"), Expression("aNum2"));

The inheritance does not matters. You should pass the instances of Expression instead of Identifier.

Sum *sum = new Sum(Expression("aNum1"), Expression("aNum2"));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文