【问题标题】:In C++ can a derived class (with more parameters than it's Base class) use base class functions and overloaded operators?在 C++ 中,派生类(参数比基类多)可以使用基类函数和重载运算符吗?
【发布时间】:2021-07-02 05:01:05
【问题描述】:

我是 C++ 类的新手,所以这个问题对某些人来说可能看起来很愚蠢,但我想知道我们是否可以在派生类中使用基类函数,就像我们使用构造函数一样?

据我所知,基类的构造函数可以在派生类中使用,而无需一次又一次地复制相同的代码行,只需稍微修改构造函数以适应派生类的附加参数。 例如:

class parent{

protected:

    char *p;

public:

    parent()
    {
        p=NULL;
    }

    parent(char ch[10])
    {
       /// initialization for p with ch (p=ch);
    }

    /// other functions and destructor ..
};

class child: public parent{

protected:
    char *c;

public:
    child():parent()
    {
        c=NULL;
    }
    child(char ch1[10], char ch2[10]): parent(ch1)
    {
       /// initialization for c with ch2 (c=ch2);
    }
};

我尝试做类似的事情,但没有使用构造函数,使用父类函数和重载运算符,但它不起作用(也许我做错了什么):

class parent{
    
    protected:
    
        char *p;
    
    public:
    
        parent()
        {
            p=NULL;
        }
    
        parent(char ch[10])
        {
           /// initialization for p with ch (p=ch);
        }
        parent &operator=(const parent &p1)
        {
          /// overloading the operator for parent class parameters;
        }
    
        /// other functions and destructor ..
    };
    
    class child: public parent{
    
    protected:
        char *c;
    
    public:
        child():parent()
        {
            c=NULL;
        }
        child(char ch1[10], char ch2[10]): parent(ch1)
        {
           /// initialization for c with ch2 (c=ch2);
        }
        child &operator=(const parent &p1, const child &p2): parent &operator=(p1)
{ 
    ////didn't work
}
    };

这就是我问这个问题的原因。

我主要想知道这是否可能,因为它对运算符重载有帮助(通常我需要复制粘贴用于重载运算符的代码,从基类到派生类,然后再添加几行,具体取决于关于派生类中有多少参数。但是一遍又一遍地编写或复制粘贴相同的代码行非常烦人。

那么有解决办法吗?

【问题讨论】:

  • “我试图做类似的事情,但不是用构造函数,用父类函数,但它没有工作” 那么你能告诉我们这个尝试吗?除非您分享相关代码,否则我们无法帮助确定问题。
  • 请比“没用”更准确。什么没用?如果您遇到编译器错误,请告诉我们。
  • 我编辑了代码; error1: 'only constructor take members initializer' 和 error2: 'abonament_premium& abonament_premium::operator=(const abonament&, const abonament_premium&)' 必须只采用一个参数|
  • 初始化列表只能用在构造函数的定义中,因为(嗯)构造函数的目的是初始化一个对象及其组成部分(即基础和成员)。其他函数必须在其主体中显式调用继承的版本。而且您无法更改复制赋值 operator=() 的基本形式 - 它始终是一个接受单个参数的非静态成员函数。

标签: c++ class inheritance constructor


【解决方案1】:

两个问题。

赋值运算符总是必须只接受一个参数。如果你仔细想想,两个参数是没有意义的,因为= 符号的右侧总是只有一个东西:

child1 = child2; // child2 is passed into operator= for child1

由于const parent& 尤其是const child&,因此这个问题很容易解决:获取const child& 类型的单个参数并将其用作您想要的parentchild初始化自。

第二个问题是使用特殊的冒号语法,它只适用于构造函数。如果要调用父级的赋值运算符,则需要使用不同的语法。

child &operator=(const child &p)
{
    parent::operator=(p);
    c = p.c;
}

一般来说,调用一个基类成员,需要给出具体的基类名称(因为一个类可以有多个基类,都可能定义同一个成员函数),后跟两个冒号:: , 后跟函数名。

(顺便说一句,将数组作为参数,然后在类中存储原始指针可能是个坏主意,因为数组将超出范围,指针将不再引用任何东西。最好是使用std::string 进行存储。)

【讨论】:

  • “以数组为参数”。这是不可能的,有衰减,所以parent(char ch[10])parent(char* ch) 相同。显示的代码中没有悬空指针。 (尽管使用std::string 的建议仍然相关:-))。
  • 这对你的回答来说很多,这就是我要找的!没想到会这么容易。虽然我认为你错过了 operator= 中的“return *this”。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-20
  • 2011-09-05
  • 2010-10-17
  • 1970-01-01
  • 1970-01-01
  • 2021-02-14
相关资源
最近更新 更多