![]() |
Order of Variable
i have following class
Class A { public : int a; int b; int c; } Class B { public : int a; int b; } can i take pointer of type *B and point to class A Object and successfully modify Data member a and b of A . What standard has to say about it ? B * ptr = reinterpret_cast<B*>( new A ); Are Data members laid in memory in order in which they are declared ? |
Re: Order of Variable
On 09/10/10 03:36 PM, shaanxxx wrote:
> i have following class > > Class A > { > public : > int a; > int b; > int c; > } > > Class B > { > public : > int a; > int b; > } > > can i take pointer of type *B and point to class A Object and > successfully modify Data member a and b of A . Probably, but why would you want to? > What standard has to say about it ? Not a lot, because its silly and unnecessary. > B * ptr = reinterpret_cast<B*>( new A ); > > Are Data members laid in memory in order in which they are declared ? For POD structs, yes. -- Ian Collins |
Re: Order of Variable
* shaanxxx, on 10.09.2010 05:36:
> i have following class > > Class A > { > public : > int a; > int b; > int c; > } > > Class B > { > public : > int a; > int b; > } > > can i take pointer of type *B and point to class A Object and > successfully modify Data member a and b of A . What standard has to > say about it ? In C++98 you're formally in UB-land when you try that on non-POD types such as above. For POD types the standard supports it via the discussion of layout-compatible types in §9.2, and in particular support of reinterpret_cast from first member in struct to struct and vice versa in §9.2/17. This support is there because it was/is a common technique in C. In C++ you simply use inheritance instead. No reinterpret_cast required then. :-) > B * ptr = reinterpret_cast<B*>( new A ); Here you're on shaky ground because you need to deallocate via an A* pointer. Also this problem is solved by inheritance, combined with a virtual destructor. > Are Data members laid in memory in order in which they are declared ? As long as there's no intervening access specifier, yes. Later members are then at higher addresses than earlier members. However, there can be padding between them (although not before the first member of POD). Cheers & hth., - Alf -- blog at <url: http://alfps.wordpress.com> |
Re: Order of Variable
On Sep 10, 8:40*am, Ian Collins <ian-n...@hotmail.com> wrote:
> On 09/10/10 03:36 PM, shaanxxx wrote: > > > > > > > i have following class > > > Class A > > { > > public : > > int a; > > int b; > > int c; > > } > > > Class B > > { > > public : > > int a; > > int b; > > } > > > can i take pointer of type *B and point to class A Object and > > successfully modify Data member a and b of A . > > Probably, but why would you want to? > > > What standard has to say about it ? > > Not a lot, because its silly and unnecessary. > > > B * ptr = reinterpret_cast<B*>( new A ); > > > Are Data members laid in memory in order in which they are declared ? > > For POD structs, yes. > > -- > Ian Collins- Hide quoted text - > > - Show quoted text - You can think of class B as old implementation and A is new implementation. If you dont specify data member c , implementation will set C to some default value . |
Re: Order of Variable
On Sep 10, 8:40*am, Ian Collins <ian-n...@hotmail.com> wrote:
> On 09/10/10 03:36 PM, shaanxxx wrote: > > > > > > > i have following class > > > Class A > > { > > public : > > int a; > > int b; > > int c; > > } > > > Class B > > { > > public : > > int a; > > int b; > > } > > > can i take pointer of type *B and point to class A Object and > > successfully modify Data member a and b of A . > > Probably, but why would you want to? > > > What standard has to say about it ? > > Not a lot, because its silly and unnecessary. > > > B * ptr = reinterpret_cast<B*>( new A ); > > > Are Data members laid in memory in order in which they are declared ? > > For POD structs, yes. > > -- > Ian Collins- Hide quoted text - > > - Show quoted text - What happens if I add non-virtual member function to 1) Class A 2) Class A and Class B 3) Class B |
Re: Order of Variable
On 2010-09-10 5:51, Alf P. Steinbach /Usenet wrote:
> * shaanxxx, on 10.09.2010 05:36: >> i have following class >> >> Class A >> { >> public : >> int a; >> int b; >> int c; >> } >> >> Class B >> { >> public : >> int a; >> int b; >> } >> >> can i take pointer of type *B and point to class A Object and >> successfully modify Data member a and b of A . What standard has to >> say about it ? > > In C++98 you're formally in UB-land when you try that on non-POD types > such as above. > > For POD types the standard supports it via the discussion of > layout-compatible types in §9.2, and in particular support of > reinterpret_cast from first member in struct to struct and vice versa in > §9.2/17. Things aren't quite that rosy. You still violate the aliasing rules and there enough compilers that will happily generate wrong code. The only safe thing to do is memcpy or using a union (if your C++ compiler supports C99 semantics for the union access, the C++ standard itself doesn't really allow it). |
Re: Order of Variable
On 09/10/10 shaanxxx wrote:
> > > i have following class > > > Class A > > > { > > > public : > > > int a; > > > int b; > > > int c; > > > } > > > Class B > > > { > > > public : > > > int a; > > > int b; > > > } > > > can i take pointer of type *B and point to class A Object and > > > successfully modify Data member a and b of A . > > > What standard has to say about it ? The standard says that your program will produce UB. > > > Are Data members laid in memory in order in which they are declared ? On Sep 10 Ian Collins wrote: > > For POD structs, yes. On 10 Sep. shaanxxx wrote: > What happens if I add non-virtual member function to > 1) Class A > 2) Class A and Class B > 3) Class B- Zitierten Text ausblenden - Why don't you just try it and see? You could also buy this book "Inside the C++ Object Model" from Stan Lippman, which Victor Bazarov recommended in his quite recent posting 2341de56-493f-46bf-9c23-3c6a73c5dd79...oglegroups.com. You should have noted by now that the standard doesn't give you any guarantee that your code is going to work. Any further questions whether some code will work should be asked in a newsgroup that is dedicated to the particular compiler you are using. Regards, Stuart |
Re: Order of Variable
On 09/10/2010 04:36 AM, shaanxxx wrote:
> i have following class > > Class A > { > public : > int a; > int b; > int c; > } > > Class B > { > public : > int a; > int b; > } > > can i take pointer of type *B and point to class A Object and > successfully modify Data member a and b of A . What standard has to > say about it ? > > B * ptr = reinterpret_cast<B*>( new A ); > > > Are Data members laid in memory in order in which they are declared ? > > I would strongly discourage doing this, even if it appears to work, there is no guarantee it will work on another platform, or for future updates. The type of thing you are trying to do should be implemented using public inheritance as shown below. class B { public: int a; int b; }; class A : public B { public: int c; }; B *ptr = new A; HTH cpp4ever |
Re: Order of Variable
On Sep 10, 5:36*am, shaanxxx <shaan...@yahoo.com> wrote:
> i have following class > > Class A > { > public : > int a; > int b; > int c; > > } > > Class B > { > public : > int a; > int b; > > } > > can i take pointer of type *B and point to class A Object and > successfully modify Data member a and b of A . What standard has to > say about it ? ;-) I don't think you should care about the standard. I think you should care about solving your actual problem, which I guess is interfacing two parts of the code, where you control one. Well, the one you do control, you can either derive from A from B or vice-versa, or use composition and avoid any hacks. IOW, what are you trying to do in the grander scheme of things? Goran. |
Re: Order of Variable
* tni, on 10.09.2010 08:31:
> On 2010-09-10 5:51, Alf P. Steinbach /Usenet wrote: >> * shaanxxx, on 10.09.2010 05:36: >>> i have following class >>> >>> Class A >>> { >>> public : >>> int a; >>> int b; >>> int c; >>> } >>> >>> Class B >>> { >>> public : >>> int a; >>> int b; >>> } >>> >>> can i take pointer of type *B and point to class A Object and >>> successfully modify Data member a and b of A . What standard has to >>> say about it ? >> >> In C++98 you're formally in UB-land when you try that on non-POD types >> such as above. >> >> For POD types the standard supports it via the discussion of >> layout-compatible types in §9.2, and in particular support of >> reinterpret_cast from first member in struct to struct and vice versa in >> §9.2/17. > > Things aren't quite that rosy. You still violate the aliasing rules and there > enough compilers that will happily generate wrong code. Nah to both. :-) > The only safe thing to do is memcpy or using a union (if your C++ compiler > supports C99 semantics for the union access, the C++ standard itself doesn't > really allow it). Ouch. It's an old C technique. No compiler will foul it up. Don't add needless complexity. Cheers & hth., - ALf -- blog at <url: http://alfps.wordpress.com> |
| All times are GMT. The time now is 12:58 AM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.