Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2011-10-19
6
Medium Priority
?
491 Views
Last Modified: 2012-08-13
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?
0
Comment
Question by:Wanting2LearnMan
6 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 168 total points
ID: 36991697
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
 
LVL 35

Assisted Solution

by:sarabande
sarabande earned 336 total points
ID: 36991785
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
 
LVL 35

Assisted Solution

by:sarabande
sarabande earned 336 total points
ID: 36991882
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 13

Assisted Solution

by:Hugh McCurdy
Hugh McCurdy earned 164 total points
ID: 36992042
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
 
LVL 40

Assisted Solution

by:evilrix
evilrix earned 332 total points
ID: 36995190
>> 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
 
LVL 40

Assisted Solution

by:evilrix
evilrix earned 332 total points
ID: 36995281
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question