C++ 使用成员函数返回结构体指针属性的值
C++ Use member function to return value of a struct's pointer property
我正在用SDL学习一些C ,我遇到了一个令人困惑的问题。
我在标题文件中有一个类:
SDL_Surface* loadImage(const char*);
class GameSystem
{
public:
// Constructor
GameSystem();
// Destructor
~GameSystem();
SDL_PixelFormat* screenFormat();
private:
SDL_Window* m_window;
SDL_Surface* m_screen;
}
// Global pointer for all to access
extern const GameSystem* g_gameSystem;
在构造函数中,M_Window和M_Screen可以初始化。在相应的源文件中,我定义此成员函数:
SDL_PixelFormat* GameSystem::screenFormat()
{
return m_screen->format;
}
m_screen的结构就是这样:
typedef struct SDL_Surface
{
Uint32 flags; /**< Read-only */
SDL_PixelFormat *format; /**< Read-only */
int w, h; /**< Read-only */
int pitch; /**< Read-only */
void *pixels;
}
要运行此操作,在main()
函数中,我获得了游戏系统的实例,然后从header文件到该实例。喜欢:
int main(int argc, char** argv)
{
// Initialise the game system core
GameSystem gameSystem;
// Make the global gameSystem pointer point to this instance
g_gameSystem = &gameSystem;
我的问题是:如何获得format
属性的值以这样使用ScreenFormat函数:g_gameSystem->screenFormat()
非常感谢。
回答您的实际问题,因为我假设m_screen
在您的构造函数中适当初始化:
g_gameSystem->screenFormat()->someVariable
另外,一些未经请求的建议,请勿使用匈牙利符号。
相关文章:
- 通过指向指针数组的指针访问子类的属性
- 重新检查时指针属性会更改
- 返回的指针属性中类实例上的矢量属性消失
- boost::spirit指针属性是用nullptr初始化的吗?
- 如何使用指针从另一个类访问属性
- 我想获取点的属性,它报告错误 C3867:"point::output_x":非标准语法;使用"&"创建指向成员的指针
- 使用智能指针属性创建资源库函数?
- 通过指针算法调用结构的结构属性
- C++对象共享属性 - 使用指针或引用
- C++ 使用成员函数返回结构体指针属性的值
- 指向结构的指针保留某些属性,同时将其他属性重置为零(使用 map)
- 当我初始化了指针属性 allready 时,如何将 QGraphicsItem 向下投射到创建的类?
- 如何使用boost::graph dijkstra的算法,如果顶点属性是指针?
- 提升属性树:使用指向节点及其父节点的指针删除节点
- 如果使用类的指针属性来访问堆,如何避免非标量类型的错误
- C++引用基类中的子类指针属性
- 返回泛型函数中对象的引用(具有指针或非指针属性)
- 用静态数组覆盖指针属性
- 如何将指向类的指针属性的指针作为双指针传递?
- 如何使用非指针属性的析构函数