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

Discussion in 'C++' started by John Goche, Oct 12, 2006.

  1. John Goche

    John Goche Guest

    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
     
    John Goche, Oct 12, 2006
    #1
    1. Advertising

  2. John Goche

    Phlip Guest

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

    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 Goche wrote:

    > 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!

    --
    Phlip
    http://www.greencheese.us/ZeekLand <-- NOT a blog!!!
     
    Phlip, Oct 12, 2006
    #2
    1. Advertising

  3. John Goche

    Jim Langston Guest

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

    "John Goche" <> wrote in message
    news:...
    >
    > 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?
     
    Jim Langston, Oct 13, 2006
    #3
  4. John Goche

    Greg Guest

    John Goche wrote:
    > 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
     
    Greg, Oct 13, 2006
    #4
  5. John Goche

    John Goche Guest

    > John Goche wrote:
    >
    > > 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.

    >


    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();

    Any ideas?

    Thanks,

    JG

    Greg wrote:

    > 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
     
    John Goche, Oct 15, 2006
    #5
  6. John Goche

    Greg Guest

    John Goche wrote:
    > > John Goche wrote:
    > >
    > > > 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.

    > >

    >
    > 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
     
    Greg, Oct 16, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. kevin
    Replies:
    11
    Views:
    5,842
    Andrew McDonagh
    Jan 8, 2005
  2. ken
    Replies:
    3
    Views:
    2,575
    Rolf Magnus
    Nov 8, 2003
  3. Abhishek

    Casting a file pointer to char pointer

    Abhishek, Mar 21, 2006, in forum: C Programming
    Replies:
    9
    Views:
    668
    santosh
    Mar 22, 2006
  4. Replies:
    11
    Views:
    1,427
    James Kanze
    Jun 7, 2007
  5. Alex Shen
    Replies:
    5
    Views:
    443
    Alex Shen
    Oct 30, 2010
Loading...

Share This Page