错误的原因是对象切片吗?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
std::vector< std::unique_ptr ; >
就是这样:一个充满基数指针的向量。并且您无法通过基类指针/引用访问派生类的内容 - 即使派生类的对象位于这些指针/引用后面。这与此没有什么不同:
这并不意味着在
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:
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.不,您有一个指向基类
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 namedsomeInfo
. That pointer still points to aSubInfo
, 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.它说的是真的
错误:'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 returnsomeInfo