After the end of my first c++ course , I have some questions that I'd like to discuss with you guys :)
1.Why would we ever want to upcast (or use upcasting) ? after having a few homework regarding the matter of polymorphism , I do find it a very strong mechanism for allowing an object to behave differently , but still , why would I want to use the upcasting mechanism ?
2.When would we use private inheritance and when would choose composition ? private inheritance seals , in a way, the chain of inheritance , so the grandson won't be able to access any fields of the father , after
we do :
class Father : private GrandFather {...}
3.When would we prefer inheritance over composition ? I think that if we ever would want to upcast , we must use inheritance (so ask ourselves the question "would we ever want to upcast" ) ?
4.What is the use of putting the copy-constructor in the private ? I think it comes handy when we do something like
a shallow copy , so if we have an object that has a dynamic allocation
1 2
|
class A{public: char * myStringChar;}
A a; A b = a;
| |
so here "a" and "b" would point to the same address in memory , and if we didn't write a proper cctor (i.e with
the use of "new") then both of them would point the same address .
5.Why cctor (copy constructor) cannot be virtual ?
is it because of the same reason that the Ctor can't be virtual ? because while the object's creation its (the object's) ctor is called , and its Vtable is not done yet , therefore the ctor can't be in the Vtable (would be ready after the ctor is done creating the object...)
6.Why a destructor cannot be overloaded ? of course it can be virtual (for inheritance use and preventing memory leaks) , but what might be the reason that the language doesn't support overloaded destructors ?
Regards,Davi