5

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)

JBL
  • 12,588
  • 4
  • 53
  • 84
user63898
  • 29,839
  • 85
  • 272
  • 514
  • 1
    Why does it need to be a reference? I'm going to go ahead and bet that it probably doesn't. – Benjamin Lindley Sep 01 '13 at 09:04
  • 2
    `this` is not a `GameLayer *&`. It's either a `GameLayer *` or a `const GameLayer *`, depending on the constness of the currently running method. – Frédéric Hamidi Sep 01 '13 at 09:04
  • 1
    Also, note that instead of a variable you're using the type name itself in the constructor. `GameController(GameLayer*& GameLayer)` shouldn't it be `GameController(GameLayer*& gameLayer)` – Uchia Itachi Sep 01 '13 at 09:06
  • Please avoid pointers: GameController(GameLayer& GameLayer) and than m_GameController = GameController(*this); - this does not address const correctness –  Sep 01 '13 at 09:30

4 Answers4

6

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).

Uchia Itachi
  • 5,287
  • 2
  • 23
  • 26
  • *"Since, this pointer is a constant pointer you cannot assign that to a non-const pointer(GameLayer * p)"* -- Yes, you can. – Benjamin Lindley Sep 01 '13 at 09:44
  • 1
    Specifically: the set of all things that can be used to initialize `GameLayer *` is identical to the set of all things that can be used to initialize `GameLayer *const`. The `const` in that location only affects whether you can assign to `p`, it doesn't affect what you can initialize it with. The difference between initializing `GameLayer *&` and initializing `GameLayer *const&` is as you say. – Steve Jessop Sep 01 '13 at 09:55
  • @SteveJessop: Yes, I agree. But isn't what is written above true? – Uchia Itachi Sep 01 '13 at 09:58
  • @Uchia: No, what you wrote is not all true. The part that is not true is, "Since, this pointer is a constant pointer you cannot assign that to a non-const pointer(GameLayer * p) but you can assign it to a const pointer (GameLayer * const p)." – Steve Jessop Sep 01 '13 at 10:00
  • @SteveJessop: Could you please point out, I am unable to get you. – Uchia Itachi Sep 01 '13 at 10:01
  • @UchiaItachi: I'm sorry, I cannot explain it in any other terms. There is a sentence in your answer that is false. I have quoted that sentence to you, so that you know which one I'm talking about. You agree with my comment in which I describe what is false about it. I simply cannot understand what it is that you don't get. – Steve Jessop Sep 01 '13 at 10:03
5

i don't know why are you using *& but it'll work fine if you do:

GameController(GameLayer* GameLayer)
{
 setGameLayer(gameLayer); // to GameLayer memeber ) 
}
No Idea For Name
  • 11,411
  • 10
  • 42
  • 70
  • 2
    Depends. If the method that performs `new GameController(this)` is `const` (which seems to be true in the questioner's case), then `this` will be a `const GameLayer *` and the constructor of `GameController` (or an overload) should take a `const GameLayer *`. – Frédéric Hamidi Sep 01 '13 at 09:16
  • 2
    @FrédéricHamidi The method is not `const` in the questioner's case. You can tell from the error message. – interjay Sep 01 '13 at 09:18
  • @interjay, aye, but is `GameLayer *const` not supposed to be the same as `const GameLayer *`? I'll have to look that up. – Frédéric Hamidi Sep 01 '13 at 09:19
  • 2
    `this` can be of type `GameLayer * const` or `const GameLayer * const`. – Uchia Itachi Sep 01 '13 at 09:20
  • 1
    @FrédéricHamidi No, they are not the same. `GameLayer *const` means that the pointer is `const` but the value it points to is not. – interjay Sep 01 '13 at 09:20
  • 1
    @interjay, [yup](http://c-faq.com/ansi/constptrconst.html), Itachi and you are right. Guess I need more tea. – Frédéric Hamidi Sep 01 '13 at 09:21
1

' this' is a pointer constant so you can pass it as reference or const reference.

 m_pGameController = new GameController(*this);

 GameController(GameLayer& GameLayer) {}
alexbuisson
  • 7,699
  • 3
  • 31
  • 44
  • 2
    You don't pass `this` as a reference, you pass `*this` as a reference. Which is perfectly valid, but not what the question is about. –  Sep 01 '13 at 09:11
  • Agree but I used reference because it is more C++ – alexbuisson Sep 01 '13 at 09:17
1

"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.

Steve Jessop
  • 273,490
  • 39
  • 460
  • 699