given the following code:
#include <iostream>
using std::cout;
using std::endl;
class classA {
virtual void virtualMethod() {
cout << "classA" << endl;
}
public:
virtual ~classA() {
virtualMethod();
}
void mA() {
virtualMethod();
}
};
class classB: public classA {
void virtualMethod() override {
cout << "classB" << endl;
}
public:
~classB() override {
virtualMethod();
}
void mB() {
virtualMethod();
}
};
int main(void) {
classA* obj = new classB;
obj->mA();
obj->mB(); // *** why I really get here error?
delete obj;
return 0;
}
I will be happy to understand what happens in any line that exists in the main, and in addition what is happens while obj deleted in the end running of the main function. I don't want to know just the answer: "the output is ...." , but, to understand why it's so.
My problem is mainly to understand the case that we at mA method. To where we are going from there? It seems that we should go to classA::virtualMethod because that classB::virtualMethod is a private method at classB and hence, we don't meet it from classA (but while I running the code, I find that it's not correct, and I do not understand why).
In addition, I will be happy to understand why I get error in the line obj->mB(); , according to the compiler, the reason is:
'class classA' has no member named 'mB'
But why it's not going according to classB (After all, we did new classB)..
Aucun commentaire:
Enregistrer un commentaire