Advertisement

Virtual Destructors take up space?

Started by November 13, 2000 03:45 AM
2 comments, last by Jx 24 years, 1 month ago
Not a very descriptive title i know so here is a better description of my problem.... I have a vector class and a matrix class, like so:
          
class Vector
{
   float x;
   float y;
   float z;

   Vector();
   virtual ~Vector();

};


class Matrix
{

   union
   {
      struct
         {
            Vector r1;
            Vector r2;
            Vector r3;
            Vector r4;
         };

      float m4x4[4][4]
      float m16[16];

   };

   Matrix();
   ~Matrix();
};
        
Obviously, these are cut down versions of the classes themselves, but they illustrate my point. So - my problem? Well, if i do this: r1.x = 1.0; then m4x4[0][0] doesn't equal 1.0. But, if I remove the virtual destructor from the vector class, then the code now works. Why? I spent hours wondering what was wrong with my code and i'd love to hear the real reason why.... Oh - one last thing; i'm using Visual C++ 6.0 Professional, Service Pack 4.0 & Processor Pack (with DirectX 7.0a SDK not that this should matter) Later Jx Edited by - Jx on 11/13/00 3:51:03 AM Edited by - Jx on 11/13/00 3:52:20 AM Edited by - Jx on 11/13/00 3:53:22 AM Edited by - Jx on 11/13/00 3:53:56 AM
Virtual functions require a so-called ''vtable'' which takes up 4 bytes (its a 32bit address)

Solution: don''t use virtual stuff for simple structures.
-------homepage - email
Advertisement
As I see it, you''re using classes not the way they are meant to be... (No offence meant) ;-)
The way you want to handle your code, better use "struct"! Classes are better used when handling more complex code, inheritance, etc. You should not change the value of member variables of a class directly (m4x4[0][0]=1.0 but with Get- and Set-functions...
I don''t want to lecture you, but for your purpose, in my opinion, structures would be the better way!

------------------------------

There are only 10 kinds of people: those that understand binary and those that don't.

As Countach said a class with a virtual function will create a vtable. So don''t make virtual destructors unless you have another virtual function and you need polymorphism. And if you don''t need destructors at all, don''t use them, they can make your code not going inline. Take a further look at Visual c++ destructors are evil.

This topic is closed to new replies.

Advertisement