I'm trying to learn c ++, doing some tests, I have the following questions, which, in a way shows one thing and the other works as expected, I will try to explain as best I can, "sorry for my English," I have a base class that is more or less so I think this is the important part;
class Basefptr {
public:
Basefptr();
virtual ~Basefptr();
virtual void funcPtr1();
};
inline void Basefptr::funcPtr1(){
std::cout << "funcPtr1" << std::endl;
}
#endif /* BASEFPTR_H_ */
and in the derived class that other;
#include "Basefptr.h"
class Derivada : public Basefptr {
public:
Derivada();
virtual ~Derivada();
virtual void funcPtr1();
};
inline void Derivada::funcPtr1(){
std::cout << "funcPtr1_de_Derivada" << std::endl;
}
#endif /* DERIVADA_H_ */
that this is the main;
#include "Basefptr.h"
#include "Derivada.h"
#include <vector>
#include <algorithm>
int main(int argc, char* argv[]){
Basefptr* base = new Basefptr;
Derivada* derivada = new Derivada;
std::vector<Basefptr*> poli;
poli.push_back(base);
poli.push_back(derivada);
std::for_each(poli.begin(), poli.end(),[](Basefptr* b){
b->funcPtr1();
});
Basefptr base1;
Derivada derivada1;
std::vector<Basefptr> poli1;
poli1.push_back(base1);
poli1.push_back(derivada1);
std::for_each(poli1.begin(), poli1.end(),[](Basefptr b){
b.funcPtr1();
});
return 0;
}
this the shell out;
funcPtr1
funcPtr1_de_Derivada
funcPtr1
funcPtr1
The first two lines are as expected, but the second is not what I expected, because he believed that would be like the first, can someone explain me, the reason this is so, if you please. or am I doing something wrong greetings and thanks for reading.