473,320 Members | 1,719 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

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
3 1784

"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
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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: MrCoder | last post by:
Hey guys, my first post on here so I'll just say "Hello everbody!" Ok heres my question for you lot. Is there a faster way to compare 1 byte array to another? This is my current code //...
4
by: Shashi | last post by:
Can somebody explain how the byte alignment for structures work, taking the following example and considering: byte of 1 Byte word of 2 Bytes dword of 4 Bytes typedef struct { byte a; word...
13
by: Ray Z | last post by:
So far, I get the idea that if I want to use both the unmanaged and managed memory, I can not avoid memory copy. But I DO need to avoid it. I get a idea that maybe I could use "union" to convert...
1
by: Marquee | last post by:
Hello, This is my first program c#, my background is c++. One thing I would like to do is put binary data (e.g. a record from disk, or network packet) in a struct. In C++ I would create the...
18
by: p.lavarre | last post by:
Can Python not express the idea of a three-byte int? For instance, in the working example below, can we somehow collapse the three calls of struct.pack into one? 08 12 34 56 80 00 I ask...
7
by: p.lavarre | last post by:
How do I vary the byte offset of a field of a ctypes.Structure? How do I "use the dynamic nature of Python, and (re-)define the data type after the required size is already known, on a case by...
10
by: Scott Townsend | last post by:
So I need to talk to a devices that expects all of the bits and bytes I sent it to be in specific places (not yet 100% defined). I wanted to create a structure/class with all of the data in it...
2
by: O.B. | last post by:
When using Marshal to copy data from a byte array to the structure below, only the first byte of the "other" array is getting copied from the original byte array. What do I need to specify to get...
28
by: cpluslearn | last post by:
Hi, I have a local class inside a function template. I am able to wrap any type in my local class. Is it legal C++? What type of class is Local? Is it a class template or regular class? Thanks...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shllpp 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.