See the following code (for example):
class A {
int n;
public:
int f() const { return n; }
void set(int i) { n = i; }
};
class B : public A {
public:
int g() const { return f()+1; }
};
void h(const A& a) {
a.f();
}
int main() {
B b;
A& a = b;
A* ptrA = new B;
h(b);
delete ptrA;
return 0;
}
Now look at these lines:
A & a = b; // Is there a "cut"? (Why?)
A * ptrA = new B; // Is there a "cut"? (Why?)
A a = b; // Is there a "cut"? (why?)
I really don't understand when I want to use any of them, and when, as an alternative, you will not be allowed to use one of them. What is the difference between these lines.
source
share