Question

Why is this not compiling?
error C2660: 'Concrete::WriteLine' : function does not take 1 arguments
I know if i add the Line:
//using AbstractBase::WriteLine;
it works, but i dont understand why.

#include "iostream" 

class AbstractBase
{
public:
   virtual void WriteLine() = 0;
   virtual void WriteLine( int i )
   {
      std::cout<<"AbstractBase"<<std::endl;
   }
};

class Concrete : public AbstractBase
{
public:
   //using AbstractBase::WriteLine;
   virtual void WriteLine()
   {
      std::cout<<"Concrete Sub Class"<<std::endl;
   }
};

int main()
{
   Concrete ff;
   ff.WriteLine();
   ff.WriteLine(1);
   return 0;
}

Can someone explain me what happens here. Thanx


Does anyone knows if this behavior is a defined behavior from the C++ standart. Is it mentioned in the C++ standart? Or is it just a kind of compiler behavior?

Was it helpful?

Solution

Once you declare a function:

void WriteLine()

in derived class it hides all the base class functions with the same name.
The above function which takes no parameters hides the function with same name and taking one parameter, Since compiler cannot find a function with one parameter it reports a error.

The line:

using AbstractBase::WriteLine;

enables(brings in scope) all the hidden names from the Base class in your derived class and hence the function taking one parameter is available.

Good Read:
What's the meaning of, Warning: Derived::f(char) hides Base::f(double)?

OTHER TIPS

Method hiding happens. The other function in the base class is hidden by the function with the same name, although it has a different signature.

To solve this, you can use a using directive:

using AbstractBase::WriteLine;

in the derived class:

class Concrete : public AbstractBase
{
public:
   using AbstractBase::WriteLine;
   //using AbstractBase::WriteLine;
   virtual void WriteLine()
   {
      std::cout<<"Concrete Sub Class"<<std::endl;
   }
};

Declaring a function in a derived class hides all functions with the same name in the base class - they are not considered as potential overloads of the derived class functions.

Adding the using declaration brings the hidden functions back into the scope of the derived class, and they are considered as potential overloads along with the derived class functions.

The problem is that Concrete::WriteLine is hiding all AbstractBase::WriteLines. You can add

using AbstractBase::WriteLine;

to your derived class.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top