c++这个指针问题
事情是这样的,我想要(可能不是最好的事情)能够调用一些类构造函数,该构造函数接收指向调用者的类的指针作为参数(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
回答了上面的要求后,还要注意,在 C++ 中,您实际上不需要始终使用
new
声明对象。如果您声明,则意味着您有一个名为
someOtherClass
的SomeOtherClass
对象。Having answered your requirements above, also note that in C++ you really don't need to declare objects always with
new
. If you declare,then it means that you have an object of
SomeOtherClass
namedsomeOtherClass
.这可能不是一个坏主意。然而,每次在 C++ 中使用指针时,您都必须完全清楚它将如何使用:被指向什么类型的东西(而不仅仅是指针的类型)指针,但标量与数组等),所指向的东西如何到达那里(例如通过
new
?作为其他对象的一部分?其他东西?),以及它将如何全部清理干净。几乎完全相同,当然,当您按值创建本地实例时,C++ 不使用
new
(因此我们改为编写SomeClass c = SomeClass(this);
或更多只是SomeClass c(this);
),我们必须注意指针与值类型(因此 SomeClass::someOtherClass 现在是SomeOtherClass *
,这也是我们在构造函数中接受的类型)。您还应该强烈考虑使用初始化列表来初始化数据成员,因此SomeClass::SomeClass(SomeOtherClass* someOtherClass): someOtherClass(someOtherClass) {}
。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.Almost identically, except of course that C++ does not use
new
when you create a local instance by value (so we instead writeSomeClass c = SomeClass(this);
, or more simplySomeClass c(this);
), and we must be aware of the pointer vs. value types (so SomeClass::someOtherClass is now aSomeOtherClass *
, which is also the type we accept in the constructor). You should also strongly consider using initialization lists to initialize data members, thusSomeClass::SomeClass(SomeOtherClass* someOtherClass): someOtherClass(someOtherClass) {}
.您也可以在 C++ 中做几乎相同的事情:
问题是,您真的需要它吗?
You can do pretty much the same thing in C++ as well:
The question is, do you really need that?
也许像这样:)
Maybe like this :)
“this”是声明为 const 的成员函数(方法)中指向 const 的指针。
所以:
'this' is a pointer-to-const in member functions (methods) declared as const.
So: