C ++如何将“this”传递给指针引用 [英] C++ how to pass 'this' to pointer reference

查看:320
本文介绍了C ++如何将“this”传递给指针引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个主类,我喜欢传递它的指针引用对象创建,但它给我错误:

i have main class that i like to pass its pointer reference to on of the objects i creating but it gives me error :


错误1错误C2664:'GameController :: GameController(GameLayer *&)':
无法将参数1从'GameLayer * const'转换为'GameLayer *&'

Error 1 error C2664: 'GameController::GameController(GameLayer *&)' : cannot convert parameter 1 from 'GameLayer *const ' to 'GameLayer *&'

我在GameLayer(主对象)中有什么

what i have in the GameLayer ( the main object )

m_pGameController = new GameController(this);

并且在GameController中有这个构造函数

and in the GameController i have this constructor

GameController(GameLayer*& GameLayer)
{
 setGameLayer(gameLayer); // to GameLayer memeber ) 
}

resone是我需要能够修改GameController(GUI东西)中GameLayer中的数据

the resone is i need to be able to modify the data in GameLayer from GameController (GUI stuff)

推荐答案

首先, c $ c>可以是 GameLayer * const const GameLayer * const ,具体取决于成员函数 const 。但是,它是一个 const 指针。

First, the type of this could be GameLayer * const or const GameLayer * const depending upon whether the member function is const or not. But, it is a const pointer.

说到这里,引用指针 type *& ref 表示您将修改指针指向的对象。

Having said that, a reference to a pointer type *&ref means indicates that you will be modifying the pointer, the object which it is pointing to.

指针是一个常量指针,您不能将它分配给引用上下文中的非常量指针(GameLayer * p)。

"Since, this pointer is a constant pointer you cannot assign that to a non-const pointer(GameLayer * p)" in the context of references.

引用指针的情况下,您不能分配到将修改 this 中的值的引用。您不能将其分配给非常量指针( GameLayer *& p)的引用,但您可以分配对 const 指针的引用,即 GameLayer * const& p

In case of references to pointer, you cannot assign to a reference which will modify the value in this. You cannot assign it to a reference to a non-const pointer(GameLayer *&p), but you can assign a reference to a const pointer i.e. GameLayer *const &p.

所以,将构造函数改为 GameController(GameLayer * const& gameLayer)应该可以工作。

但是我没有看到

So, changing the constructor to GameController(GameLayer * const & gameLayer) should work.
But I don't see any use of it here currently.

如果你是从一个 const 成员函数调用, $ c>这个有类型 const * const 所以你必须将它改为 GameController(const GameLayer * const & gameLayer)

And if you're calling from a const member function then this has the type const *const so you will have to change it to GameController(const GameLayer * const & gameLayer).

这篇关于C ++如何将“this”传递给指针引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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