2

As a follow up to this question: If I create a pure virtual interface class and split the implementing class into a header and source file, I get the following:

Interface:

class IDemo
{
    public:
        virtual ~IDemo() {}
        virtual void OverrideMe() = 0;
};

Implementing class, header:

class Child : public IDemo
{
    public:
       virtual void OverrideMe();
};

Implementing class, source:

void Child::OverrideMe() 
{
    //doStuff
}

So I need to tpye the name of a method declared in the interface 3 times - which is not only cumbersome, but also confusing. Is there a nicer way to do this?

Community
  • 1
  • 1
mort
  • 12,988
  • 14
  • 52
  • 97
  • 7
    No, there isn't. And what's so confusing? – Kiril Kirov Mar 28 '13 at 08:45
  • Because the same method is declared in the interface plus the header file. If someone looks into the header, how does he know that this method declaration is actually inherited from the base class? – mort Mar 28 '13 at 08:49
  • Type twice by merging the 2nd and 3rd together. – Roman R. Mar 28 '13 at 08:50
  • @RomanR.: I don't want to merge the declaration of a class and it's implementation. That was the motivation for using an "interface" in the first place: I didn't want to declare implementation specific members in the "contract" – mort Mar 28 '13 at 08:55
  • 1
    Your compiler might support special syntax for interfaces, letting you to get a bit more distinction and improve readability. E.g., MSVC++ has `__interface` - if you don't mind the portability, you can use it instead of pure virtual classes (read the documentation thoroughly, though) – SomeWittyUsername Mar 28 '13 at 08:58
  • Thanks for the hint, but I'm trying to keep it portable. – mort Mar 28 '13 at 09:01
  • @mort - +1, actually, based on your comment about "If someone looks into the header, how does he know that this method declaration is actually inherited from the base class". You may see my answer for this. – Kiril Kirov Mar 28 '13 at 09:02
  • @KirilKirov: I already did and upvoted it ;) – mort Mar 28 '13 at 09:03

2 Answers2

4

There's no way to avoid this (unless you write the definitions in the header file, as others already suggested; as Botz3000 also suggested - don't do this).

That's the way to do this.


I decided to write an answer, based on your comment:

me:

And what's so confusing

you:

If someone looks into the header, how does he know that this method declaration is actually inherited from the base class

Well, there are two ways - leave a comment, like:

/*
 * This overrides Base::OverrideMe
 */
virtual void OverrideMe();

Most editors will show you this comment, when you put the mouse cursor over it.

The other way to do this is using C++11, by adding override keyword after the function:

class Child : public IDemo
{
    public:
       virtual void OverrideMe() override;
};

This could be useful for other cases, too. You may want to see this part of the wiki article about C++11: override

See also: Is the 'override' keyword just a check for a overridden virtual method?

Community
  • 1
  • 1
Kiril Kirov
  • 37,467
  • 22
  • 115
  • 187
2

No, there's no good way around it.
The interface has to specify the method, because it's part of its contract.
The header has to specify the method, to state that the class overrides it.
The source file has to specify the method so the compiler knows which method the implementation belongs to. I don't find this confusing.

The only way to reduce the occurences to two is to implement the method in the class's header, but i would strongly discourage this.

Botz3000
  • 39,020
  • 8
  • 103
  • 127