如何将指针作为双指针传递给类的指针属性?

发布于 2025-01-05 10:05:05 字数 419 浏览 1 评论 0原文

我不知道如何正确传递这个双指针......我尝试过的都不起作用......

class myClass{

    MyClass *attribClass;
}

void derp(MyClass **myA) {
    // recursively calls down classes....
    derp(&(myA->attribClass)); // what am i doing wrong?
}

int main() {

    MyClass *myClass = new MyClass;

    myClass.attribClass = *whatever code to initialize a long linked list of MyClass's*;

    derp(&myClass); 
}

I cant figure out how to properly pass this double pointer.... nothing i've tried works...

class myClass{

    MyClass *attribClass;
}

void derp(MyClass **myA) {
    // recursively calls down classes....
    derp(&(myA->attribClass)); // what am i doing wrong?
}

int main() {

    MyClass *myClass = new MyClass;

    myClass.attribClass = *whatever code to initialize a long linked list of MyClass's*;

    derp(&myClass); 
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

倾听心声的旋律 2025-01-12 10:05:05

有几个问题:

class MyClass {  //  Change to "MyClass".

public:   //  Need to make it public or it can't be accessed by "derp()"
          //  Did you intend "derp()" to be a class member?

    MyClass *attribClass;
}; //  missing semicolon

void derp(MyClass **myA) {
    // recursively calls down classes....
    derp(&((*myA)->attribClass)); // what am i doing wrong?
}

在最后一个问题中,您需要在访问 attribClass 之前引用 myA 一次。

derp(&((*myA)->attribClass)); // what am i doing wrong?

There's several issues:

class MyClass {  //  Change to "MyClass".

public:   //  Need to make it public or it can't be accessed by "derp()"
          //  Did you intend "derp()" to be a class member?

    MyClass *attribClass;
}; //  missing semicolon

void derp(MyClass **myA) {
    // recursively calls down classes....
    derp(&((*myA)->attribClass)); // what am i doing wrong?
}

And in the last one, you need to deference myA once before you access attribClass.

derp(&((*myA)->attribClass)); // what am i doing wrong?
◇流星雨 2025-01-12 10:05:05

myA 是一个指向指针的指针,因此您需要取消引用它以获取可以使用 -> 的指针,所以:

derp(&(myA->attribClass));

应该

derp(&((*myA)->attribClass));

我 假设您需要更改/设置指针的值。如果你不是,那么你可能不应该在这里使用双指针。

myA is a pointer to a pointer, so you need to dereference it to get a pointer which you can use -> with, so:

derp(&(myA->attribClass));

Should be

derp(&((*myA)->attribClass));

I'm assuming you need to change/set the value of the pointer. If you aren't you probably shouldn't use a double pointer here.

樱&纷飞 2025-01-12 10:05:05

您需要对 myA 进行额外的取消引用,其类型为 MyClass ** (不仅仅是 MyClass *):

derp(&(*myA)->attribClass);

You need an extra dereference on myA, which is of type MyClass ** (not simply MyClass *):

derp(&(*myA)->attribClass);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文