I am trying to create a setter function for a reference variable

I have a 'Player' class with a reference variable to another class within it called Arena &arenaref, its initialised safley in the constructor of 'Player'

The idea is to update the reference to this class in the main loop regularly so my 'Player' class has access to some of the data in the reference to perform operations like collisions etc.



(Im really enjoying the speed of c++ for operations like this, sooo much faster than java and so soo much faster than processing)



my variable arenaref needs to be a reference or a const reference so as not to copy the whole thing over in each loop so I have created a setter function to update it, here is an example;



in the main loop:



1
2
3
4
    Arena arena(stuff, stuff, stuff)

    player1.setArena(&arena)


and in player:

1
2
3
4
5
    void setArena(Arena &_arena){

        arenaref = _arena;

    }


however the error for this is:error: use of deleted function 'Arena& Arena::operator=(const Arena&)



and when I create like this

1
2
const Arena &arenaref;


I get this error



|error: passing 'const Arena' as 'this' argument of 'Arena& Arena::operator=(const Arena&)' discards qualifiers [-fpermissive]|



How exactly do I create the setter required?



EDIT: noticed that i get same as first error just passing the thing outright :/ gonna revize references
Last edited on
Once you've bound a reference to an object, you can't change the binding to a different object. You can only bind the reference to an object when you create it.

When you do something like:

 
arenaref = _arena;

What you're actually doing is assigning the value of whatever object _arena is bound to, into the whatever object arenaref is bound to. In other words:

1
2
3
4
5
6
7
Arena a;
Arena b;

Arena& arenaref = a;
Arena& _arena = b;

arenaref = _arena;


is exactly the same as:

1
2
3
4
Arena a;
Arena b;

a = b;


If you want to change which object arenaref is associated with, you need to use pointers, not references.
Last edited on
Topic archived. No new replies allowed.