Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Order of Variable (http://www.velocityreviews.com/forums/t732949-order-of-variable.html)

shaanxxx 09-10-2010 03:36 AM

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 ?



Ian Collins 09-10-2010 03:40 AM

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

Alf P. Steinbach /Usenet 09-10-2010 03:51 AM

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>

shaanxxx 09-10-2010 04:14 AM

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 .



shaanxxx 09-10-2010 04:18 AM

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


tni 09-10-2010 06:31 AM

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).

Stuart Redmann 09-10-2010 06:51 AM

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

cpp4ever 09-10-2010 07:00 AM

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

Goran Pusic 09-10-2010 07:08 AM

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.

Alf P. Steinbach /Usenet 09-10-2010 01:31 PM

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.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57