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

Started by
9 comments, last by Ravyne 8 years, 5 months ago

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?

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.

Hello to all my stalkers.

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! :)

The usual approach is to use inheritance, make a common base class and use that as type for the function parameter.

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.

Interested in Fractals? Check out my App, Fractal Scout, free on the Google Play store.

The usual approach is to use inheritance, make a common base class and use that as type for the function parameter.

Only if the two classes have similar behavior (at least in the big picture). If they just have a similar interface, and aren't otherwise related, a templated function is what I'd prefer.

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 :)

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.

If this post or signature was helpful and/or constructive please give rep.

// C++ Video tutorials

http://www.youtube.com/watch?v=Wo60USYV9Ik

// Easy to learn 2D Game Library c++

SFML2.2 Download http://www.sfml-dev.org/download.php

SFML2.2 Tutorials http://www.sfml-dev.org/tutorials/2.2/

// Excellent 2d physics library Box2D

http://box2d.org/about/

// SFML 2 book

http://www.amazon.com/gp/product/1849696845/ref=as_li_ss_tl?ie=UTF8&camp=1789&creative=390957&creativeASIN=1849696845&linkCode=as2&tag=gamer2creator-20

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.


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.

Hello to all my stalkers.

This topic is closed to new replies.

Advertisement