Originally Posted by
laserlight
You don't even need a cast since a Decal is an Image and a Primitive is an Image.
Hmm, okay. In theory I would think that is how it should be, but I get "error: ‘Image’ is an inaccessible base of ‘Primitive’" when I try to compile the below [subsequently corrected] without the cast. I should set myself out some simple C++ specific examples -- otherwise I tend to use a quick hit and miss to deduce what rules apply in which language when what I think "in theory should be" goes wrong.
@Shokwav WRT to accessing arrays, you can pass a reference to them internally too:
Code:
#include <vector>
using namespace std;
class Image {
public:
virtual ~Image () {}
};
class Decal : public Image {};
class Primitive : public Image {};
class Frame {
private:
vector<Decal*> decals;
vector<Primitive*> primitives;
void processImage (Image *img, vector<Image*> &list) {
list.push_back(img);
// other stuff common to all to all Image derived types
}
public:
void process (Decal *dec) {
processImage (dec, (vector<Image*>&)decals);
// stuff particular to Decals
}
void process (Primitive *prim) {
processImage (prim, (vector<Image*>&)primitives);
// stuff particular to Primitives
}
};
int main(void) {
Decal *d = new Decal();
Primitive *p = new Primitive();
Frame *f = new Frame();
f->process(p);
f->process(d);
return 0;
}
If laserlight can straighten out the casting issue for me, I now have a C++ specific example of...something...
Originally Posted by
laserlight
You should store a vector of (smart) pointers. If you store a vector of Image objects, there is type slicing and you lose the parts specific to each derived class.
I think my idea here gets around this, since the vectors are correctly typed.