开发者

autoreferencing this class to use in another for C++

开发者 https://www.devze.com 2022-12-25 13:59 出处:网络
in java we can do this: public class A{ publ开发者_JAVA技巧ic static void main(String...str){ B b= new B();

in java we can do this:

public class A{

    publ开发者_JAVA技巧ic static void main(String...str){
        B b  = new B();
        b.doSomething(this);   //How I do this in c++ ? the this self reference
    }
}


public class B{
    public void doSomething(A a){
        //Importat stuff happen here
     }
}

How can I do the same but in c++, I mean the self reference of A to use the method in B ?


First, in a static method there is no this parameter. Anyway, assuming that main() is not static here is how you can do it in C++

class A {
public:
   void f() { 
      B* b = new B();  
      b->doSomething(this);
   }

   void g() { 
      // ...
   };
};


class B {
public:
   void doSomething(A* a) {
       // You can now access members of a by using the -> operator:
       a->g();
   }
};

In C++ this is a pointer to the "current" Object. Thus if you define doSomething() as taking a pointer to A (that is: doSomething(A* a)), then you will be able to receive the this of A. The -> operator will give you access to the members of the a parameter, as follows: a->g().

Alternatively you can pass *this and define doSomething() to take a reference to A (that is: doSomething(A& a)):

class A {
public:
   void f() { 
      B* b = new B();  
      b->doSomething(*this);
   }

   void g() { 
      // ...
   };
};


class B {
public:
   void doSomething(A& a) {
       // You can now access members of a by using the . operator:
       a.g();
   }
};

To access members of a reference you need to use the . (dot) operator: a.g().


C++ has this key word. But it is pointer and not a reference. In your case you need to change the signature of doSomething in class B to doSomething(A* pA) and use it like b.doSomething(this);


this is a keyword in C++ pointing to the current object. Perhaps you want *this which is a reference to the current object.

I'd say that this is an anachronism; references are more common than pointers in most modern C++ code and everything would work quite fine if this were defined to be a reference.

But the inconvenience is small.


Java is a "modern" compiler. Modules are correctly compiled, including their cross-references. C++ is an "old" compiler, smart enough to do everything in a single pass over the source code. However, this implies that it will let you reference A from B, and even B from A, provided that you only use pointers to them. Actually, when C++ compiles A it does not know that you there is a B class. Okay, we can fix this by preceding the declaration of A with:

class B;

Which equals to say to the compiler: okay, there is a B class somewhere else. Fantastic. Now you can put parameters of the kind "B* b", because a pointer is just a pointer (4 bytes in 32bits architectures...). However, you won't be able to compile something like, say, a parameter-by-value "B b", because among other things, the compiler needs to know the size of the class B, as it hasn't even compiled it yet.

There are other considerations about your code, specially about passing this, (this is a pointer), but those aspects have been answered yet.

0

精彩评论

暂无评论...
验证码 换一张
取 消