为什么我无法将指针投射到类结构
Why i am not able to cast pointer to the class structure
我有一个Geometry
类
class Geometry
{
public:
std::string stdstrType;
bool bValid;
public:
Geometry()
Geometry( std::string strType , bool bValue )
Geometry(const Geometry &g)
~Geometry()
virtual void draw();
bool isValid();
void setValidState(bool bState);
virtual void Init();
std::string GetName();
};
这是Geometry
对象的基类,就像本例中Sphere
类一样
class Sph : public Geometry
{
public:
Sph( float radius , float segments );
~Sph();
void init();
void CleanUp();
void draw();
private:
float fRadius, fSegments;
bool isInited;
unsigned int m_VAO, m_VBO;
int iNumsToDraw;
SumShader shader;
bool isChanged;
};
我有一个包含不同Container
对象的树结构,Geometry
是Container
对象中的数据类型。
class Container
{
private:
std::string stdstrContainerName;
std::string stdstrPluginType;
Geometry Geom;
}
由于树中的每个项目都可以容纳一个圆形球体或矩形,因此我想使用几何图形的绘制函数来绘制Geometry
对象类型。 为此,当我尝试将任何Geometry
对象类型转换为Geometry
时,我收到错误。
Sph sphere(0.1 , 32);
Geometry *geom = &sphere;
Container cont("Sphere" , "SPHERE" , *geometry );
myModel->SetContainer(child, cont);
容器的构造函数
Container::Container( std::string strName, std::string strType, const
Geometry& geometry) : Geom(geometry)
{
stdstrContainerName = strName;
stdstrPluginType = strType;
}
void TreeModel::SetContainer(const QModelIndex &index, Container Cont)
{
TreeItem *item = getItem(index);
item->setContainer(Cont);
}
class TreeItem
{
public:
// Functions here
private:
QList<TreeItem*> childItems;
Container itemData;
TreeItem* parentItem;
};
1)这是正确的方法吗?
2) 如何将Geometry
对象强制转换为Geometry
指针?
Sph sphere();
您已经声明了一个返回 Sphere 对象的函数,并且不采用任何参数。
要向 Sph 声明一个对象,您只需编写
Sph sphere;
或Sph sphere{};
我猜你尝试了第一个,但它没有编译,所以你只是更改了签名"直到编译"。 您已经声明了一个自定义构造函数,这意味着编译器不再为您提供默认构造函数,因此您无法在不调用正确的构造函数的情况下声明变量(在您的情况下,它没有意义)。
此外,还有
Geometry *geom = new Geometry;
geom = &sphere;
您正在创建一个新的指针几何体,而不是立即泄漏它并重新分配给一个根本没有意义的球体几何体。
此外,您的所有方法在几何中都是公开的,这没有意义(为什么要将布尔值有效公开然后放入getter?
此外,类Container
将实例保存到基类,这将由于对象切片而给您带来问题,您需要使用指针或引用。
为了回答你的问题,你应该实例一个球体
Geometr* geom = new Sphere(1, 5); // random numbers
但我能说的最真实和诚实的事情是从头开始重写所有内容,在此之前,用一个更简单的例子来学习更多的尝试。
在问题中工作和实现想法的最小代码:
#include <iostream>
class Geometry
{
public:
Geometry(const std::string type) { stdstrType = type; }
std::string GetName() { return stdstrType; }
virtual void draw() {} ;
private:
std::string stdstrType;
};
class Sphere : public Geometry
{
public:
Sphere(float radius): Geometry("Sphere ") { fRadius = radius; }
virtual void draw() { std::cout << GetName() << fRadius << "n";}
private:
float fRadius;
};
class Container
{
public:
Container(std::string strName, Geometry* g)
{
stdstrContainerName = strName;
geometry = g;
}
void draw() { geometry->draw(); }
private:
std::string stdstrContainerName;
Geometry *geometry;
};
int main(int argc, const char * argv[]) {
Sphere sphere(0.1);
Geometry *geom = &sphere;
Container cont("Sphere container", geom);
cont.draw();
return 0;
}
Xcode 10.2.1:没有构建时/运行时问题。输出:
Sphere 0.1
Program ended with exit code: 0
相关文章:
- 如何在 C# 中映射双 C 结构指针?
- C++:映射结构中的结构指针
- C++:添加新结构时,结构指针向量中的所有元素都会更新
- 类的静态结构指针声明在C++
- 在两个.cpp文件之间定义全局类/结构指针
- 返回对常量结构(指针类型)成员的引用:明显的左值到右值转换
- 是否可以将无符号字符数组reinterpret_cast到仅包含C++中无符号字符成员的结构指针
- 如何在地图中使用结构指针
- 结构指针在"新"调用上给出已分配的地址?
- 铸造结构指针指向C 的工会指针
- c++ 结构指针在初始化为 NULL 时无法读取内存
- 函数中的结构指针的地址为0x1
- C 用结构指针初始化结构
- C 结构指针铸成数组Interop C#
- 方法中不允许访问结构指针
- 如何打印出结构指针
- MPI_Op_create:候选功能不可行.自定义结构指针不能解释为空指针
- 尝试将结构指针传递给类时出错
- 初始化结构指针的值
- 使用 pybind11 将自定义结构(指针向量)从 C++ 传递到 python 的方法是什么?