John Harrison 写道:
ThazKool wrote: Is there anyway to write a class or struct that has no storage. It
only operates on a reference to an already existing type.
You can only have a reference to an existing object not type.
In a class that had a reference to an existing object I would expect
that class to have a size the same as a class with a pointer to an
existing object. In any case the size will not be zero, most likely it
will be four.
No type has a size of zero, but there is something called empty base
class optimization which can mean that one class does not add to the
size of another class when used as a base class, but that is not
relevant to your question.
john
In addition, the empty base class optimization may not be supported by
some compiler.
However,even if the compiler supports the feature your base class will
also has its size. but another derived class sets as an empty one will
not need extra space to store.
for example:
class empty{
typedef int INT;
};
// suppose the size of this base class is N
class another : public empty{
};
// the size of this class is also N
But if a class is derived from more than one empty classes and there
are inherit relationship among this classes, the empty class
optimizatino will not work.
for example:
class three: public empty{
};
// the size of this class is N
class four: public three,another{
};
// more than N