在构造函数中创建一个指针,指向C ++中的类的实例 [英] Create a pointer inside a constructor to point to the instance of the class in C++

查看:267
本文介绍了在构造函数中创建一个指针,指向C ++中的类的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对这些C ++指针感到困惑,我想知道是否可以在一个指向类的实例的构造函数中创建一个指针(下面的代码行)。

I'm a bit confused with these C++ pointers and I would like to know if it's possible to create a pointer inside a constructor that will point to the instance of the class (something on the lines of the following).

class Room{
public:  
    Room();  
    ~Room();  
private:  
    Room* ptrToSelf;  
};  

Room::Room(){  
    ptrToSelf = &(what should I write in here to have the pointer point to it)  
}

我知道这可能听起来很疯狂(新手在这里),但我只想知道是否可能。有人可以向我解释这个。

非常感谢

I know this might sound crazy (newbie here), but I just wanted to know if it is possible. Could someone please explain this to me.
Many thanks

编辑:

谢谢大家回答我这么快。我问是因为我想使用一个公共 Room * :: getPointer(); 方法返回类的指针。我有一个字符从一个房间移动到房间,我使用指针来实现这一点。我以字符串的形式从用户的输入,我不能将该字符串传递给函数 character.moveTo(const Room *)我创建的方法。我需要一种方法来将字符串转换为类的指针,以便我可以传递指针的方法。


Thank you everyone for answering me so fast. I was asking because I wanted to use a public Room*::getPointer(); method to return the pointer for the class. I have a character that moves from room to room and I'm using pointers to achieve this. I'm getting the input from the user in form of string and I can't pass that string to the function to character.moveTo(const Room*) method I created. I need a way to "convert" the string into a pointer for the class so that I could pass the pointer to the method.

编辑:

我正在考虑实现一个基于字符串类型参数返回对象指针的方法:


I was thinking of implementing a method that will return the pointer for the object based on a string type parameter:

Room::Room* getPointer(const string &nameOfRoom){  
    //I can't use this return statement as the following error  
    //Error: 'this' may only be used inside a nonstatic member function  
    return this; 

编辑(希望最后一个):

寻找?

Edit (hopefully last one):
Is this returning what I'm looking for?

Room::Room* getPointer(const string &nameOfRoom){    
    return &Room();  
}

还是返回新房间的地址?

谢谢

or it's returning the address of a new Room?
Thank you

推荐答案

只需使用隐式的 this 参数: p>

Just use the implicit this parameter:

ptrToSelf = this;

或者,更好的(忽略 ptrToSelf 是一个相当愚蠢的想法开始):

Or, even better (ignoring the fact that ptrToSelf is a rather silly idea to begin with):

Room::Room() : ptrToSelf(this) { }

编辑:@BenjaminLindley提请我注意一个事实,与某些其他成员功能。特别的,复制构造函数在默认情况下将不会正确地运行( ptrToSelf 将简单地从源实例复制,因此两个实例将最终指向该实例)。因此,您可能需要提供一个复制构造函数,以确保 ptrToSelf 正确分配。基本情况是:

@BenjaminLindley drew my attention to the fact that there will be issues with certain other member functions. In particular, the copy constructor won't behave correctly by default (ptrToSelf will simply be copied from the source instance, so both instances will end up pointing to the one). You may therefore need to provide a copy constructor that ensures ptrToSelf is assigned correctly. The base case is this:

Room::Room(const Room& room) : ptrToSelf(this) { … }

您还需要担心赋值运算符,但只要它调用复制构造函数,会很好。以下通用实现将会实现:

You also need to worry about the assignment operator, but as long as it simply invokes the copy constructor, you'll be fine. The following general-purpose implementation will do the trick:

Room& operator=(Room room) { swap(room); return *this; }
void swap(Room& room) { /* Swap rooms… */ }

(PS:应该是 class Room {... ,而不是 class Room(){... 。)

(P.S.: It should be class Room {…, not class Room(){….)

这篇关于在构造函数中创建一个指针,指向C ++中的类的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆