简体   繁体   English

(iphone)如何在C ++类中访问CGRect成员变量?

[英](iphone) How to access CGRect member variable inside c++ class?

i have a c++ class with CGrect variable and i'm getting segfault when trying to access it. 我有一个带有CGrect变量的C ++类,尝试访问它时遇到了段错误。

class Parent
{
 //with some virtual functions/dtors
};

class Child
{ 
  public:
    void SetRect(CGRect rect) { mRect = rect; } 
    CGRect GetRect() { return mRect; }
    int GetIndex() { return mIndex; }

  private:
    CGRect mRect;
    int    mIndex;
};

i'm doing 我正在做

 CGRect rect = childPtr->GetRect();   

from object c code and it segfaults. 从对象c代码中,它会出现段错误。
I printed *childPtr just before the call and rect looks fine with intended data value. 我在调用之前打印了* childPtr,并用预期的数据值显示了rect。

int index = childPtr->GetIndex(); 

from same object c code(*.mm), works fine though. 从相同的目标c代码(* .mm),虽然工作正常。

Any idea why I'm getting segfaults? 知道为什么我会出现段错误吗?
Thank you 谢谢

  • edit - 编辑-

It's got something to do with virtual functions. 它与虚函数有关。

(gdb) p singlePuzzlePiece->GetRect() (gdb)p singlePuzzlePiece-> GetRect()

Program received signal EXC_BAD_ACCESS, Could not access memory. 程序收到信号EXC_BAD_ACCESS,无法访问内存。 Reason: KERN_PROTECTION_FAILURE at address: 0x00000001 0x00000001 in ?? 原因:KERN_PROTECTION_FAILURE地址为:0x00000001 0x00000001 in? () Cannot access memory at address 0x1 The program being debugged was signaled while in a function called from GDB. ()无法访问地址0x1处的存储器,处于从GDB调用的函数中时,发出正在调试的程序的信号。 GDB remains in the frame where the signal was received. GDB保留在接收信号的帧中。 To change this behavior use "set unwindonsignal on" Evaluation of the expression containing the function (at 0x1) will be abandoned. 要更改此行为,请使用“ set unwindonsignal on”,将放弃对包含函数(在0x1处)的表达式的求值。 (gdb) (gdb)

Somehow, the function is not properly compiled? 不知何故,函数未正确编译?

in objective C to get to the rect variables u do this 在目标C中获取rect变量

int indexX = rect.origin.x int indexX = rect.origin.x

int indexY = rect.origin.y int indexY = rect.origin.y

etc.. 等等..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM