¿Cómo llamo a una función sobrecargada desde una función en la clase base?

I have a class (B) that inherits another class (A). I want to call a function from class A that has been overridden. I also want to be able to call the overridden function independent of what class inherited the base (say class C : public A , where I want to call C's version of the function.)

Aquí un ejemplo

class A {

public:
    void callF();
    virtual void f() {};
};

class B : public A {

public:
    void f();
};

void A::callF()
{
    //FYI, I want to be able to call this without knowing what the super class is.
    f();
}

void B::f()
{
    std::cout << "I want this function to be called, but instead the default f() is called.";
}

Edit: In my real code, I have an std::vector<A> aVector;. Then I would call aVector.push_back(B());. Si llamara aVector[0].callF();, The default a::f() would be called. As answered below, I have a problem with slicing.

preguntado el 10 de marzo de 12 a las 02:03

Please clarify your question a bit. You want to be able to call the overridden function f() when it is called from the base-class. In the example above, when you call f() in the class A, you want the overridden function in B to be called. Is that so? -

That is right. Also, say I implemented class C which inherits A. When I call C c; c.callF(), I would want it to call C's version of f(). -

The code you have given us does exactly what you ask for. If your program is behaving differently, the problem may lie in what you are no showing us. Please create a small, completar program that demonstrates the problem and paste that program into your question. sscce.org -

Where is your virtual inheritance? -

3 Respuestas

Your construction:

vector_of_A.push_back( B() );

doesn't store a B in the vector. It constructs a B, then constructs an A from that, then stores que A in the vector. As a consequence, you experience slicing.

Vea esto para más información:

https://stackoverflow.com/a/4403759/8747

contestado el 23 de mayo de 17 a las 13:05

Tu código es correcto.

You may be getting the behavior you observed because your were calling f() or callF() from the constructor of A. That's the only case I can think of where A::f() would get invoked instead of B::f().

respondido 10 mar '12, 02:03

That's a good point. I may have been calling it from the constructor, or I might be having problems calling it from a std::vector<A>. I'm calling it outside of the constructor, but It still calls A::f(). - sFuller

Your code works for me with this little test program:

int main()
{
    // Instantiate B, and reference it via a base class pointer.
    A* b = new B;

    b->callF();
    delete b;
}

Salida:

I want this function to be called, but instead the default f() is called.

When calling a virtual member function within a base class member function, it's the derived member function that will be invoked.

respondido 10 mar '12, 02:03

Okay. I'm probably having trouble with something else. In my real code, I have a std::vector<A> aVector;. Then I would call aVector.push_back(B());. Si llamara aVector[0].callF();, The default a::f() would be called. I should probably ask another question now.. Thanks for answering! - sFuller

@sFuller Please read sscce.org, particularly the part that says, "Make sure the code you post displays the problem!" - Robᵩ

Ah, your problem is a common one and is called rebanar. I'm sure the other answerers will explain your problem faster than I can, as I don't have their fast ninja skills. Please edit your question and paste the comment you just posted here in my answer. - Emilio Cormier

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.