Compiler error when overwriting virtual methods
Posted
by Stefan Hubert
on Stack Overflow
See other posts from Stack Overflow
or by Stefan Hubert
Published on 2010-04-07T13:50:51Z
Indexed on
2010/04/07
13:53 UTC
Read the original article
Hit count: 392
Using VC71 compiler and get compiler errors, that i don't understand. Here comes the example
class A
{
public:
virtual int& myMethod() = 0;
virtual const int& myMethod()const = 0;
};
class B: public A
{
public:
// generates: error C3241: 'const int &B::myMethod(void)' : this method was not introduced by 'A' virtual const int& A::myMethod() const;
virtual int& A::myMethod();
};
when i switch order of both method definitions in B then I see a different compiler error:
class B: public A
{
public:
virtual const int& A::myMethod() const;
// error C2556: 'const int &B::myMethod(void)' : overloaded function differs only by return type from 'int &B::myMethod(void)'
// error C2373: 'B::myMethod' : redefinition; different type modifiers
virtual int& A::myMethod();
};
however, if I omit the A::
stuff then i don't get any compiler error:
class B: public A
{
public:
virtual int& myMethod();
virtual const int& myMethod() const;
};
So, what exactly does A::
in front of my method names and why do i see these diverse compiler errors? Any explanation welcome!
© Stack Overflow or respective owner