问题
i have main class that i like to pass its pointer reference to on of the objects i creating but it gives me error :
Error 1 error C2664: 'GameController::GameController(GameLayer *&)' : cannot convert parameter 1 from 'GameLayer *const ' to 'GameLayer *&'
what i have in the GameLayer ( the main object )
m_pGameController = new GameController(this);
and in the GameController i have this constructor
GameController(GameLayer*& GameLayer)
{
setGameLayer(gameLayer); // to GameLayer memeber )
}
the resone is i need to be able to modify the data in GameLayer from GameController (GUI stuff)
回答1:
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.
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.
"Since, this pointer is a constant pointer you cannot assign that to a non-const pointer(GameLayer * p)" in the context of references.
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
.
So, changing the constructor to GameController(GameLayer * const & gameLayer)
should work.
But I don't see any use of it here currently.
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)
.
回答2:
i don't know why are you using *&
but it'll work fine if you do:
GameController(GameLayer* GameLayer)
{
setGameLayer(gameLayer); // to GameLayer memeber )
}
回答3:
' this' is a pointer constant so you can pass it as reference or const reference.
m_pGameController = new GameController(*this);
GameController(GameLayer& GameLayer) {}
回答4:
"No Idea For Name"'s answer is almost certainly what you need, but for additional information the reason your code doesn't work is that GameLayer*&
doesn't just mean, "I can modify the GameLayer object referred to by the argument value I receive", it means "I am passed a reference to a pointer to a GameLayer object I can modify, and also I can use this reference to change the pointer value itself".
So for example:
void foo(GameLayer *&layerptr) {
layerptr = new GameLayer();
}
void main() {
GameLayer *ptr = 0;
std::cout << (void*)ptr << "\n";
foo(ptr);
std::cout << (void*)ptr << "\n";
}
The value of the ptr
variable in main
has changed. That's what pass-by-non-const-reference is for, and so you do not need pass-by-non-const-reference here. Either pass a pointer to a GameLayer
object (type GameLayer*
) or a reference (type GameLayer&
), not both.
The reason it is not valid to pass this
to a function that accepts GameLayer*&
is that this
is not a variable and it cannot be assigned to -- in C++ jargon it is not an lvalue and so you can't take an lvalue reference to it. this
tells you what object the current member function is being called on, and you cannot suddenly decide that your member function will from now on be operating on a different object.
来源:https://stackoverflow.com/questions/18556866/c-how-to-pass-this-to-pointer-reference