0

I'm making a game where I have a Level class that deals with storing all the objects in vectors and changing them.

And I have another class that is called Engine.h that has static functions that calculate things based on the vectors Level owns but doesn't change anything in them. Level calls the Engine functions and does operations based on what's returned.

But since Engine needs to see the vectors that Level owns, I'm being forced to pass a bunch of pointers (const) of vectors to each Engine function so that it can do calculations based on the current state of the vectors.

Is there any way to make Engine just have access to all of Level's member variables but not be able to edit any of them. Some sort of a 'const friend"?

I just realized that there is an object level. So I also would need to have a way to assign Engine a specific object of Level with which it could be a 'const friend'. Maybe this got to complicated. If there is no solution is it bad practice to pass a lot of member variables to the Engine functions, or is that how this is done. What about passing a pointer to Level that is const (will that make all of Levels member variables const also?)

Souren
  • 47
  • 5
  • 1
    `friend` doesn't override `const`, so passing a `const Level*` to a befriended class/function will give read access only. – Ben Voigt Aug 30 '14 at 13:38
  • `I'm being forced to pass a bunch of pointers (const) of vectors to each Engine function` And why can you not expose those vectors via a const member function? –  Aug 30 '14 at 13:54

2 Answers2

1

Lets say you have some class Foo that has a member variable x you want to access. You can make const functions that access the member variables by value. Since they are const, they may not modify any of the member variables.

class Foo
{
public:
    Foo() : x{0} {}  // constructor

    int   getX() const       { return x; }   // this is a "getter"
    void  setX(int value)    { x = value; }  // this is a "setter"
private:
    int x;
};

Then you can have your other class Bar. Maybe in some function it needs to access to x variable of Foo, you can use the getter.

class Bar
{
    void doStuff (Foo a)
    {
        int answer = a.getX();
        std::cout << answer << std::endl;
    }
};

Now we can look at a quick demo. We make an instance of Foo and an instance of Bar. Then we can set the x value for the Foo, and access it from Bar's doStuff function.

int main()
{
    Bar myBar;
    Foo myFoo;

    myFoo.setX(5);

    myBar.doStuff(myFoo);  // will output 5

    return 0;   
}
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
  • 1
    This doesn't give more read access to one class than to any other. – Ben Voigt Aug 30 '14 at 13:37
  • My Game class is the one which creates Level then calls these Engine functions. If I pass the Level object pointer to each of the Engine functions, I can use the getter method you suggested. But is it clean to pass a pointer of a class to an engine? Is there a better structure I should be using? – Souren Aug 30 '14 at 18:16
0

You can do stuff like this if it helps:

class Level
{
private:
    std::vector<float> stuff;

public:

    // give outsiders const access

    const std::vector<float>& const_stuff = stuff; // c++11 else use ctor

    const std::vector<float>& get_stuff() const { return stuff; } // more idiomatic
};
Galik
  • 47,303
  • 4
  • 80
  • 117