Simply, I am unable to call non-inherited methods from an object in a Vector.
I'm using Qt Creator 2.7.0 which to my knowledge doesn't use the full C++11 yet (?) - I'm unsure if this is what is causing the following problem (although I'm very sure it's me not the IDE/Compiler):
I have 3 classes which inherit from the base class and have an additional primitive/getter/setter each.
Simply, my classes look like:
class A
{
public:
virtual std::string getName() = 0 ;
virtual void setName(std::string) = 0 ;
virtual int getNumber() ;
virtual void setNumber(int) ;
protected:
std::string name ;
int number ;
}
class B : public A
{
public:
std::string getName() ;
void setName(std::string) ;
int getNumber() ;
void setNumber(int) ;
std::string getEmail() ;
void setEmail(std::string) ;
protected:
std::string email ;
}
In my main I have a Vector of pointers, i.e.:
std::vector<A*> contacts ;
//Add Pointers to Vector
A *a ;
B *b ;
contacts.push_back(a) ;
contacts.push_back(b) ;
I then Check Object Class Type, to ensure it's of class type B.
if (dynamic_cast<B*>(contacts.at(1)) != NULL) //nullptr not working in Qt yet
{
I can access the getters & setters of Class A, but not B:
std::string name = contacts.at(1)->getName() ; //Works
std::string email = contacts.at(1)->getEmail() ; //Compiler Error: 'class A' has
//no member named 'getEmail'
}
The Error ('class A' has no member named 'getEmail') is happening at compile time and not at run-time.
I can't see it being Object Slicing as this should all be polymorphic, should I be using some type of C++ Casting?
Any help or kick in the right direction would be appreciated, thanks.