Can I cast the 'this' pointer to a void*??

I have a function that accepts a void* as a parameter:

int SomeFunction(void* object) ;

Open in new window


When I call this function from inside a class function, is it OK to cast the this pointer to a void* like so?

int SomeClass::Somefuncion() const
{
	return SomeFunction((void*)this);
}

Open in new window


After testing this I find that it works, but is it ok to do this?
Wanting2LearnManAsked:
Who is Participating?
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
explicit casting normally indicates you know what you are doing ...
the "better" method for void* is usually to define a interface class, and let all objects that should be passed to that function inherit of that class, instead of using void ...
0
 
sarabandeCommented:
void* means any pointer so you could pass your pointer without an explicit cast.

a void* is used by c interfaces to pass a pointer to somewhat which knows how to cast. often you will get the void* back in a callback function you provided and this function then would cast the pointer to its original type.

Sara
0
 
sarabandeCommented:
to add to angellll's comment:

in a pure c++ environment you rarely would use void*. here you could use baseclass pointers or templates to have arguments which were type-safe.

so when the SomeFunction is in your responsibility i strongly would recommend to replace it by a template function if you want to call it with pointers of classes which were not related or use the baseclass pointer type if all classes could be derived from same baseclass. you also could provide multiple implementations of SomeFunction (given it is c++) with explicite types like

int SomeFunction(int object) ; 
int SomeFunction(SomeClass* object) ; 
int SomeFunction(double object[], int num) ; 

Open in new window


and of course such functions could use some common function(s) internally to avoid duplicate code.

Sara
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
Hugh McCurdyCommented:
I'd just like to add my support to the earlier posters (no points please).  Void pointers make sense in C but not in C++.
0
 
evilrixSenior Software Engineer (Avast)Commented:
>> I have a function that accepts a void* as a parameter:

Just to add to http:#a36991882 : Use templates instead.
#include <iostream>

template <typename ObjType>
int SomeFunction(ObjType & object)
{
   return object.get1();
}

template <typename ObjType>
int SomeFunction(ObjType * pobject)
{
   return pobject->get2();
}

struct foo
{
   int SomeFunction1() const
   {
      return ::SomeFunction(this);
   }

   int SomeFunction2() const
   {
      return ::SomeFunction(*this);
   }

   int get1() const { return 1; }
   int get2() const { return 2; }
};

int main()
{
   foo f;

   std::cout
      << f.SomeFunction1() << std::endl
      << f.SomeFunction2() << std::endl;
}

Open in new window

0
 
evilrixSenior Software Engineer (Avast)Commented:
Or, better still, learn how to use functors to encapsulate behaviour using a command pattern.


Below is a simple example using Boost bind.
#include <iostream>
#include <boost/bind.hpp>
#include <boost/function.hpp>

int SomeFunction(boost::function0<int> functor)
{
   return functor();
}

struct foo
{
   int SomeFunction1() const
   {
      return ::SomeFunction(boost::bind(&foo::get1, this));
   }

   int SomeFunction2() const
   {
      return ::SomeFunction(boost::bind(&foo::get2, this));
   }

   int get1() const { return 1; }
   int get2() const { return 2; }
};

int main()
{
   foo f;

   std::cout
      << f.SomeFunction1() << std::endl
      << f.SomeFunction2() << std::endl;
}

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.