C ++ inheritance, hidden base methods

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

// 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 now I 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 methods of the base class are hiding. I got the impression that since the methods of the base class are virtual, should they be visible even when viewed using the Derived pointer, or am I mistaken?

+5
source share
2 answers

++. , , , . . .

, Derived Base

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

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

, using

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

+11

.

, , , :

class Base { 
public:
    void setData(float d);
    void setData(float d, float e);
};

class Derived : public Base { 

    using Base::setData;

    void SetData(float d);
};

[:] , . - Derived::setData ( ), , setData(float) , Derived::setData(float). , Derived::setData(float);.

+3

All Articles