Hi
Just have a few questions
1. Virtual functions can be accessed using object pointers only ??
2. We cannot have virtual constructors but we can have virtual destructors ??
Hi
Just have a few questions
1. Virtual functions can be accessed using object pointers only ??
2. We cannot have virtual constructors but we can have virtual destructors ??
Syra
Amateur's urge to master C/C++
1. They can be accessed through pointers and references. They cannot be accessed through an instance of a class itself simply because an instance of the class must be of that particular type, and not a derived type.
2. Virtual destructors are allowed (and often a good idea), but it wouldn't make sense to have virtual constructors (after all, you call the constructor of the type of object you want to create, right). That said, you should be mindful to call the appropriate base class constructor in the constructor of your derived classes (this point is not directly related, but does go along with the theme of inheritance and constructors).
The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.
No, virtual functions can be accessed the same way regular functions can, through pointers, references or actual object instances.Originally Posted by aldajlo
The benefits of a virtual function - basically the fact that you can call a virtual function from a base class pointer or reference and the correct derived class version of that virtual function will be executed - only apply to pointers and references. This makes sense, because the point is to have a base class pointer (or reference) pointing to a derived class object. Only pointers or references can point to object instances in this way.
Correct. It doesn't make sense to have a virtual constructor, because when you are constructing an object you know what type that object is. A virtual function is needed for when you have a base class pointer (or reference) and you don't know what type of derived class it points to. At the time of construction, you know the type of derived class you are constructing.Originally Posted by aldajlo
At the time of destruction, it is possible to delete a base class pointer that points to a derived class. In that case, you want to make sure the appropriate destructors are called, which is why the destructor can (and always should) be virtual when using inheritance polymorphically.
To clarify my first statement to be consistent with jlou's, while you can call a virtual function from an actual instance of the object, it does not make use of the fact that the function is virtual (that is, the instance cannot actually be an instance of the derived type, so it works the same as if it was non-virtual.
That said, jlou's description is probably more coherent anyway.
The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.
Ok, now I have a question about this...
Souldn't it be...Code:#include<iostream> class A{ public: A(){} void p(){std::cout << "A.p();\n";} }; class B: public A{ public: B(){} void p(){std::cout << "B.p();\n";} }; class C{ public: C(){} virtual void p(){std::cout << "C.p();\n";} }; class D: public C{ public: D(){} virtual void p(){std::cout << "D.p();\n";} }; int main(int argc, char* argv[]){ A a; B b; a.p(); b.p(); static_cast<A>(b).p(); C c; D d; c.p(); d.p(); static_cast<C>(d).p(); std::cout<<"\nszA: "<<sizeof(A)<<" szB: "<<sizeof(B); std::cout<<"\nszC: "<<sizeof(C)<<" szD: "<<sizeof(D)<<"\n"; return 0; } //OUTPUT A.p(); B.p(); A.p(); C.p(); D.p(); C.p(); szA: 1 szB: 1 szC: 4 szD: 4
The virtual function pointer isn't being assigned, or it's changed in the castingCode://OUTPUT A.p(); B.p(); A.p(); C.p(); D.p(); D.p(); ...
Last edited by xErath; 11-10-2004 at 09:33 PM.
No... that is the point about virtual functions only working virtually when called from pointers or references. When you use the static cast, it slices the D-ness off of the D object and converts the object to type C. At that point, you are calling p() on a C object, not a D object.
More appropriate casts might be:They don't change the object, just how it is referred to.Code:int main() { D d; static_cast<C&>(d).p(); static_cast<C*>(&d)->p(); }
Ahh!! Thank you...
Arhum... Now I see that with my static_cast I create a new C object. References or pointers refer always to my original D object.
Last edited by xErath; 11-11-2004 at 11:24 AM.