Compiler error when overriding virtual methods

Using the VC71 compiler and getting compiler errors that I don't understand. Here is an 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 int&  A::myMethod();

  // error C2555: 'B::myMethod': overriding virtual function return type differs and is not covariant from 'A::myMethod'
  virtual const int& A::myMethod() const;
};

when I switch the order of both method definitions in B, I see another compiler error:

class B: public A
{
public:
  // error C3241: 'const int &B::myMethod(void)' : this method was not introduced by 'A'
  virtual const int& A::myMethod() const;

  // error C2556: 'int &B::myMethod(void)' : overloaded function differs only by return type from 'const int &B::myMethod(void)'
  // error C2373: 'B::myMethod' : redefinition; different type modifiers
  virtual int&  A::myMethod();

  // error C2555: 'B::myMethod': overriding virtual function return type differs and is not covariant from 'A::myMethod'

};

however, if I omit the material A::, then I get no 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 various compiler errors? Any explanation is appreciated!

+3
source share
5 answers
class B: public A
{
public:
  virtual const int& myMethod() const;
  virtual int& myMethod();
};

Delete A::B in the definition and it works well :)

EDIT: missed something in the question ...

:: . , , .

, , , , :

struct A { int getInt(); }

struct B: public A { int getInt(); }

B b;
b.A::getInt(); // calls A::getInt, not B::getInt

, , , :

namespace foo
{
  int bar();     // full name is foo::bar
}

struct Foo
{
  static int bar();     // full name is Foo::bar
};

, :

using foo::bar;

int a = bar();    // calls foo::bar because we asked the compiler
                  // to import it in the current scope

, , :

int B::getInt()                 // definition outside the class
                                // we need to specify what we define
{
  return this->A::getInt() + 1; // call A::getInt, without precising it
                                // we would have a stack overflow
}

, .

+2

A:: , A. , A::

class A{
public:
   int m_val;
};

class B{
public:
   int m_val;
};

class C: public A, public B{}

, m_val C, :

C myC;
myC::A::m_val = 4;

, .

+1

. ++. , , , - .

+1

, - , :

class A
{
public:
  virtual int& myMethod() = 0;
};

class A2
{
public:
  virtual int& myMethod() = 0;
};

class B: public A, public A2
{
public:
  virtual int&  A::myMethod();
  virtual int&  A2::myMethod();
};

. B myMethod(). . :

http://www.cprogramming.com/tutorial/multiple_inheritance.html

+1

, .cpp .h? .cpp, .h( ).

// generates: error C3241: 'const int &B::myMethod(void)' : this method was not introduced by 'A'
virtual int&  A::myMethod();

, , , , . A:: , , myMethod, A. (B C ), , virtual B::ThatFunction(), B imlpementation virtual C::ThatFunction(), C.

A:: A .

0

Source: https://habr.com/ru/post/1740164/


All Articles