简体   繁体   English

如何通过指针更改类成员的值

[英]How to change class member value by pointer

This function is part of a class called ClipInterface which provides a GUI to edit some member values from a given Clip object. 该函数是名为ClipInterface的类的一部分,该类提供GUI来编辑给定Clip对象中的某些成员值。

To a complish that, I am using this function that receives a Clip object by pointer, and then it access to some of its members to modify them as follows: 为此,我正在使用此函数,该函数通过指针接收Clip对象,然后访问其某些成员以对其进行如下修改:

void ClipInterface::update(Clip* _clip){
//p_clip is a pointer declared in the .h file so it can 
//work across the whole class

    p_clip = _clip;

    for (int i = 0; i < p_clip -> stepSequence.size(); ++i){
        stepToggles[i] -> active = p_clip -> stepSequence[i];        
    }

    for (int i = 0; i < p_clip -> stepSequence.size(); ++i){
        *p_clip -> stepSequence[i] = stepToggles[i] -> active;
    }
}

By using the previous expression, the program compiles but then it gives me a segmentation fault error when i try tu run it. 通过使用前面的表达式,程序可以编译,但是当我尝试运行它时,它给了我一个分段错误错误。

The questions are: 问题是:

  • how do i modify a class member value by pointer? 如何通过指针修改类成员值? i tried what is here but i get a segmentation fault. 我尝试了这里的内容,但出现了细分错误。

  • what in this expresion y causing the segmentation fault? 在此表达式y中导致分段错误的原因是什么?

thanks in advance. 提前致谢。

*p_clip -> stepSequence[i] = stepToggles[i] -> active;

in here p_clip is already a pointer you should use; 在这里p_clip已经是您应该使用的指针;

p_clip -> stepSequence[i] = stepToggles[i] -> active;

To access members of a structure through a pointer, use the arrow operator. 要通过指针访问结构的成员,请使用箭头运算符。

You can use either one of these 您可以使用以下任何一种

(*p_clip).stepSequences[i] = stepToggle

Where you first derefrence the pointer to get the object then get the member 首先您取消对指针的引用以获取对象,然后获取成员

Or use the arrow operator which is equivalent and have the same effect 或使用等效的箭头运算符并具有相同的效果

p_clip->stepSequences[i] = stepToggle

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

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