Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ inheritance, base methods hidden

I have a simple C++ base class, derived class example.

// Base.hpp 
#pragma once

class Base
{
public:
    virtual float getData();
    virtual void setData(float a, float b);
    virtual void setData(float d);

protected:
    float data;
};

//Base.cpp
#include "stdafx.h"
#include "Base.hpp"

float Base::getData()
{ 
    return data; 
}

void Base::setData(float a, float b)
{ 
    setData(a);
}

void Base::setData(float d)
{ 
    data = d;
}

//Derived.hpp
#pragma once
#include "Base.hpp"

class Derived
    : public Base
{
public:
    virtual void setData(float d);
};

//Derived.cpp
#include "stdafx.h"
#include "Derived.hpp"

void Derived::setData(float d)
{
    data = d + 10.0f;
}

If I now make a pointer to the Base this compiles fine.

//Main.cpp
#include "stdafx.h"
#include "Base.hpp"
#include "Derived.hpp"

Base *obj = new Derived();

But if I make a pointer to the Derived class, then the compiler (VC 2008 and 2010) complains that:

Main.cpp(12): error C2660: 'Derived::setData' : function does not take 2 arguments

And here is the code that causes this error:

//Main.cpp
#include "stdafx.h"
#include "Base.hpp"
#include "Derived.hpp"

Derived *obj = new Derived();

It seems that the base class methods are being hidden. I was under the impression that since the base class methods are virtual they should be visible even when viewed from the Derived pointer, or am I wrong?

like image 502
BefittingTheorem Avatar asked Sep 29 '10 15:09

BefittingTheorem


People also ask

How do you hide base class methods?

In method hiding, you can hide the implementation of the methods of a base class from the derived class using the new keyword. Or in other words, in method hiding, you can redefine the method of the base class in the derived class by using the new keyword.

Can the private members of the base class be inherited?

The derived class doesn't "inherit" the private members of the base class in any way - it can't access them, so it doesn't "inherit" them.

How do you inherit base class as protected?

public inheritance makes public members of the base class public in the derived class, and the protected members of the base class remain protected in the derived class. protected inheritance makes the public and protected members of the base class protected in the derived class.

When the inheritance is private the private methods in base class are in C ++)?

Explanation: When the inheritance is private, the private methods in base class are inaccessible in the derived class (in C++). 2.


1 Answers

This is an artifact of C++ name lookup. The basic algorithm is the compiler will start at the type of the current value and proceed up the hierarchy until it finds a member on the type which has the target name. It will then do overload resolution on only the members of that type with the given name. It does not consider members of the same name on parent types.

The way to work around this is to redefine the functions on Derived and just forward them up to Base

class Derived {
  ...
  void setData(float a, float b);
}

void Derived::setData(float a, float b) {
  Base::setData(a,b);
}

Additionally you can bring the base members into scope using the using declaration

class Derived {
  using Base::setData;
  ...
}

Documentation on this use of using

  • http://publib.boulder.ibm.com/infocenter/comphelp/v101v121/index.jsp?topic=/com.ibm.xlcpp101.aix.doc/language_ref/using_declaration_class_members.html
like image 55
JaredPar Avatar answered Sep 29 '22 21:09

JaredPar