Virtual

This is a discussion on Virtual within the C++ Programming forums, part of the General Programming Boards category; Hi Just have a few questions 1. Virtual functions can be accessed using object pointers only ?? 2. We cannot ...

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    30

    Virtual

    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++

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    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.

  3. #3
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Quote Originally Posted by aldajlo
    1. Virtual functions can be accessed using object pointers only ??
    No, virtual functions can be accessed the same way regular functions can, through pointers, references or actual object instances.

    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.
    Quote Originally Posted by aldajlo
    2. We cannot have virtual constructors but we can have virtual destructors ??
    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.

    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.

  4. #4
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    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.

  5. #5
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    Ok, now I have a question about this...
    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
    Souldn't it be...
    Code:
    //OUTPUT
    A.p();
    B.p();
    A.p();
    C.p();
    D.p();
    D.p();
    ...
    The virtual function pointer isn't being assigned, or it's changed in the casting
    Last edited by xErath; 11-10-2004 at 09:33 PM.

  6. #6
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    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:
    Code:
    int main()
    {
        D d;
        static_cast<C&>(d).p();
        static_cast<C*>(&d)->p();
    }
    They don't change the object, just how it is referred to.

  7. #7
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Virtual Box
    By ssharish2005 in forum Tech Board
    Replies: 3
    Last Post: 02-12-2009, 05:08 AM
  2. Replies: 48
    Last Post: 09-26-2008, 04:45 AM
  3. Program with Shapes using Virtual Functions
    By goron350 in forum C++ Programming
    Replies: 12
    Last Post: 07-17-2005, 02:42 PM
  4. C++ XML Class
    By edwardtisdale in forum C++ Programming
    Replies: 0
    Last Post: 12-10-2001, 11:14 PM
  5. Exporting Object Hierarchies from a DLL
    By andy668 in forum C++ Programming
    Replies: 0
    Last Post: 10-20-2001, 02:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21