Re: Using a macro, can I change what type an object is being cast to?

From: Karl Heinz Buchegger (kbuchegg_at_gascad.at)
Date: 03/16/05


Date: Wed, 16 Mar 2005 21:29:52 +0100

Eric Lilja wrote:
>
> So why do I want to do this? Well, as I said I am trying to encapsulate a
> third-party C-based gui toolkit that revolves around callbacks. My parent
> class has a static member function that acts a dummy callback (called by the
> system), and using the parameters it finds a pointer to itself (actually a
> pointer to a subclass) and uses that to call non-static member functions.
> The problem is that the system seems to be calling this static member
> function before the subclass object has been constructed fully thus calling
> the parent version of some virtual functions the first few calls.

And you think, that casting the pointer will help with that?
If the object isn't fully constructed, the most likely thing is
that you produce a crash (if you are lucky). If you are unlucky
the system just executes whatever it finds in memory, thinking
this is your function.

Why not install the callback *after* the object has been constructed
fully. That would avoid all those problems.

-- 
Karl Heinz Buchegger
kbuchegg@gascad.at


Relevant Pages

  • Re: P/Invoke MFC member
    ... Why not create a function which will make the call to the static member ... > exposes limited API functionality, ... > that returns a pointer to its CWinApp object. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: creating thread in C++
    ... This is defined behaviour as long as you always pass a non-NULL pointer ... to myclass together with the function pointer &myclass::threadstub to ... a static member function does not have ... required extern "C" linkage. ...
    (comp.programming.threads)
  • Re: Private constructor
    ... They can if they access it through a pointer or a reference. ... > With constructors, this is different although they are non-static. ... The static member can create an object of the ... Access control (which is the public/private/static protected rules). ...
    (comp.lang.cpp)
  • Re: Static/ non-static member access
    ... Static member functions certainly can use non-static members, ... have no "this" pointer and thus don't operate implicitly on an object ... // Placement new - constructs a T in the memory pointed to by temp. ...
    (microsoft.public.vc.language)
  • NullReferenceException using static members in a multi-thread .exe
    ... 'System.NullReferenceException' exception. ... This happen only if the class is not initialized (calling any static member) ... If I invoke a static member in main before threads start. ...
    (microsoft.public.dotnet.framework)

Loading