polymorphism and templates

Write your question here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
  #include <iostream>
#include <string>
using namespace std;


template<class T>
class Base{
private:
	static int myVar;
public:
	static int getMyVar(){
		return Base::myVar;
	}
	virtual void type(){
		cout << "NONE";
	}
};



class Empty : public Base<Empty>{
public:
	void type(){
		cout << "EMPTY";
	}
};

class Ant : public Base<Ant>{
	void type(){
		cout << "ANT";
	}
};

class Beetle : public Base<Beetle>{
	void type(){
		cout << "BEETLE";
	}
};



int main(){

	Base *Organism = new Base < Empty > ; // doesnt work because missing argument

	Organism->type();

	delete Organism;

	*Organism = new Base < Ant > ;

	cin.ignore();

	return 0;
}


I figured out how to "inherit" static instances but now I cant use that base class. How to avoid this problem?
Last edited on
Why do you want Base to be a class template? Base<Empty> and Base<Ant> are two different classes so you will not be able to have a pointer that can point to both of them (except if you make them inherit from some common base class).
Last edited on
I want child classes to already have their static variables Beetle::myVar, Ant::myVar etc. without declaring them in those clases. This is how I figured out it can be possible. How can I do it? :)
Last edited on
The value of myVar is always going to be the value at which it was most recently assigned and it will be consistent throughout all the classes derived from your Base. That meaning if your Base assigns it the value of 5, every class that is derived from Base will have myVar at 5 until it is changed(this will affect every instance of every derived class).

If that's what you're trying to accomplish? I don't really follow what you're trying to do.
Last edited on
If what you just said Im doing, then no I dont want to accomplish it.

I have:
1
2
3
4
5
6
7
8
9
10
11
class Base{
     static int myVar;
};

class ChildOne : Base{
// has member myVar, but not same as in Base class
};

class ChildTwo : Base{
// has member myVar, but not same as in Base class or ChildOne class
};


I cant change values of these instances and they dont affect different classes.
That is what I want to accomplish. :) Im now stuck with this for 3rd hour. :(

And after that I want base class have virtual functions and use base class variable as pointer to extended classes.
 
Base *ptr = new ChildClass;
Last edited on
Note that you can't declare a pointer to Base without specifying the template argument like on line 44.
 
Base<Empty> *Organism = new Base<Empty>;

But that means you will not be able to use the Organism pointer to point to other Base types like Base<Ant>.
 
Organism = new Base<Ant>; // error: A Base<Empty>* can't point to a Base<Ant> 

Maybe you should split the your Base in two. One base class without templates and one class template that handles the static variable.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Base{
public:
	virtual ~Base(){} // Base class should always have virtual destructor
	virtual void type(){
		cout << "NONE";
	}
};

template<class T>
class StaticVar{
private:
	static int myVar;
public:
	static int getMyVar(){
		return StaticVar::myVar;
	}
};

And then your classes can inherit from two classes like this:
1
2
3
4
5
6
class Empty : public Base, public StaticVar<Empty>{
public:
	void type(){
		cout << "EMPTY";
	}
};

After these changes it should work.
1
2
3
4
5
6
7
Base* Organism = new Base;
Organism->type(); // prints "NONE"

delete Organism;

Organism = new Ant;
Organism->type(); // prints "ANT" 
Thank you so much. You are the best.

ps. change Empty to Ant.
Last edited on
Topic archived. No new replies allowed.