为什么我无法将指针投射到类结构

Why i am not able to cast pointer to the class structure

本文关键字:结构 指针 为什么      更新时间:2023-10-16

我有一个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对象的树结构,GeometryContainer对象中的数据类型。

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