Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to pass a class as a parameter to a function in c++?

Tags:

c++

I have a function with void * as one of its parameters (the void is used as a generic object). But to be able to call a function in that generic object, I need to cast it first, and for that I need to know what type the class is. And I wanted to know, is it possible to pass a class or some information that allows me to cast the object as a function parameter?

like image 396
XaitormanX Avatar asked Apr 03 '12 15:04

XaitormanX


1 Answers

By any chance have you looked into Templates?

An example would be something such as

class SomeClass
{
public:
    template<typename CastClass>
    void DoSomething(void* someArg)
    {
         (CastClass)someArg;
    }
};

Usage:

class A{ }; // Some random test class

SomeClass test;
A a;
test.DoSomething<int>(&a); // The template parameter can be anything. 
                           // I just have int to make it a smaller example.
like image 170
josephthomas Avatar answered Oct 01 '22 18:10

josephthomas