More offsetof

T

Tony Johansson

Hello!

Assume I have a class definition of CSpaceship below.
Here you have the macro definition of
#define METHOD_PROLOGUE(theClass, localClass) \
theClass* pThis = \
((theClass*)((BYTE*)this - offsetof(theClass, m_x##localClass)));

If I have the following statement METHOD_PROLOGUE(CSpaceship, XMotion)
within class XMotion in method GetPosition.what will the result of pThis be.
I also wonder what is this here?

class CSpaceship

{

protected:

int m_nPosition, m_nAcceleration

public:

Cspaceship(){m_nPosition = m_nAcceleration =0;}

ClassXMotion : public Imotion

{

Xmotion(){}

virtual void Fly();

virtual int& GetPosition();

}m_xMotion;



friend class Xmotion*;

};



//Tony
 
A

Alf P. Steinbach

* Tony Johansson:
Hello!

Assume I have a class definition of CSpaceship below.
Here you have the macro definition of
#define METHOD_PROLOGUE(theClass, localClass) \
theClass* pThis = \
((theClass*)((BYTE*)this - offsetof(theClass, m_x##localClass)));

If I have the following statement METHOD_PROLOGUE(CSpaceship, XMotion)
within class XMotion in method GetPosition.what will the result of pThis be.

A full answers to this question is impossible without you understanding
about 'this' pointers -- which you state below that you don't.

However, the C cast uses at least one reimplement_cast in this case, and
so the result is formally undefined, and that one Good (but incomplete)
answer.

Also, in the case where the XMotion object is not a direct subobject of
a CSpaceShip object the pointer is a wild, invalid one anyway, and
that's anoter Good (but incomplete) answer.

I also wonder what is this here?

The 'this' pointer. Which is so basic a concept in C++ that it's
evident you need a much better textbook than you have (assuming you do
have one). Try "Accelerated C++", and add to that "The C++ Programming
Language"; also see the FAQ.
 
R

Ron Natalie

Tony said:
Hello!

Assume I have a class definition of CSpaceship below.
Here you have the macro definition of
#define METHOD_PROLOGUE(theClass, localClass) \
theClass* pThis = \
((theClass*)((BYTE*)this - offsetof(theClass, m_x##localClass)));
You can not use offsetof on a non-POD class.
 
M

Mike Wahler

Alf P. Steinbach said:
* Tony Johansson: be.

A full answers to this question is impossible without you understanding
about 'this' pointers -- which you state below that you don't.

However, the C cast uses at least one reimplement_cast

ITYM 'reinterpret_cast'

(But the idea of 'reimplement_cast' is intriguing. :) )

-Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top