- #1
ChrisVer
Gold Member
- 3,378
- 464
Hi,
Is it possible to declare an array list or vector in C++ without knowing beforehand the type of objects it may contain? (in fact it may contain any kind of object)
For example if I have the following classes (maybe there are some mistakes overall, but my main concern is how to deal with the vector TYPE which should either be an Orange or an Apple):
Is it possible to declare an array list or vector in C++ without knowing beforehand the type of objects it may contain? (in fact it may contain any kind of object)
For example if I have the following classes (maybe there are some mistakes overall, but my main concern is how to deal with the vector TYPE which should either be an Orange or an Apple):
Code:
class Fruits{
private:
int m_Nfruit=0;
//HERE NEED HELP
vector<TYPE??> fruitContainer;
public:
Fruits() { m_Nfruits += 1; };
virtual ~Fruits() { };
void Print() const {
for( std::vector<TYPE>::iterator it = fruitContainer.begin() ; it!=fruitContainer.end() ; it++){
it->Print() // should print either Apple or Orange
}
};
};
class Orange : public Fruits{
private:
int m_Norange = 0;
public:
Orange(){
Fruit::Fruits()
m_Norange +=1 ;
fruitContainer.push_back(*this)
};
virtual ~Orange(){};
void Print() const {
std::cout << "Orange" << std::endl;
};
}
class Apple : public Fruits{
private:
int m_Napple = 0;
public:
Apple(){
Fruit::Fruits()
m_Napple +=1 ;
fruitContainer.push_back(*this);
};
virtual ~Apple(){};
void Print() const {
std::cout << "Apple" << std::endl;
};
}