Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Order of Variable

Reply
Thread Tools

Order of Variable

 
 
shaanxxx
Guest
Posts: n/a
 
      09-10-2010
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 ?


 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      09-10-2010
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
 
Reply With Quote
 
 
 
 
Alf P. Steinbach /Usenet
Guest
Posts: n/a
 
      09-10-2010
* 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>
 
Reply With Quote
 
shaanxxx
Guest
Posts: n/a
 
      09-10-2010
On Sep 10, 8:40*am, Ian Collins <(E-Mail Removed)> 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 .


 
Reply With Quote
 
shaanxxx
Guest
Posts: n/a
 
      09-10-2010
On Sep 10, 8:40*am, Ian Collins <(E-Mail Removed)> 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

 
Reply With Quote
 
tni
Guest
Posts: n/a
 
      09-10-2010
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).
 
Reply With Quote
 
Stuart Redmann
Guest
Posts: n/a
 
      09-10-2010
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
 
Reply With Quote
 
cpp4ever
Guest
Posts: n/a
 
      09-10-2010
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
 
Reply With Quote
 
Goran Pusic
Guest
Posts: n/a
 
      09-10-2010
On Sep 10, 5:36*am, shaanxxx <(E-Mail Removed)> 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.
 
Reply With Quote
 
Alf P. Steinbach /Usenet
Guest
Posts: n/a
 
      09-10-2010
* 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>
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
"Variable variable name" or "variable lvalue" mfglinux Python 11 09-12-2007 03:08 AM
If you get an order # does it mean the order is accepted? =?Utf-8?B?U3RldmUxMDc3?= Windows 64bit 3 05-12-2005 11:46 PM
How do I scope a variable if the variable name contains a variable? David Filmer Perl Misc 19 05-21-2004 03:55 PM
Traversion order cf. output order in XSL Soren Kuula XML 2 02-01-2004 09:10 AM
How to Display DropDownList with preserved order (custom order) cspoh ASP .Net Web Controls 0 07-31-2003 09:19 AM



Advertisments