5

I've used VisualAssistX Implement Virtual Methods option and it generated:

class Base: public IBase
{
public:
    Base(void);
    ~Base(void);
    virtual void IBase::Foo();

I've noticed that I can omit IBase and program still compiles like that:

    virtual void Foo();

If this is the same code? Why VisualAssistX inserts IBase::? Is it just kind of "code-style" to improve readability?

Thanks

Oleg Vazhnev
  • 23,239
  • 54
  • 171
  • 305
  • 3
    I think inserting the namespace/class name *inside* a class declaration **decreases** readability. The bare function name is just fine. –  Mar 13 '13 at 18:34
  • 3
    Is this even valid C++? – NPE Mar 13 '13 at 18:35
  • @RichardJ.RossIII: I can't get this to compile using `g++ 4.7.2`: `error: cannot declare member function 'B1::foo' within 'D'` – NPE Mar 13 '13 at 18:37
  • @RichardJ.RossIII Good catch, although I would seriously punish Stroustrup for having added multiple inheritance support to C++. –  Mar 13 '13 at 18:37
  • 1
    @RichardJ.RossIII: http://stackoverflow.com/questions/7925792/distinct-implementations-for-pure-virtual-functions-with-same-name – NPE Mar 13 '13 at 18:39
  • @NPE it isn't *standard* C++, but it is MSVC C++, which, as we all know, doesn't conform to any standards but it's own. – Richard J. Ross III Mar 13 '13 at 18:46

3 Answers3

2

This would help resolve the ambiguity if you were to derive from multiple base classes with clashing virtual functions. I suspect this is the reason why VisualAssistX is choosing to insert the IBase::.

Whether IBase:: helps improve readability is debatable. I personally find it distracting.

Furthermore, the syntax is not even standard C++. See Distinct implementations for pure virtual functions with same name for a discussion and for a suggestion how the multiple inheritance problem can be solved using standard, portable C++.

Community
  • 1
  • 1
NPE
  • 486,780
  • 108
  • 951
  • 1,012
1

Definitely not improving readability, the other way around. The scope prefix is for use from outside or when there can be resolution ambiguity, using the prefix inside the scope itself just obfuscates the code and actually contradicts OOP principles. When inside scope your entity doesn't need to indicate it again, it's completely redundant. Regarding the VA I can only speculate that it was simpler for implementation.

SomeWittyUsername
  • 18,025
  • 3
  • 42
  • 85
1

The difference is that the first is not valid C++.

Perhaps your compiler accepts it (since that compiler has many strange "extensions" to the language); but if you need your code to be portable to more standard compilers, then you will need to use the second form.

Why VisualAssistX inserts IBase::?

I would guess that it's a misguided attempt to indicate that the function overrides the one declared in IBase; but I've no idea why anyone thought that might be a good idea.

Is it just kind of "code-style" to improve readability?

On the contrary; this bogus declaration makes the function look like a member of IBase, when it's actually a member of Base. That kind of confusion is going to reduce readability.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644