I should specify that I'm a bit new to OOP.
I'm tying to make a vector of type pointer to Person that has a method GetName() and access a method GetSpg() from my Player class that derives Person. I get an error "GetSpg() is not a member of Person". My question would be: is there any way to access both functions from the vector so that if it points to a Person to not show that method but if it is to do so?
Here is my code:
#ifndef _PERSON_H
#define _PERSON_H
#include <iostream>
#include <algorithm>
typedef std::pair<std::string, std::string> StrPair;
class Person :private StrPair
{
public:
Person(const std::string& fn = "none", const std::string& sn = "none") :StrPair(fn,sn){};
virtual void Update(const std::string& fn, const std::string& sn){ StrPair::first = fn; StrPair::second = sn; };
virtual const StrPair& GetName(){ return (const StrPair&)(*this); };
};
#endif
typedef std::pair<int, int> IntPair;
class Jucator: public Person, private IntPair
{
std::string tip;
int spg;
int average;
public:
Jucator(const std::string& fn = "none", const std::string& sn = "none",
const std::string& t = "", const int& _spg = 0, const int& _avr = 0,
const int& _g = 0, const int& _r = 0) :Person(fn, sn),tip(t),spg(_spg),average(_avr),IntPair(_g,_r){};
virtual void Update(const std::string& fn, const std::string& sn, const std::string& t, const int& _spg, const int& _avr,
const int& _g, const int& _r){
Person::Update(fn, sn); tip = t; spg = _spg; average = _avr; IntPair::first = _g; IntPair::second = _r;
};
virtual const int& GetSpg(){ return spg; };
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…