Hi guys
I've been programming a while now at school, and I'm working on my first independent large project. I've been discovering a lot of things about programming that I haven't known before and it's been great.
However, more and more, I feel like I no longer understand C++ as a language the more I delve into it. I'd like to get some of my (mis) conceptions about references and pointers straight before continuing, and I hope that you, stackoverflow, could correct me if I'm wrong.
Thanks in advance, and here we go!
1. When converting between classes, what actually gets converted is the virtual table.
Example:
class A{
public:
A() : x(0) {};
int x;
virtual void doStuff() {
cout << x <<endl;
}
};
class B : public A{
public:
B() : y(1) {};
int y;
virtual void doStuff() {
cout << y <<endl;
}
};
If I had converted an object b of type B to A, what would happen internally is the virtual table of b would be discarded, and replaced with a corresponding virtual table of type A, and the destructor of y would be called because there is no longer a reference to it. Similarly, the doStuff in b would be made to point to the function address of A::doStuff instead of B::doStuff. The address pointing to x would stay the same however.
2. Which implies that the only way to take advantage of polymorphism is through pointers and references
As a consequence of point 1, the only way to take advantage of polymorphism in virtual methods of classes would be to use references and pointers, because if we passed by value, the classes itself would be automatically converted into the base class.
Example:
void doALotOfStuff(A a1, A a2) {
a1.doStuff();
a2.doStuff();
}
int main(){
A a;
B b;
doALotOfStuff(a,b);
return 0;
}
would print out
0
0
because the compiler would generate code to convert b into A.
3. Furthermore, the only way to take advantage of this kind of polymorphism with arrays and STL containers would be to use pointers because references cannot be stored
Since vector would not work because references are not assignable, it follows that if I wanted to have a vector of base classes, I would need to create a vector of pointers of A in order to preserve the virtual table of elements of Type B.
Sorry if this was TL;DR, but it's be kind of bothering me for a while when I was doing some class design on my project, and I realized I couldn't really get away from just using either pointers or references because of library interfaces and polymorphism issues.
Thanks for any clarification you can provide!