Issue
I'm having trouble understanding how slicing occurs? for example, in this piece of code:
class A {
public:
virtual h() {
cout << "type A" << endl;
}
};
class B : public A {
public:
virtual h() override {
cout << "type B" << endl;
}
};
void f(A a) {
a.h();
}
void g(A& a) {
a.h();
}
int main() {
A a1 = B(); // a1 is A and doesn't recognize any B properties
A *a = new B();
f(*a);
g(*a);
}
I noticed that:
variable a1 doens't know it's a B, but variable a does know. I refer this is happening because in variable a1 the assignment to B is by value, in contrst to variable a, where I create a pointer to B.
same thing happens when I pass variable a to different functions - when I pass by value, it thinks it's an A, but when i pass by reference, it thinks it's B.
I would be happy if anyone could give me more extensive and deeper explanation. Thank you in advance!
Solution
In this statement
A a1 = B();
there is used the default copy constructor of the class A because the object a1
has the type A.
This constructor looks like
constexpr A( const A & );
So only subobject of the type A
of the object of the class B
is copied to the object a1
. The table of pointers to virtual functions of the object a1
will contain pointer to its own virtual function.
In this declaration
A *a = new B();
the pointer a
points to an object of the type B
that contains the table of pointers to virtual functions that in turn contains a pointer to the virtual function of the class B.
So in this call
g(*a);
the reference to the object of the dynamic type B
is passed (the object itself was not changed when it was passed to the function by reference).
So within the function
void g(A& a) {
a.h();
}.
there will be access to the table of virtual function pointers that contains a pointer to the virtual function of the class B. Here a new object of the class A
is not created because the original object is passed by reference.
In this call
f(*a);
the argument of the function is passed by value. That is there is a copy initialization of the parameter of the function declared like
void f(A a) {
a.h();
}
So it is in fact the same case as in the declaration
A a1 = B();
considered above.
Answered By - Vlad from Moscow Answer Checked By - Marilyn (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.