5

I want to specifically invoke the base class method; what's the most concise way to do this? For example:

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;
  }
};
Peter Alexander
  • 53,344
  • 14
  • 119
  • 168
sivabudh
  • 31,807
  • 63
  • 162
  • 228

5 Answers5

8

No, that is as concise as it gets. Base::operator!= is the name of the method.

And yes, what you are doing is standard.

However, in your example (unless you have removed some code), you do not need Child::operator!= at all. It does the same thing as Base::operator!= will.

Andrew Stein
  • 12,880
  • 5
  • 35
  • 43
  • @Andrew: thanks for the remark. Though, as you have guessed, I did remove some code just to simplify the question. =) – sivabudh Mar 12 '10 at 17:51
5

1

if ( *((Base*)this) != child_ ) return true;

2

if ( *(static_cast<Base*>(this)) != child_ ) return true;

3

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

if ( getBase() != child_ ) return true;
Alexey Malistov
  • 26,407
  • 13
  • 68
  • 88
  • Careful! This only works because you are calling non-virtual functions! –  Mar 12 '10 at 19:57
4

What you're doing is the most concise and "standard" way to do it, but some people prefer this:

class SomeBase
{
public:
    bool operator!=(const SomeBaseClass& other);
};

class SomeObject: public SomeBase
{
    typedef SomeBase base;  // Define alias for base class

public:
    bool operator!=(const SomeObject &other)
    {
        // Use alias
        if (base::operator!=(other))
            return true;

        // ...

        return false;
    }
};

The benefits of this method is that it clarifies intention, it gives you a standard abbreviation for what could be a long base-class name, and if your base class changes, you don't have to change every use of the base.

See Using "super" in C++ for additional discussion.

(Personally, I don't care for this, and I don't recommend it, but I think it is one valid answer to the question.)

Community
  • 1
  • 1
Kristopher Johnson
  • 81,409
  • 55
  • 245
  • 302
  • +1 for mentioning this: "... and if your base class changes, you don't have to change every use of the base." Thanks! – sivabudh Mar 12 '10 at 20:15
0
if (condition) return true;
return false;

can be abbreviated to

return condition;
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
-2

I'd get rid of the if/then control structure and just return the return value of the base class operator, but otherwise what you're doing is fine.

It can be a bit more concise, though: return ((Base&)*this) != child_;

Dathan
  • 7,266
  • 3
  • 27
  • 46
  • If he's going to do that, he may as well just get rid of the function altogether, and let the base class be called. I think it's clear that the "Some other comparisons" section should be filled in with code. – Kristopher Johnson Mar 12 '10 at 18:06