Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Root base class in C++

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.

like image 443
Littlesmith Avatar asked Mar 08 '10 06:03

Littlesmith


People also ask

What is a root class?

A root class inherits from no other class and defines an interface and behavior common to all objects in the hierarchy below it. All objects in that hierarchy ultimately inherit from the root class. A root class is sometimes referred to as a base class.

What is base class in C?

A base class is a class, in an object-oriented programming language, from which other classes are derived. It facilitates the creation of other classes that can reuse the code implicitly inherited from the base class (except constructors and destructors).

What is the base class of all classes in C#?

Object is the base class for all data types in C#. The Object Type is the ultimate base class for all data types in C# Common Type System (CTS).


4 Answers

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*.

like image 81
Terry Mahaffey Avatar answered Oct 03 '22 13:10

Terry Mahaffey


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

like image 31
Corwin Avatar answered Oct 03 '22 13:10

Corwin


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.

like image 39
Didier Trosset Avatar answered Oct 03 '22 15:10

Didier Trosset


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.

like image 26
Greg Hewgill Avatar answered Oct 03 '22 13:10

Greg Hewgill