简体   繁体   English

C ++ / Qt对象到向量和返回对象

[英]C++/Qt Object to Vector & Back to Object

Simply, I am unable to call non-inherited methods from an object in a Vector. 简单地说,我无法从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): 我正在使用Qt Creator 2.7.0据我所知还没有使用完整的C ++ 11(?) - 我不确定这是否是造成以下问题的原因(虽然我很确定这是我的不是IDE /编译器):

I have 3 classes which inherit from the base class and have an additional primitive/getter/setter each. 我有3个类继承自基类,每个类都有一个额外的原语/ getter / setter。

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, ie: 在我的主要我有一个指针向量,即:

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. 然后我检查对象类类型 ,以确保它是类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: 我可以访问A类的getter和setter,但不能访问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. 错误('类A'没有名为'getEmail'的成员)在编译时发生,而不是在运行时发生。

I can't see it being Object Slicing as this should all be polymorphic, should I be using some type of C++ Casting ? 我不能看到它是对象切片,因为这应该是多态的,我应该使用某种类型的C ++ Casting吗?

Any help or kick in the right direction would be appreciated, thanks. 任何帮助或踢正确的方向将不胜感激,谢谢。

You need to use a dynamic_cast to get a pointer to the derived type. 您需要使用dynamic_cast来获取指向派生类型的指针。 You are not actually doing that, you are calling a B method on an A* : 你实际上并没有这样做,你在A*上调用B方法:

contacts.at(1)->getEmail() ; // look, no dynamic_cast

You would need to do the equivalent of this: 你需要做相同的事情:

B* b = dynamic_cast<B*>(contacts.at(1));
std::string email = b ? b->getEmail() : "N/A";

contacts pertains to A which is base class.! 联系人属于A类,它是基类。 You've to perform the operation on derived one.! 你必须在派生的一个上执行操作。

Instead of contacts.at(1)->getName use vector<B*> b->getName . 而不是contacts.at(1)->getName使用vector<B*> b->getName

Hope this clears :) 希望这清除:)

Your contacts vector contains A*. 您的contacts矢量包含A *。 C++ verifies types at compilation so there is no problem when executing the code. C ++在编译时验证类型,因此在执行代码时没有问题。 Since contacts is of A* type, only methods of class A can be called. 由于contacts是A *类型,因此只能调用A类方法。 In your case, you want to cll a B method on an objct that might be a B , or not! 在你的情况下,你想在一个可能是B的objct上cll一个B方法! C++ is sure it is an A , but it might be a C (if class C inherits from A). C ++确定它是A ,但它可能是C (如果C类继承自A)。 Whay you want to do is to get the instance of the B object in the vector and cast it into a B so you can call the method: B* b = dynamic_cast<B*>(contacts.at(1)); 你想要做的是在向量中获取B对象的实例并将其转换为B,这样你就可以调用方法: B* b = dynamic_cast<B*>(contacts.at(1)); noyw use the b to do whatever you want. noyw用b来做你想做的事。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM