Re: Getting class pointer from interface pointer [don't use structures?]



I'm not a professional programmer, just a hobbyist, but I recall
reading a book on MFC programming by Richard M. Jones "Intro to MFC
Programming with Visual C++" that indicated he had a bug involving a
structure used inside of a class, and for the life of him he could not
get rid of the bug until he changed the structure to a class. Since
Jones is a professional and teaches as well, I concluded structures are
poorly supported by Microsoft--change to a class and see if that helps.
Compilers do stuff behind the scenes, especially with dotnet languages
where everything is 'virtual' and/or placed on the heap, that you may
not be aware of.

RL

Rory Plaire wrote:
Sure - thanks.

Say I have this struct definition:

struct IUserObject
{
RESULT Invoke(int method) = 0;
void Destroy() = 0;
}

And this class definition:

class ManagedWrapper : IUserObject
{
RESULT Invoke(int method);
void Destroy() ;
}

And finally, a method to get an instance of the base struct (interface):

IUserObject* GetUserObject();

When I invoke the method to get the interface-typed instance, I can't cast
it reliably:

ManagedWrapper* wrap = dynamic_cast<ManagedWrapper*>(GetUserObject());

... this invocation is always results in a pointer referenceing a location a
DWORD preceeding the ManagedWrapper* implementation, and all dereferences are
then invalid since they are all off by -4 bytes.

Can you see something I can't?

-r

"Peter Ritchie [C# MVP]" wrote:

Sorry, when you mentioned non-managed interface I assumed COM since C++
doesn't have the concept of an "interface", just struct or class.

Can you give me an example of what you're doing that doesn't work?

.



Relevant Pages

  • Re: reading from a serial port
    ... >> array of structs, it uses a pointer to one element in an array ... >far enough to know that FILE is possibly a typedef to some struct. ... Ritchie, "The C Programming Language", 2nd Edition. ... the stdio library allocates a data buffer (BUFSIZ in stdio.h is ...
    (comp.unix.programmer)
  • Re: string class question..
    ... I didn't necessarily mean timing or memory overhead. ... when all I needed was a struct. ... that do usually do not tout it as the be-all end-all of programming. ... data hiding has it's uses. ...
    (comp.lang.cpp)
  • Re: WinCE Struct Help
    ... Some of us just started programming while others are more advanced. ... >> what do you mean basic concept of malloc and realloc?? ... But I want to use struct. ... >> malloc or realloc function for struct? ...
    (microsoft.public.windowsce.app.development)
  • Re: Object-oriented programming in standard ANSI C
    ... The C computer programming language allows you to encapsulate data ... There have been many discussions whether encapsulation and information ... (publishing a struct declaration without publishing it's definition) ...
    (comp.lang.c)