Hi, I am new to C++, picking it up to dabble in some windows mobile programming.
I am using the excellent documentation on this site, and I have come up against something I do not see the solution for, so I thought I would ask.
I am writing an app to display multiple widgets on the screen, pulled from an INI or XML file. I have a Widget class with member variables of x and y coordinates and name of the widget.
Then I have derived classes for different kinds of widget (eg clock, image, etc), each of which would have their own member variables (eg an image widget would have filename but clock would not) that the base Widget class does not.
I want to effectively put all the Widgets in a global array for rendering, regardless of which sublcass they are, so I was thinking an array of pointers, but obviously I would need it to be of Widget* type, because an Image* type could not hold a Clock* type.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
class Widget {
public:
LPCWSTR name;
int x, y; // Screen loc of widget
};
Widget* g_Widgets;
class Image: public Widget {
public:
LPCWSTR filename;
IImage* pImage;
};
class Clock: public Widget {
public:
LPCWSTR fontname;
};
g_Widgets = new Image[];
g_Widgets[0].filename=L"TEST"; // DOES NOT WORK - error C2039: 'filename' : is not a member of 'Widget'
|
If I change the declaration to:
Image* g_Widgets;
it works, but obviously I could not include a widget of subclass Clock in the g_Widgets array.
I am probably being a dumb newb, could someone please explain to me where I am going wrong?