Classes derived from std:: classes

Sorry if this is a question answered in the documentation, but I can’t find it.

I refer you to the sample code and output in the attached tarball. The code is based on an example from the tutorials. I define three classes: MyClass1, MyClass2, and MyClass3. They differ only in that MyClass1 does not derive from another class, MyClass2 derives from std::string, and MyClass3 derives from std::vector.

The code compiles and links with no errors (using Root 5.17/02). However (see file “myoutput” in the above archive):

  • If I start myroot and enter ‘MyClass1::’ followed by a tab, Root prints the public methods of MyClass1 as I would expect. Likewise MyClass2, including the methods inherited from std::string. But for MyClass3, only the methods of MyClass3 are printed, not the ones inherited from vector.

  • If I start myroot, enter ‘MyClass1 x1’ and similarly for MyClass2 and MyClass3, and then enter ‘x1.’ followed by a tab, Root prints the public methods of MyClass1 as I would expect. But for MyClass2 I get the message ‘problem determining class of “x2” / variable “x2.” not defined.’ However, ‘x2.c_str()’ works as expected. For MyClass3, “x3.” and tab causes only the methods of MyClass3 to be printed, not the ones inherited from vector, and ‘x3.resize(3)’ fails with the message ‘Error: Can’t call MyClass3::resize(3) in current scope (tmpfile):1:’. The Print method (not inherited) works for all three classes, however.

I assume these problems with MyClass2 and MyClass3 have to do with bad dictionaries for those classes, but how do I fix them?
myclasses.tar.gz (20.1 KB)

I am sorry to disappoint you, but the idea to have

[quote=“rsholmes”]
MyClass2 derives from std::string, and MyClass3 derives from std::vector[/quote]
is entirely wrong :frowning:

STL containers are not meant to be derived from! They have to be used as primitives, as it is.
STL containers are not designed in such a way as to be extensible via inheriting from them as base classes. They don’t have even a virtual destructor (no virt. methods at all) and only this leads to an undefined behaviour of your code: parashift.com/c+±faq-lite/v … l#faq-20.7

If you need to extend functionality of containers or something like that. In this case I would recommend to wrap vector or string or STLever, but not derive from it - use composition classes.

P.S. I wouldn’t also recommend to use “using namespace” in header files. Very uncomfortable and aggressive for users of your interfaces.

Thanks – wasn’t at all aware of that.

:wink:
this is why forums exist