11

Every object in .NET inherits (directly or indirectly) from the common root base "Object". Is there such a common object root in C++? How do I pass any object to a function?

public void DoSomeStuff(object o) { ... }

EDIT: To clarify, the purpose: In that function I want to invoke a pointer to member function. For that I need the object instance and pointer to the required function. To simplify readability I want to make a wrapper containing the both required information. I'm not sure if that is the best way, but it is the background idea.

Littlesmith
  • 123
  • 1
  • 7
  • 4
    May I ask what you're going to do with this fully generic object? – Chris Lutz Mar 08 '10 at 07:06
  • Thanks to all who gave valuable ideas. I was also thinking of templates, but then the class that uses this template wrapper would need to know the correct template parameter -> need to no the type in compile time. Terry Mahaffey suggests using boost::any, but that brings the dependency on boost library (so long using just the standard library). My good friend also reminded me, that in Java there is no concept of delegates (what this is all about) either. The design is solved with interfaces instead. With this reminder I figured out it is probably the best solution in my case. – Littlesmith Mar 09 '10 at 22:50

7 Answers7

13

There is no common base class; but using a something like boost::any or more generally a template based approach is preferred over a void*.

Terry Mahaffey
  • 11,775
  • 1
  • 35
  • 44
7

There is no common root class. Use either void* to pass any object into a function, or better define some base class.

Corwin
  • 575
  • 2
  • 9
5

Template functions are present and avoid the need for such root parent of all classes.

template <class T>
void DoSomeStuff(T const &t) {
    // Do the stuff with t...
    t.callTheFunction();
}

If all your objects have a member function callTheFunction(), then you got the exactly same behavior than having a root base class, with the same requirement (all your classes have a function with that name).

In addition, you got the additional benefit of being able to specialize the template function DoSomeStuff() for some classes that are not yours, and could not inherit your virtual member function.

Didier Trosset
  • 36,376
  • 13
  • 83
  • 122
  • You can't use `DoSomeStuff(T const& t)` as a member function, though. That is, you can't call it like this: `t.DoSomeStuff()`. A root base class would allow you to do that. Alternatively, this could be done by writing a generic dot operator but for now, overloading the dot operator is not possible in C++. – Mark Mar 02 '18 at 15:18
  • Of course, it is possible. However, having all your objects inheriting from a root base class creates unnecessary coupling within your source code. It's IMHO not worth the trade for the `t.` syntactic sugar. – Didier Trosset Mar 02 '18 at 17:45
2

For that I need the object instance and pointer to the required function.

That sounds a lot like "delegates". First, you definitely will need to define a common base class for all the objects that you want to call. In C++ you can use multiple inheritance if the object already belong to some other hierarchy.

Then have a read through Member Functions and the Fastest Possible C++ Delegates which is an excellent in-depth article on the topic of delegates (which are an object and member function pointer bound together). Using the header file described in that article, you can create delegates and easily call them just like regular function pointers.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
0

Sorry - there is no root base object in C++. But you can define your own for all your classes.

Warpin
  • 6,971
  • 12
  • 51
  • 77
0

There is no common base class in C++. However, there are already libraries that allow you to call member functions as delegates. You can try using boost::function together with boost::bind or boost::lambda.

Bojan Resnik
  • 7,320
  • 28
  • 29
0

You'd at least depend on a minimum c++ runtime if there were a root object implemented in c++. This is undesirable sometimes.

sof
  • 9,113
  • 16
  • 57
  • 83