开发者

c++ this pointer question

here is the thing, I want to (probably not the best thing to do) have the ability to call some class constructor that receives as a parameter a pointer to the class who's calling (ufff!!!). Well in code looks better, here it goes, as I do it in C#.

public class SomeClass
{
   SomeOtherClass someOtherClass;

   //Constructor
   public SomeClass(SomeOtherClass someOtherClass)
   {
      this->someOtherClass = someOtherClass;
   }
}

public class SomeOtherClass
{

   public SomeOtherMethod()
   {
 开发者_开发知识库     SomeClass c = new SomeClass(this);
   }
}

So, How can I achieve the same result in c++? Thanx in advance.


class SomeOtherClass;  // forward declaration (needed when used class is not visible)
class SomeClass
{
   SomeOtherClass *someOtherClass;
public:
   SomeClass(SomeOtherClass *some) : someOtherClass(some)
   {}  // this is called initialization at constructor (not assignment)
}

class SomeOtherClass
{
public:
   SomeOtherMethod()
   {
      SomeClass *c = new SomeClass(this);
   }
}

Having answered your requirements above, also note that in C++ you really don't need to declare objects always with new. If you declare,

SomeOtherClass someOtherClass;

then it means that you have an object of SomeOtherClass named someOtherClass.


probably not the best thing to do

It might not be a bad idea. However, every time you use pointers in C++, you must be completely clear about how it will be used: what kind of thing is being pointed to (not just the type of the pointer, but scalar vs. array, etc.), how the pointed-at thing gets there (e.g. via new? As part of some other object? Something else?), and how it will all get cleaned up.

How can I achieve the same result in c++?

Almost identically, except of course that C++ does not use new when you create a local instance by value (so we instead write SomeClass c = SomeClass(this);, or more simply SomeClass c(this);), and we must be aware of the pointer vs. value types (so SomeClass::someOtherClass is now a SomeOtherClass *, which is also the type we accept in the constructor). You should also strongly consider using initialization lists to initialize data members, thus SomeClass::SomeClass(SomeOtherClass* someOtherClass): someOtherClass(someOtherClass) {}.


You can do pretty much the same thing in C++ as well:

class B;

class A
{
public:
  A (B * b) : pb (b) { }

private:
  B * pb;
};

class B
{
public:
  B () : a (this) { }

private:
  A a;
};

The question is, do you really need that?


Maybe like this :)

class SomeOtherClass;

class SomeClass
{
private:
  SomeOtherClass * someOtherClass;
public:
  SomeClass(SomeOtherClass *someOtherClass)
  {
    someOtherClass = someOtherClass;
  }
};
class SomeOtherClass
{
public:
  void SomeOtherMethod()
  {
    SomeClass *c = new SomeClass(this);
  }
};


'this' is a pointer-to-const in member functions (methods) declared as const. So:

void f1(X* p);
void f2(const X* p);

class X {
    void m1() {
        f1(this); // OK
        f2(this); // also OK
    }
    void m2() const {
        f2(this); // OK
        f1(this); // error, 'this' is a pointer to const X
    }
};
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜