Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / March 2007

Tip: Looking for answers? Try searching our database.

sizeof() on members

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Michael Bauers - 19 Mar 2007 19:24 GMT
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
 }  
};
Ben Voigt - 19 Mar 2007 21:33 GMT
>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
[quoted text clipped - 4 lines]
> 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?
>
[quoted text clipped - 12 lines]
>  }
> };
Michael Bauers - 19 Mar 2007 22:40 GMT
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
{
};
Tamas Demjen - 20 Mar 2007 02:02 GMT
> class  C
> {
[quoted text clipped - 5 lines]
>   {
>     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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.