OT: casting class to pointer: how can this work?

J

John Goche

Hello,

I have a question regarding an instance of casting a
class to a pointer which I came across:

The Symbian class CBufSeg has the following function:
virtual TPtr8 Ptr(TInt aPos);
I would like to know why it is possible to cast a TPtr8 to a TText *.
Since TPtr8 is a class, and the compiler does not agree that its text
member will be compacted at the beginning of the class, I don't see
how the cast can make sense.

Thanks,

JG
 
P

Phlip

Firstly, your question is on-topic if someone familiar with the C++ Standard
but not your library could answer it.

Next, putting "OT" in the Subject line does not magically absolve an
off-topic question here. Questions are off-topic when we should not answer
them, and begging permission to ask does not increase the number of people
here who could answer, or change whether we should dilute this group's focus
or reward poor netiquette. Always post to the narrowest possible technical
newsgroup, for the best answers.

John said:
I have a question regarding an instance of casting a
class to a pointer which I came across:

The Symbian class CBufSeg has the following function:
virtual TPtr8 Ptr(TInt aPos);
I would like to know why it is possible to cast a TPtr8 to a TText *.

What does TPtr8 look like, and what does the typecast look like? The above
is a function prototype.
Since TPtr8 is a class, and the compiler does not agree that its text
member will be compacted at the beginning of the class, I don't see
how the cast can make sense.

Sometimes library vendors exploit marginally defined behaviors. If, for
example, TPtr8 were a struct with no virtual members, and a single pointer,
then such objects would be reinterpretable as character pointers.

So just post more data, and the question's on topic!
 
J

Jim Langston

John Goche said:
Hello,

I have a question regarding an instance of casting a
class to a pointer which I came across:

The Symbian class CBufSeg has the following function:
virtual TPtr8 Ptr(TInt aPos);
I would like to know why it is possible to cast a TPtr8 to a TText *.
Since TPtr8 is a class, and the compiler does not agree that its text
member will be compacted at the beginning of the class, I don't see
how the cast can make sense.

It depends on the class. In some circumstances it could make sense. Lets
take it from the simplest thing that works and what you can do with it.

class MyClass
{
public:
int MyInt;
float MyFloat;
};

MyClass MyInstance;

unsigned char* Data = reinterpret_cast<unsigned char*>( &MyInstance );

Now what good does that do me? It allows me to copy this (POD) class over
the network or save for to a file for one. This is an alternative to
serialization, although a very poor one, sometimes it can come in handy.
Now, we see that this could be useful if someone knows what they're doing.
And so we can cast to a void pointer or any other type of pointer. So why
not classes?
 
G

Greg

John said:
Hello,

I have a question regarding an instance of casting a
class to a pointer which I came across:

The Symbian class CBufSeg has the following function:
virtual TPtr8 Ptr(TInt aPos);
I would like to know why it is possible to cast a TPtr8 to a TText *.
Since TPtr8 is a class, and the compiler does not agree that its text
member will be compacted at the beginning of the class, I don't see
how the cast can make sense.

If the TPtr8 class (assuming TPtr8 is a class type and not a pointer
typedef) declared (and implemented) a conversion member function like
so:

class TPtr8
{
public:
...
operator TText*();
};

Then a TPtr8 object could be converted (either implicitly, or
explicitly with a cast) to a TText pointer.

Greg
 
G

Greg

John said:
Greg,

The TPtr8 is a class type and not a pointer type. It is described here:

http://www.symbian.com/developer/te...rce/reference/cpp/Descriptors/TPtr8Class.html

However, I don't see an operator TText*() listed here.
There is a method: const TUint8* Ptr() const;

However, assuming foo is a Ptr8, I don't see how
.
TText *bar = (TText *) foo;

would yield what supposedly should be the result of

TText *bar = (TText *) foo.Ptr();

According to the Symbian docs, CBufSet::ptr() returns a TPtr8 object
describing a pointer to that offset within CBufSeg's internal data
buffer. So Ptr() does not return a pointer per se, but rather an object
that describes one. Presumably, the Symbian API has routines that
accept TPtr8 parameters, or possibly, also has conversion routines to
turn a TPtr8 object into an actual C++ pointer.

Greg
 

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

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top