错误的原因是对象切片吗?

发布于 2024-09-11 07:28:44 字数 1151 浏览 3 评论 0原文

g++ -std=gnu++0x main.cpp
In file included from main.cpp:6:0:
CustArray.h: In constructor 'CustArray::CustArray()':
CustArray.h:26:32: error: 'class Info' has no member named 'someInfo'
make: *** [all] Error 1
/*
 * Info.h
 *
 */

#ifndef INFO_H_
#define INFO_H_

class Info
{
    friend class CustArray;
};

#endif /* INFO_H_ */


/*
 * SubInfo.h
 *
 */

#include "Info.h"

class SubInfo : public Info
{

const int someInfo;

public:
    SubInfo(const int someInfo):someInfo(someInfo){}
};

#include <vector>
#include <memory>
#include "Info.h"
#include "SubInfo.h"

template<typename T>
struct ptrModel
{
    typedef std::unique_ptr<T> Type;
};

//Alias shortener.
typedef ptrModel<Info>::Type ptrType;

class CustArray
{

protected:
    std::vector<ptrType> array;

public:
    CustArray()
    {
        ptrType el_init(new SubInfo(1));
        array.push_back(std::move(el_init));
        int someInfo = (*(array[0])).someInfo;
    }

};

/*
 * main.cpp
 *
 */

#include "CustArray.h"
#include <vector>

int main()
{
    CustArray seq;

    return 0;
}
g++ -std=gnu++0x main.cpp
In file included from main.cpp:6:0:
CustArray.h: In constructor 'CustArray::CustArray()':
CustArray.h:26:32: error: 'class Info' has no member named 'someInfo'
make: *** [all] Error 1
/*
 * Info.h
 *
 */

#ifndef INFO_H_
#define INFO_H_

class Info
{
    friend class CustArray;
};

#endif /* INFO_H_ */


/*
 * SubInfo.h
 *
 */

#include "Info.h"

class SubInfo : public Info
{

const int someInfo;

public:
    SubInfo(const int someInfo):someInfo(someInfo){}
};

#include <vector>
#include <memory>
#include "Info.h"
#include "SubInfo.h"

template<typename T>
struct ptrModel
{
    typedef std::unique_ptr<T> Type;
};

//Alias shortener.
typedef ptrModel<Info>::Type ptrType;

class CustArray
{

protected:
    std::vector<ptrType> array;

public:
    CustArray()
    {
        ptrType el_init(new SubInfo(1));
        array.push_back(std::move(el_init));
        int someInfo = (*(array[0])).someInfo;
    }

};

/*
 * main.cpp
 *
 */

#include "CustArray.h"
#include <vector>

int main()
{
    CustArray seq;

    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

稍尽春風 2024-09-18 07:28:44

std::vector< std::unique_ptr; > 就是这样:一个充满基数指针的向量。并且您无法通过基类指针/引用访问派生类的内容 - 即使派生类的对象位于这些指针/引用后面。

这与此没有什么不同:

SubInfo si(1); 
Info& info = si;
info.someInfo; // won't compile

这并不意味着在 info 后面没有派生类的对象。有。但是除了通过基类接口可用的内容之外,您无法访问其中的任何内容。这是基本的面向对象。

An std::vector< std::unique_ptr<Base> > is just that: a vector filled with pointers to bases. And you cannot access derived class' content through base class pointers/references - even if objects of derived classes are behind those pointers/references.

This is no different from this:

SubInfo si(1); 
Info& info = si;
info.someInfo; // won't compile

That doesn't mean that behind info there isn't a derived class' object. There is. But you cannot access anything of it but what's available through the base class interface. That's basic OO.

猥琐帝 2024-09-18 07:28:44

不,您有一个指向基类 Info 的指针,正如编译器所说,它没有名为 someInfo 的成员。该指针仍然指向 SubInfo,但您无法通过基类指针访问派生类成员。

如果您确实需要访问该字段,则需要使用dynamic_cast进行向下转换。请务必检查结果以确保演员阵容有效。

No, you have a pointer to the base class Info, which as the compiler says, doesn't have a member named someInfo. That pointer still points to a SubInfo, but you can't access derived class members through a base class pointer.

If you really need access to the field, you would need to use dynamic_cast to downcast. Be sure to check the result to ensure the cast is valid.

随风而去 2024-09-18 07:28:44

它说的是真的

错误:'class Info'没有名为'someInfo'的成员

事实并非如此。您无法像您尝试的那样以多态方式访问子成员。

您将需要 Info 类中的虚拟成员函数,例如 GetSomeInfo() ,它可以是纯虚拟的,也可以在基类中返回一些有趣的内容。然后在子类中它可以返回someInfo

What it's saying is true

error: 'class Info' has no member named 'someInfo'

It doesn't. You can not access child members in a polymorphic way like you are attempting.

You will need a virtual member function in the class Info such as GetSomeInfo() which can either be pure virtual or return something interesting in the base class. Then in the child class it can return someInfo

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文