Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7664873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T14:24:53+00:00 2026-05-31T14:24:53+00:00

I have a weird problem with my code as the compiler generates code which

  • 0

I have a weird problem with my code as the compiler generates code which crashes my testing application.
I am using Visual C++ 2010 compiler.
The code is:

template < typename TDstType,
           typename TSrcType >
TDstType unsafe_cast( TSrcType anySrc )
{
    return ( TDstType ) anySrc;
}

template < typename TDstType,
           typename TSrcType >
TDstType brutal_cast( TSrcType anySrc )
{
    return *( TDstType* ) &anySrc;
}

template < typename TParamType >
class EventHandler
{

public:

    template < typename TObjectType >
    EventHandler( TObjectType&  refObject, 
           void ( TObjectType::*pfnMethod )( TParamType ) );

    void operator()( TParamType anyParam );

private:

    void* m_ptrMethod;
    void* m_ptrObject;

};

template < typename TParamType  >
template < typename TObjectType >
inline EventHandler< TParamType >::EventHandler( TObjectType& refObject, void ( TObjectType::*pfnMethod )( TParamType ) )
: m_ptrMethod( brutal_cast< void* >( pfnMethod ) ),
  m_ptrObject( &refObject )
{
}

template < typename TParamType >
inline void EventHandler< TParamType >::operator()( TParamType anyParam )
{
                    class Class;
    ( unsafe_cast<        Class  *                 >( m_ptrObject )->*
      brutal_cast< void ( Class::* )( TParamType ) >( m_ptrMethod ) )( anyParam );
}

And the testing application’s code:

class SomeClass
{

public:

    void Method( int intParam )
    {
        printf( "%d\n", intParam );
    }

};

int main( int intArgc, char* arrArgv[] )
{
    EventHandler< int > varEventHandler( *new SomeClass(), &SomeClass::Method );

    varEventHandler( 10 );

    return 0;
}

The compiled application crashes as it have tried to read from an invalid memory location. I checked in the Visual Studio’s debugger every variable going though and none contains invalid address.
I hope anyone can help me fix this as I failed. Maybe a coffee overdose is the reason though…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T14:24:55+00:00Added an answer on May 31, 2026 at 2:24 pm

    This cannot work; you can’t cast pointer-to-member to void*. Doing so loses information and the program, unsurprisingly, crashes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have weird problem. I'm using Eclipse for writing J2EE (java, jsf, javascript) application
We have this really weird problem in a Web Application when using ActiveRecord 2.0
I have a weird problem, dont know which code should I post because I
I have a weird problem with my HTML and CSS code: <header> <h1>title</h1> </header>
I have a really weird problem with fgets() in C. Below is the code
The following code demonstrates a weird problem I have in a Turbo C++ Explorer
I have a weird problem with images in visual web developer, I cant change
I have a weird problem. Using the mail() function and a local Postfix on
I have a weird problem with defining a mpi_type_contiguous and using mpi_gatherv later on.
I have a weird problem with VS 2010 (Premium). just reinstalled it added the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.