Bonsoir il y a certaines notion que j'ai pas trop saisie en c++
comment faire un copie constructor d'une class qui hérite d'une class?
par example ma class unit contient plusieurs variables et ma class marine hérite de la classe unit en public
donc dans la classe marine je vais avoir un constructor puis une copie le probleme c'est que je ne sais pas comment initialiser les valeurs de la classe unit pour le constructor copy de marine
ma classe unit :
#ifndef _UNIT_H_
#define _UNIT_H_
#include <iostream>
#include <string>
class unit
{
protected:
std::string name;
std::string type;
int posX;
int posY;
int dam;
int cHP;
int mHP;
public:
explicit unit(std::string name, std::string type, int posX, int posY, int dam, int cHP, int mHP);
virtual ~unit();
unit(const unit &ref);
unit &operator=(const unit &ref);
unit();
std::string getName() const;
std::string getType() const;
int getPosX() const;
int getPosY() const;
int getDam() const;
int getCHP() const;
int getMHP() const;
unit &operator+=(int tamer);
unit &operator-=(int tamer);
};
std::ostream &operator<<(std::ostream &os, unit &);
#endif /* !_UNIT_H_ */
mon.h marine :
#ifndef _MARINE_H_
#define _MARINE_H_
#include <string>
#include <iostream>
#include "unit.h"
class marine : public unit
{
public:
marine(const std::string &name, int posX, int posY);
virtual ~marine();
marine(const marine &);
marine();
marine &operator=(const marine &);
void move(int x, int y);
void stimpack();
};
#endif /* !_MARINE_H_ */
dans unit.cpp je fais directement
unit::unit(const unit &ref) : name(ref.name), type(ref.type), posX(ref.posX), posY(ref.posY), dam(ref.dam), cHP(ref.cHP), mHP(ref\
.mHP)
{
}
//dans le marine.cpp
marine::marine(const marine &ref) : name(ref.name), type(ref.type) etcc..
j'ai comme erreur :
marine.cpp: In copy constructor ‘marine::marine(const marine&)’:
marine.cpp:38:37: error: class ‘marine’ does not have any field named ‘name’
marine::marine(const marine &ref) : name(ref.name)
les variables sont dans la classe parent et je veux les initialiser en list pour la copy constructor de la class marine