Can there be a 0 byte size class or struct to wrap a ref

T

ThazKool

Is there anyway to write a class or struct that has no storage. It
only operates on a reference to an already existing type.

This is actually an extension to another thread. The thread went a
little off of what I was looking for. Hence this new thread.

Thanks,
ThazKool
 
M

Mike Wahler

ThazKool said:
Is there anyway to write a class or struct that has no storage.

If you mean "can an object have zero size", the answer
is no.
It
only operates on a reference to an already existing type.

This is actually an extension to another thread. The thread went a
little off of what I was looking for. Hence this new thread.

Why do you need an object with no size?

-Mike
 
J

John Harrison

ThazKool said:
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
 
L

lxconan

John Harrison 写é“:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,434
Messages
2,571,690
Members
48,796
Latest member
Greg L.

Latest Threads

Top