473,654 Members | 3,280 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1803

"ThazKool" <Ch**********@g mail.com> wrote in message
news:11******** **************@ g49g2000cwa.goo glegroups.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
20619
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 // Check for a match
4
17681
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 b;
13
2153
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 byte to int and so on. Here is my source code, I wonder if this will work with GC? struct MyUnion {
1
3704
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 struct and memcpy() the data into it thus creating a structured overlay. The struct can have variable length e.g. struct CRecord //Just for example {
18
7560
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 because I'm trying to refactor working code that is concise: 08 12 34 56 80 00
7
7022
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 case basis"? \\\ For example, suppose sometimes I receive the value '\x03hi' + \x04bye' for the struct:
10
6364
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 and then convert that to a byte array, pass it to the device, then get a reply and then convert that to a structure. I'm having issues with making sure what I've coded is correct. Cant figure out how to define an array in structure that is a...
2
7198
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 Marshal.PtrToStructure to copy the all the data into the "other" array? unsafe public struct DeadReckoning {
28
2546
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 in advance. --dhina --------------------------------------------------------------------------------------------------------------------------------------------- class Foo { public:
0
8290
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8815
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8708
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8489
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8594
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6161
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4294
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2716
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1916
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.