By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,159 Members | 1,941 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,159 IT Pros & Developers. It's quick & easy.

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

P: n/a
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

Oct 20 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a

"ThazKool" <Ch**********@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
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
Oct 20 '05 #2

P: n/a
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
Oct 20 '05 #3

P: n/a

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

Oct 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.