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

  • Home
  • SEARCH
  • 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 845911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T06:31:50+00:00 2026-05-15T06:31:50+00:00

I had a problem while hacking a bigger project so I made a simpel

  • 0

I had a problem while hacking a bigger project so I made a simpel test case. If I’m not omitting something, my test code works fine, but maybe it works accidentally so I wanted to show it to you and ask if there are any pitfalls in this approach.

I have an OutObj which has a member variable (pointer) InObj. InObj has a member function. I send the address of this member variable object (InObj) to a callback function as void*. The type of this object never changes so inside the callback I recast to its original type and call the aFunc member function in it. In this exampel it works as expected, but in the project I’m working on it doesn’t. So I might be omitting something or maybe there is a pitfall here and this works accidentally. Any comments? Thanks a lot in advance.

(The problem I have in my original code is that InObj.data is garbage).

#include <stdio.h>

class InObj
{
public:
 int data;
 InObj(int argData);
 void aFunc()
 {
  printf("Inside aFunc! data is: %d\n", data);
 };
};

InObj::InObj(int argData)
{
 data = argData;
}

class OutObj
{
public:
 InObj* objPtr;
 OutObj(int data);
 ~OutObj();
};

OutObj::OutObj(int data)
{
 objPtr = new InObj(data);
}

OutObj::~OutObj()
{
 delete objPtr;
}

void callback(void* context)
{
 ((InObj*)context)->aFunc();
}

int main ()
{
 OutObj a(42);
 callback((void*)a.objPtr);
}
  • 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-15T06:31:50+00:00Added an answer on May 15, 2026 at 6:31 am

    Yes, this is safe.

    A pointer to any type can be converted to a pointer to void and back again.

    Note that the conversion to void* is implicit, so you don’t need the cast.

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

Sidebar

Related Questions

I have had the following problem for a while and I am really not
I had this weird problem a little while ago and haven't been able to
While developing an application, I had the following problem. I wanted to return an
We found a problem while accessing the Web service from our ASP.Net MVC project
EDIT I had a problem a while ago writing a program in C which
I've had this problem for a while now but I have to remedy it
I have had this problem for a while, still trying to work on a
I had encountered a problem while using Loki::Singleton , Loki::SmartPtr , and std::vector under
I had encountered strange problem while construct a unordeed_set<tuple<int,int>> . I had tried VC++8,
So I've run into a problem while integrating QT and OSG. I've had a

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.