class Base{
public:
virtual void func2() const{
cout << "1" << endl;
}
virtual void func1(Base temp){
cout << "Class 1" << endl;
temp.func2();
}
};
class Derived : public Base{
public:
void func2() const{
cout << "2" << endl;
}
void func1(Base temp){
cout << "Class 2" << endl;
temp.func2();
}
};
int main(){
Base one;
Derived two;
two.func1(two);
cout << "------------\n";
return 0;
}
Output is:
Class 2
1
So my question is: why when I'm giving func1(two) an object of Derived, it is not calling on virtual func2 that outputs 2. The compiler is always choosing func2() that is defined in the Base class.
It does, however, chooses func2 in the Derived class when you call on it, i.e. two.func2(); which outputs 2