简体   繁体   English

我可以将指针作为指向类的指针传递,以便我可以使用分配给类中指针的任何内容吗?

[英]Can I pass a pointer as a pointer to a class, so I can use the whatever is assigned to the pointer in the class?

I have two objects of the same class in my program.我的程序中有两个相同类的对象。 I set the focused object to a third variable, which is a pointer.我将焦点对象设置为第三个变量,它是一个指针。 So I can switch the focused object with this third pointer variable and have access at every part of my code without having to know which object is focused.因此,我可以使用第三个指针变量切换焦点对象,并且可以访问代码的每个部分,而无需知道哪个对象是焦点。

This looks like the following:如下所示:

class MainClass
{
    OtherClass otherClass;

    Field fieldA;
    Field fieldB;
    Field *focusedField = &fieldA;

    void someMethod(){
         otherClass.othermethod();         

         focusedField = &fieldB;
         
         otherClass.othermethod();
    }

    void MainClass()
      : otherClass(focusedField);
    {
        
    }
}

But now I want to use this pointer in another class like this:但现在我想在另一个类中使用这个指针,如下所示:

class OtherClass{
    OtherClass(Field *f){
        focusedField = f;
    }

    Field *focusedField;
    
    void otherMethod(){
        std::cout << focusedField->getState() << std::endl;
    }
}

And this works partly...这部分有效......
The first call of the otherMethod method uses the fieldA, which is correctly, but the second call, after I changed it to fieldB, is still using fieldA. otherMethod 方法的第一次调用使用了 fieldA,这是正确的,但是在我将其更改为 fieldB 之后的第二次调用仍然使用 fieldA。

I want to achieve that I pass the focusedField to the OtherClass and it changes if I change it in the MainClass.我想实现将focusedField 传递给OtherClass 并且如果我在MainClass 中更改它,它会更改。

To expand on JS's comment , if you want to “track” a value, you store a pointer or reference to it.扩展JS 的注释,如果你想“跟踪”一个值,你可以存储一个指针或对它的引用。 That's still true if the value is itself a pointer.如果值本身是一个指针,这仍然是正确的。 For const-correctness, you should declare the OtherClass member (and constructor parameter) as Field *const & since it doesn't need to change which Field is focused.对于常量正确性,您应该将OtherClass成员(和构造函数参数)声明为Field *const &因为它不需要更改哪个Field是焦点。 If it doesn't need to write to whatever focused Field either, use const Field *const & .如果它也不需要写入任何重点Field ,请使用const Field *const &

It's worth noting that using a pointer to pointer might be better both as a member (because it allows assignment to have the usual semantics) and as a constructor parameter (to advertise that a temporary is inappropriate as an argument).值得注意的是,使用指向指针的指针作为成员(因为它允许赋值具有通常的语义)和构造函数参数(宣传临时不适合作为参数)可能会更好。 It still serves the same sharing purpose and allows the same choices of const placement.它仍然服务于相同的共享目的,并允许相同的const放置选择。

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

相关问题 我可以在朋友课堂上使用此指针吗? - can I use THIS pointer in friend class? 如果超级构造函数不使用该指针,是否可以将指向派生类字段的指针传递给超级构造函数? - Can I pass a pointer to a derived-class field to the superconstructor if the superconstructor doesn't use that pointer? 如何在CRTP实现中传递基类指针 - How can I pass base class pointer in a CRTP implementation 我可以将此虚拟函数中的指针视为派生类的指针吗? - Can I treat this pointer in virtual function as this pointer of derived class 如何使用它指向的指针将指针传递给指向 function 的指针? - How can I pass a pointer to pointer to a function using the pointer it is pointing to? 我可以将指针转换为我的班级吗? - Can I convert a pointer to my class? 我可以使用 if (pointer) 而不是 if (pointer != NULL) 吗? - Can I use if (pointer) instead of if (pointer != NULL)? 我可以将指针传递给指针,分配内存,然后将其传回去吗? - Can I pass a pointer to a pointer, allocate memory, then pass it back? 如何从父类指针使用未在父类中定义的子类方法? - How can I use child class methods not defined in a parent class from a parent class pointer? 为什么我不能将我的类指针传递给访问者类? 它说没有匹配的功能 - Why can I not pass my class pointer to my visitor class? It says there are no matching functions
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM