473,387 Members | 1,619 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,387 software developers and data experts.

sizeof() on members

I am trying to migrate code to MSVS 2005. I found some code trying to take
sizeof a member in a nested struct, see below. I know the typedef is wacky,
probably a C coder who did not know C++ well. Now, to me, neither sizeof()
looks right, I thought there were restrictions on that kind of thing.
Apparently I can use the second variation, but that looks iffy to me as well.

Is the best(preferred?) way something like this?

S tempS; sizeof(tempS.x);

class C
{
typedef struct
{
int x;
} S;
void f()
{
sizeof(C::S::x); // Used to work
this->S::x; // Still works
}
};

Mar 19 '07 #1
3 1247

"Michael Bauers" <Mi***********@discussions.microsoft.comwrote in message
news:03**********************************@microsof t.com...
>I am trying to migrate code to MSVS 2005. I found some code trying to take
sizeof a member in a nested struct, see below. I know the typedef is
wacky,
probably a C coder who did not know C++ well. Now, to me, neither
sizeof()
looks right, I thought there were restrictions on that kind of thing.
Apparently I can use the second variation, but that looks iffy to me as
well.
Can you nuke the typedef, or at least put a tag on the nested struct so it's
not anonymous any longer. Then you can refer to it directly as before.
>
Is the best(preferred?) way something like this?

S tempS; sizeof(tempS.x);

class C
{
typedef struct
{
int x;
} S;
void f()
{
sizeof(C::S::x); // Used to work
this->S::x; // Still works
}
};



Mar 19 '07 #2
I shouldn't have even included the typedef, sorry. That just added confusion.

It's irrelevant, as the error occurs if I simply define S as this:
struct S
{
};
Mar 19 '07 #3
Michael Bauers wrote:
>
class C
{
typedef struct
{
int x;
} S;
void f()
{
sizeof(C::S::x); // Used to work
The compiler seems to be correct about this error, Comeau and the EDG
front-end fail with this code as well. Borland doesn't compile it
either. Just go to dinkumware.com or comeaucomputing.com and try to
compile it online.

The reason it fails is because x is a nonstatic member. You can't use
the S::x syntax there. Try this, it should work:

sizeof(static_cast<S*>(0)->x);

I'm not sure if it's possible to get this expression any simpler than this.
this->S::x; // Still works
This shouldn't compile either.

Tom
Mar 20 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: balasubramaniam | last post by:
I comile the code in unix where sizof(int)=4 #include<iostream> using namespace std; class A { int i; static int j; }; int A::j = 10;
2
by: Xiangliang Meng | last post by:
Hi, all. What will we get from sizeof(a class without data members and virtual functions)? For example: class abnormity { public: string name() { return "abnormity"; }
3
by: Christof Warlich | last post by:
Hi, I'm trying to build an _efficient_, _purely_ _abstract_ API for inter process(or) communication, _completely_ hiding any implementation details. The core components are "Buffer", "Address"...
19
by: Martin Pohlack | last post by:
Hi, I have a funtion which shall compute the amount for a later malloc. In this function I need the sizes of some struct members without having an instance or pointer of the struct. As...
12
by: news.fe.internet.bosch.com | last post by:
Hi All , I u find out size of struct , does it considers paddding chars into consideration struct A { char c; int i; };
2
by: GRenard | last post by:
Hello, We just switch in our company to VisualStudio 2005 and the new ATL library. We use a wrapper to use CFileDialog. Its name is CFileDialogDeluxe Here the call CFileDialogDeluxe...
5
by: mathemagic | last post by:
Hello All, I apologise if this has been covered by an earlier post. I just wanted to know how the functions of a class are tied to the class. ie, in C structs, since u can have only data...
5
Banfa
by: Banfa | last post by:
I thought I would write a little article about this because it is regularly asked as a question in the Forums. Firstly this is a C++ issue, it is strictly against the rules of C to create a class...
40
by: Boltar | last post by:
Hi Why - using gcc on linux - does this return 0 in C but returns 1 in C+ +? I don't get it. #include <stdio.h> struct foo { };
27
by: CodeMonk3y | last post by:
gotta question on sizeof keyword does the sizeof keyword calcuates the size at compile time or run time ?? -- Posted on news://freenews.netfront.net - Complaints to news@netfront.net --
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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,...
0
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...

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.