I am a beginner in C++, and I want to change the function version which will be called in this code:
#include <iostream>
class C1 {};
class C2 : public C1 {};
void func(C1 var) {
std::cout << "Func 1" << std::endl;
}
void func(C2 var) {
std::cout << "Func 2" << std::endl;
}
int main() {
C1 *teste1 = new C1;
C1 *teste2 = new C2;
func(*teste1); // Want "Func 1"
func(*teste2); // Want "Func 2"
return 0;
}
As it can be seen in the comment, what I want is that the func with an C2 argument be called when I dereference the pointer which points to a C2 class.
Edit: Just to clarify what I really want to achieve, the following code is closer to what I want:
#include <iostream>
#include <list>
class C1 {};
class C2 : public C1 {};
void func(C1 var) {
std::cout << "Func 1" << std::endl;
}
void func(C2 var) {
std::cout << "Func 2" << std::endl;
}
int main() {
std::list<C1*> teste;
teste.push_back(new C1);
teste.push_back(new C2);
// Want to print:
// Func 1
// Func 2
for(auto i: teste) {
func(*i);
}
return 0;
}