How do I make a template class that inherits a non-template class and one based on the template?

I have 2 classes; a World (structure) and another class that is defined by a template parameter. I want to create a 3rd class called Trainer<Scheme> which automatically creates an instance of World and of the Scheme type. So far I've got this:

1
2
3
4
5
template <typename Scheme>
class Trainer : struct World, class Scheme {
	Trainer(const char* []); // build the trainer class
	~Trainer(); // destroy the trainer class
};


If I now create a new instance of Trainer<Scheme>, which constructor of World and Scheme is it going to call? How can I customise this? I'm a little confused on this matter and wasn't really able to find specific info on this online, would appreciate some guidance!
Well, I don't see why you are using a struct. You should use classes. I know that they function almost the same, but let's remember that C++ is about classes, not structs. Also, does that compile? In C++ you have to state the inheritance modifier (public/protected/private). I don't see the modifier in your code.

As to the constructors called, it will call the default constructors, I guess. You can quickly test using ideone.com, if you like. Just output text on each constructor. If you need a different constructor you simply call it using the initializer list.
closed account (3TXyhbRD)
This is the correct code for what you want to do:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
 
class B1{
public:
    B1() {std::cout<<"B1()"<<std::endl;}
};
 
class B2{
public:
    B2() {std::cout<<"B2()"<<std::endl;}
};
 
template <typename Another>
class D : public B1, public Another{
public:
    D() {std::cout<<"D()"<<std::endl;}
};
 
int main(){
    D<B2> d;
    return 0;
}


As for constructors. If you don't specify a constructor per class to call it will call the default constructors of all classes with respect to the inheritance list. For instance the above code will output:
B1()
B2()
D()


If you add another constructor to B1 (or B2) and specify in the initializer list which constructor to call, it will only work for that class while the D constructor will still call the default constructor for B2.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
 
class B1{
public:
    B1() {std::cout<<"B1()"<<std::endl;}
 
    B1(int) {std::cout<<"B1(int)"<<std::endl;}
};
 
class B2{
public:
    B2() {std::cout<<"B2()"<<std::endl;}
};
 
template <typename Another>
class D : public B1, public Another{
public:
    D(): B1(1) {std::cout<<"D()"<<std::endl;}
};
 
int main(){
    D<B2> d;
    return 0;
}

Will output:
B1(int)
B2()
D()
ausairman wrote:
I want to create a 3rd class called Trainer<Scheme> which automatically creates an instance of World and of the Scheme


In that case, public inheritance is the wrong solution. Use composition: make a member of Trainer of type World and a member of Trainer of type Scheme. Or, if Trainer needs to access protected or virtual methods of World or Scheme, use private inheritance.

As far as constructor calls go, it's the same either way, whatever constructors you call from the init list of the Trainer's constructor are the ones that will be called.
Last edited on
Cubbi, I think the OP simply doesn't know exactly what his/her words mean. I think the OP understands polymorphism as "creating instances". Is not that he/she really needs a discrete instance of each of the classes.
Topic archived. No new replies allowed.