mercredi 4 mars 2015

Calling subclass' method in inherited virtual function?

:I am new to C++, but I was under the impression that virtual in C++ was the equivalent of abstract in Java. I have the following:



//A.h
class A {
public:
void method();
protected:
virtual void helper();
}


With the following cpp:



//A.cpp
#include "A.h"
void A::methodA() {
//do stuff
helper();
}


Then here's the derived class:



//B.h
#include "A.h"
class B: public A{
private:
void helper2();
}


and the following derived cpp:



//B.cpp
#include "B.h"

void B::helper2() {
//do some stuff
}

void A::helper() {
helper2();
}


However, the compiler does not seem to like that I am calling a helper2 method defined in the derived class, within a virtual method defined in the super class. It gives the error "error: ‘helper2’ was not declared in this scope". Is this not how I am supposed to use virtual methods?

Also, I don't know if this is relevant, but I am use gcc 4.6 so I can't use the keyword override.


Aucun commentaire:

Enregistrer un commentaire