c++这个指针问题

发布于 2024-11-30 21:14:03 字数 464 浏览 2 评论 0原文

事情是这样的,我想要(可能不是最好的事情)能够调用一些类构造函数,该构造函数接收指向调用者的类的指针作为参数(ufff!!!)。好吧,代码看起来更好,就这样,就像我在 C# 中所做的那样。

public class SomeClass
{
   SomeOtherClass someOtherClass;

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

public class SomeOtherClass
{

   public SomeOtherMethod()
   {
      SomeClass c = new SomeClass(this);
   }
}

那么,如何在 C++ 中达到相同的结果呢? 提前致谢。

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.

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

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

发布评论

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

评论(5

千仐 2024-12-07 21:14:03
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);
   }
}

回答了上面的要求后,还要注意,在 C++ 中,您实际上不需要始终使用 new 声明对象。如果您声明,

SomeOtherClass someOtherClass;

则意味着您有一个名为 someOtherClassSomeOtherClass 对象。

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.

绝不服输 2024-12-07 21:14:03

可能不是最好的做法

这可能不是一个坏主意。然而,每次在 C++ 中使用指针时,您都必须完全清楚它将如何使用:被指向什么类型的东西(而不仅仅是指针的类型)指针,但标量与数组等),所指向的东西如何到达那里(例如通过 new?作为其他对象的一部分?其他东西?),以及它将如何全部清理干净。

如何在 C++ 中获得相同的结果?

几乎完全相同,当然,当您按值创建本地实例时,C++ 不使用 new (因此我们改为编写 SomeClass c = SomeClass(this); 或更多只是 SomeClass c(this);),我们必须注意指针与值类型(因此 SomeClass::someOtherClass 现在是 SomeOtherClass *,这也是我们在构造函数中接受的类型)。您还应该强烈考虑使用初始化列表来初始化数据成员,因此 SomeClass::SomeClass(SomeOtherClass* someOtherClass): someOtherClass(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) {}.

空‖城人不在 2024-12-07 21:14:03

您也可以在 C++ 中做几乎相同的事情:

class B;

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

private:
  B * pb;
};

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

private:
  A a;
};

问题是,您真的需要它吗?

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?

夜空下最亮的亮点 2024-12-07 21:14:03

也许像这样:)

class SomeOtherClass;

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

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);
  }
};
葬﹪忆之殇 2024-12-07 21:14:03

“this”是声明为 const 的成员函数(方法)中指向 const 的指针。
所以:

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
    }
};

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