简体   繁体   中英

“Override” constructor and call grandparent constructor

I am using a class in a library that we'll call LibraryClass<BaseClass> where LibraryClass<> inherits from its template like so:

template<class BaseClass>
class LibraryClass
:
    public BaseClass
{
...

This class does exactly what I want, however there is a bug in the constructor. I am trying to create a derived class like this:

template<class BaseClass>
class MyClass
:
    public LibraryClass<BaseClass>
{
...

and then just add a new constructor. However, in the constructor MyClass() I need to call the constructor BaseClass() which is not the direct parent. As stated above, I don't want to call LibraryClass() as it has a bug.

How should I create this new class with a just a new constructor?

I doubt there is standards-compliant way to bypass the parent constructor in the general case (if such a thing existed, what would ensure that the parent sub-object is left in a usable state?)

IMO the best way to fix the bug is by changing LibraryClass .

If you can't do that, you could copy LibraryClass and fix the bug in your copy. This has obvious and potentially very significant downsides.

Another potential alternative is to call the buggy constructor, and then fix up the object in MyClass::MyClass to work around the bug. This approach may or may not work for your case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM