How to use interface types in lists

Hi,

I'm creating a C++ gui client with FLTK and I need a little help with lists. I have a container that is supposed to host multiple widgets. The idea is to have a widget/container that hosts different gauge widgets. I have made two widgets a dialgauge and a bargauge. There inherit an abstract class that has common stuff. They also inherit the FLTK widget class.

So, because the user can choose which type of gauges they want I need a list that can store these. I'm a Java developer so in that world I would make an interface that the gauges implement and use that as a type in the list:

interface Gauge
DialGauge implements Gauge
BarGauge implements Gauge
Container instance list<Gauge> gaugelist
gaugelist.add(new DialGauge);
gaugelist.add(new BarGauge);

I know C++ does not have interfaces, but is there a way to accomplish this kind of behaviour? I could make a list for every gauge type, but seems a bit silly...

Does boost library have something I could use for this?

any help appreciated! You probably get lots of annoyances from us Java people ;). Thanks!
Whilst C++ does not have the interface keyword, you certainly can create interface classes. They are called "pure abstract classes":
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
#include <list>

// Interface
class Gauge
{
public:
	virtual ~Gauge() {} // virtual destructor (required)

	virtual void methodA() = 0; // pure virtual function
	virtual void methodB() = 0; // pure virtual function
};

// Implementation
class DialGauge
: public Gauge
{
public:
	virtual void methodA()
	{
		// implementation
	}
	virtual void methodB()
	{
		// implementation
	}
};

// Implementation
class BarGauge
: public Gauge
{
public:
	virtual void methodA()
	{
		// implementation
	}
	virtual void methodB()
	{
		// implementation
	}
};


int main()
{
	std::list<Gauge*> gaugelist;

	gaugelist.push_back(new DialGauge);
	gaugelist.push_back(new BarGauge);

	// ... etc ...
}
Thank you so much! I will try this out immediately.
Topic archived. No new replies allowed.