• Advertisement
Sign in to follow this  

C++ Passing an unknown class as an argument to a function

This topic is 906 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Lets say i want a function to take a class as a parameter but i do not know the class in advance or i want it to be able to take different classes, how can i do it?

Class object;
AnotherClass antherObject;


function(Class &object)
{
   //DO STUFF
};

How can i make "function" take "AnotherClass" as a parameter as well? Overloading is a solution but i would still have to copy the code again. Is there another way?

Share this post


Link to post
Share on other sites
Advertisement

It depends on what you want to do.
Templates might be a solution.
Another possible solution is to not pass the entire class, but just parts of it (e.g. just the parts that are common/used from Class and AnotherClass).
Inheritance might also be viable, operating on the base class.
Rewriting the functions/classes so it isn't necessary could also be an alternative.
Sending a void pointer and casting it based on a 2nd "type" parameter might also work, but doesn't sound like a very clean solution.
 
Without knowing any specifics, it's hard to give detailed advice. I would suggest you look into some of the above and see if you can make it work for you.


I just wanted to know if its possible, i am only using the function for just one class. But i will also have another class that will need the same function and i just thought i might save some code. I will try your advice as well...thanks a lot! :)

Share this post


Link to post
Share on other sites

Although it can be done by using inheritance I wouldn't use that option unless the objects are going to use inheritance anyway. Templates are definitely a good option in my opinion (though they can make header files look pretty nasty). A little example:

 

template <class T>
void Function(T &o)
{
   o.DoSomething();
}
class A
{
public:
   void DoSomething() {}
};
class B
{
public:
   void DoSomething() {}
};
void Test()
{
   A a;
   B b;
   Function(a);
   Function(b);
}

As long as the object you use has a 'DoSomething()' method with a matching signature (parameters at least) it should compile fine.

Edited by Nanoha

Share this post


Link to post
Share on other sites

Lets say i want a function to take a class as a parameter but i do not know the class in advance or i want it to be able to take different classes, how can i do it?

Class object;
AnotherClass antherObject;


function(Class &object)
{
   //DO STUFF
};

How can i make "function" take "AnotherClass" as a parameter as well? Overloading is a solution but i would still have to copy the code again. Is there another way?

 

The question is kind of vague to get a good answer.

Sure, you can use templates or polymorphism - but is that really what you need?

 

If you have a specific scenario in mind, I'd advise you to post that and see what response you get then.

Sometimes you might only need to pass a few common variables instead of the class itself.

 

Edit:

Oops, I realised now that Lactose wrote the same thing :)

Edited by irbaboon

Share this post


Link to post
Share on other sites

Like other people have said this question is very vague. I am suprised though that no one mentioned you could pass a void*. This allows you to pass the address of any class. However if you want to actually use this class you will need to convert it to something useful with casts.

Share this post


Link to post
Share on other sites

Like other people have said this question is very vague. I am suprised though that no one mentioned you could pass a void*. This allows you to pass the address of any class. However if you want to actually use this class you will need to convert it to something useful with casts.


Most likely because (at least in C++) there is almost no reason to ever pass void pointers. They are a definite sign of code smell and if the casts at both ends of the pointer are not exactly the same it results in undefined behavior.

As suggested above by several other users it's much better to use a known interface and inheritance or templates and let the type system work with you to prevent errors. Edited by SmkViper

Share this post


Link to post
Share on other sites


I am suprised though that no one mentioned you could pass a void*.

I mentioned this as an option in the thread's first reply.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement