简体   繁体   中英

Overriding a template argument in grandchild class in C++?

I have the following hierarchy

template<class SettingsType>
    class TemplatedBase;

class A : public TemplatedBase<SettingsTypeA>;

class B : public A;

class SettingsTypeA;

class SettingsTypeB : public SettingsTypeA;

Is there a way to override the template specialization in B so it inherits from A while having the template specialization SettingsType set to SettingsTypeB

No. Template is not a class. It's a template .

TemplatedBase<SettingsTypeA> is a specific instantiation of the template - it's just the regular Foo class. There is nothing special about it beyond that.

So your question is the equivalent of asking if you can change C for D in below example:

class A : public C;

class B : public A;

A simple change would be to template A while giving it a default:

template<typename T = SettingsTypeA>
class A : public TemplatedBase<T> {};

and then you can use the needed type when B inherits from A :

class B : public A<SettingsTypeB> {};

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