Cześć. Mam taki program:
#include <iostream>
using namespace std;
class C
{
public:
virtual int shift(int n = 2) const { return n << 2; }
};
class D
: public C
{
public:
int shift(int n = 1) const { return n << 5; }
};
int main()
{
const D d;
const C *c = &d;
cout << c->shift() << endl;
return 0;
}
Zwraca wartość 64, więc bierze wartość n = 2 z Klasy C i ciało funkcji z klasy D. Zaczyna działać normalnie po usunięciu const ale nie mam pojęcia czemu. Może ktoś mi to wytłumaczyć?
I w takim przykładzi eprogram zwraca "Klasa B 1" (1 jest w Klasie A)
#include <iostream>
using namespace std;
struct A{
virtual void pokaz(int n = 1) {cout << "klasa A " << n;}
};
struct B: public A{
void pokaz(int n = 2) {cout << "klasa B " << n;}
};
int main()
{
B y;
A* x = &y;
x->pokaz();
}
Domyślam się, że ma to cos wspólnego z wczesnym i póxnym wiązaniem ale czy mógłby to ktoś dokładniej wyjaśnić ?