i have this issue but i can't solve it. I only know the InitData function is 100% correct. However i try to find how need to create my classes to make this code work (without do any change to the InitData function)
class Bird
{
public:
int data[10];
int color;
Bird() {
ZeroMemory(data, 10 * 4);
color = 0;
}
Bird(int* _data, int _color) {
memcpy(data, _data, 10 * 4);
color = _color;
}
//more code
};
class Animal : Bird
{
public:
int data[10];
Animal() {
ZeroMemory(data, 10 * 4);
}
Animal(int* _data) {
memcpy(data, _data, 10 * 4);
}
//more code
};
Animal* InitData(int* _data, bool isBird) {
if (isBird) {
Bird* bird = new Bird(_data, 0);
return bird;
}
else {
Animal* animal = new Animal(_data);
return animal;
}
return nullptr;
}
Maybe has do something with virtual classes?
The compiler gives me error 'E0120 return value type does not match the function type', inside InitData at this line 'return bird;'. I use visual studio 2019, Release(x64), C++17
SOLVED: so i inherit wrong class. Need inherit Animal to Bird. So need change 'class Bird' to 'class Bird : public Animal' and 'class Animal : Bird' to 'class Animal'
Thanks everyone!
Your inheritance is the wrong way around. Also you don't need to replicate the data in the derived class, because this is what the baseclass is for, so you should call the appropriate constructor, instead of duplicating the code from your baseclass.
class Animal
{
public:
int data[10];
Animal()
{
ZeroMemory(data, sizeof(int)*10);
}
Animal(int* _data)
{
memcpy(data, _data, 10 * sizeof(int));
}
//more code
};
class Bird : public Animal
{
public:
int color;
Bird()
{
color = 0;
}
Bird(int* _data, int _color)
: Animal(_data)
{
color = _color;
}
//more code
};