Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ method only visible when object cast to base class?

Tags:

c++

It must be something specific in my code, which I can't post. But maybe someone can suggest possible causes.

Basically I have:

class CParent
{
 public:
  void doIt(int x);
};
class CChild : public CParent
{
 public:
  void doIt(int x,int y,int z);
};

CChild *pChild = ...
pChild->doIt(123); //FAILS compiler, no method found
CParent *pParent = pChild;
pParent->doIt(123); //works fine

How on earth?

EDIT: people are talking about shadowing/hiding. But the two versions of doIt have different numbers of parameters. Surely that can't confuse the compiler, overloads in child class which can't possibly be confused with the parent class version? Can it?

The compiler error I get is: error C2660: 'CChild::doIt' : function does not take 1 argument

like image 407
Mr. Boy Avatar asked Jan 14 '10 22:01

Mr. Boy


People also ask

How do you hide base class method and function?

If you do not use override keyword, then the compiler will not override the method. Instead of the overriding compiler will hide the method. If you do not use the new keyword, then the compiler will automatically hide the method of the base class.

Can base class access methods of derived class?

base (C# Reference)The base keyword is used to access members of the base class from within a derived class: Call a method on the base class that has been overridden by another method. Specify which base-class constructor should be called when creating instances of the derived class.

Can base class access derived class private?

Private members of the base class cannot be used by the derived class unless friend declarations within the base class explicitly grant access to them.

How do you access base class members in a derived class?

A base class's private members are never accessible directly from a derived class, but can be accessed through calls to the public and protected members of the base class.


2 Answers

You have shadowed a method. For example:

struct base
{
    void method(int);
    void method(float);
};

struct derived : base
{
    void method(int);
    // base::method(int) is not visible.
    // base::method(float) is not visible.
};

You can fix this with a using directive:

class derived : public base
{
    using base::method; // bring all of them in.

    void method(int);
    // base::method(int) is not visible.
    // base::method(float) is visible.
};

Since you seem insistent about the number of parameters, I'll address that. That doesn't change anything. Observe:

struct base
{
    void method(int){}
};

struct derived : base
{
    void method(int,int){}
    // method(int) is not visible.
};

struct derived_fixed : base
{
    using base::method;
    void method(int,int){}
};

int main(void)
{
    {
        derived d;

        d.method(1, 2); // will compile
        d.method(3); // will NOT compile
    }
    {
        derived_fixed d;

        d.method(1, 2); // will compile
        d.method(3); // will compile
    }
}

It will still be shadowed regardless of parameters or return types; it's simply the name that shadows. using base::<x>; will bring all of base's "<x>" methods into visibility.

like image 62
GManNickG Avatar answered Oct 06 '22 23:10

GManNickG


You are hitting a classic problem. You need using CParent::doIt; in your CChild class. I'll scrounge up the duplicate questions.

Edit:

Here's my answer to essentially the same question: Overriding a Base's Overloaded Function in C++

like image 29
Fred Larson Avatar answered Oct 06 '22 23:10

Fred Larson