用于调用基类方法的简洁(但仍具有表现力)的C++语法

siv*_*udh 4 c++ syntax inheritance base-class

我想专门调用基类方法; 什么是最简洁的方法呢?例如:

class Base
{
public:
  bool operator != (Base&);
};

class Child : public Base
{
public:
  bool operator != (Child& child_)
  {
    if(Base::operator!=(child_))  // Is there a more concise syntax than this?
      return true;

    // ... Some other comparisons that Base does not know about ...

    return false;
  }
};
Run Code Online (Sandbox Code Playgroud)

And*_*ein 8

不,这是简洁的.Base::operator!=是方法的名称.

是的,你所做的是标准的.

但是,在您的示例中(除非您删除了一些代码),您根本不需要Child::operator!=.它会做同样的事情Base::operator!=.


Ale*_*tov 5

1

if ( *((Base*)this) != child_ ) return true;
Run Code Online (Sandbox Code Playgroud)

2

if ( *(static_cast<Base*>(this)) != child_ ) return true;
Run Code Online (Sandbox Code Playgroud)

3

class Base  
{  
public:  
  bool operator != (Base&);  
  Base       & getBase()       { return *this;}
  Base const & getBase() const { return *this;}
}; 

if ( getBase() != child_ ) return true;
Run Code Online (Sandbox Code Playgroud)