> What will be the size of a class with no instance variable ?
> 

I read your question wrongly as "What will be the size of a class with no 
instance". So replying again.

I would say that the question "What will be the size of a class with no 
instance variable?" is not specific. So there are different answers.

1. The size of a class with no "non-static member variable" (I think this term 
is correct),
which doesn't inherit any other class and doesn't have any virtual functions in 
it is 1. (The least possible integer)

2. The size of a class with no non-static member variable, which inherits other 
classes and has virtual functions.
        This depends on the size of the immediate base class(s) and also 
depends on if virtual
        functions are present in that class or in any of the base classes. In 
the following
        example, eventhough there are no non-static member variables in the 
class and in any of
        the base classes, the size is 8 because the virtual base class pointer 
adds a 4 bytes 
        overhead in each virtual inheritance.

#include <iostream>
using namespace std;

class Base
{
};

class Interm1: public virtual Base
{
};

class Interm2: public virtual Base
{
};

class Derived: public Interm1, public Interm2
{
};

int main()
{
  cout << sizeof(Derived);
  return 0;
}


If there are virtual functions in the class or in any of the base classes, 
there will be a 4
bytes overhead, which is the sizeof(vptr). 

class ABC
{
  virtual void function1()
  {
  }
};

int main()
{
  cout << sizeof(ABC);
  return 0;
}


Regards,
Jos Collin

_______________________________________________
Indian Libre User Group Cochin Mailing List
http://www.ilug-cochin.org/mailing-list/
http://mail.ilug-cochin.org/mailman/listinfo/mailinglist_ilug-cochin.org
#[email protected]

Reply via email to