c++ allow gets but not sets

i was wondering if i theres a way to make a bunch of variables and only the class functions can change the values. And whoever wants to access it from outside the class can only view the value, but changing is not allowed. Is it possible, without making a get function for every variable?

EDIT:: nvm, i know what im gonna do now, just gonna make a class for each variable
so if in the future i want to add more, im just gonna add another instance of the class
Last edited on
I don't understand your solution, ¿mind to post code?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class foo{
public:
   struct value{
      int n;
      double d;
   };
   const value& get() const{
      return var;
   }
private:
   value var;
};


int main(){
   foo bar;
   std::cout << bar.get().d << '\n';
   bar.get().n = 42; //fail
}
Now, show me a use case.
Couldn't you simply declare the variables as const? But yes, you can simply create a class with no functions that alter the data members. As for getting the data members, you can only return one object with a function, so to get more then one data member you would either need to make one getter for each or use a container or something similar. I suppose there could be some creative ways to do that too, just nothing I can think of off the top of my head.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include "gfMem.h"

//-----------------------------------------------------
// gfMem()-> Assign Memory address to variable address
//-----------------------------------------------------
// p -> process handle
// a -> base_address
// o -> offsets
// c -> offsets count
//-----------------------------------------------------
gfMem::gfMem(HANDLE p, int a, int o[], int c)
{
    base_address = a;
    if(o!=NULL)
    {
        int address = base_address;
        int* val = new int;
        for(int x = 0; x < c; x++)
        {
            ReadProcessMemory(p,(void*)address,val,4,NULL);
            address = *val + o[x];
        }
    }
    else
    {
        address = base_address;
    }
}//--> end_gfMem()

//-----------------------------------------------------
// nValue() - get 4byte value of address
//-----------------------------------------------------
int gfMem::nValue(HANDLE p)
{
    int* val = new int;
    ReadProcessMemory(p,(void*)address,val,4,NULL);
    return *val;
}//--> end_nValue()

//-----------------------------------------------------
// fValue() - get float value of address
//-----------------------------------------------------
float gfMem::fValue(HANDLE p)
{
    float* val = new float;
    ReadProcessMemory(p,(void*)address,val,
        sizeof(float),NULL);
    return *val;
}//--> end_fValue()

//-----------------------------------------------------
// sValue() - get string value of address
//-----------------------------------------------------
std::string gfMem::sValue(HANDLE p)
{
    std::string* val = new std::string;
    ReadProcessMemory(p,(void*)address,val,
        sizeof(std::string),NULL);
    return *val;
}//--> end_sValue() 


this is what i did, im too lazy to take out the important stuff, so just read away....xD
so instead of making a bunch of variables or unsized arrays, i just made a class for every variable im gonna make..and make all the variables private and just make a getmethods for views only
i havent tested it yet though
but im sure the nValue and fValue will work as i had tested them on my last cpp project, i just dont know if the sValue function will really return string though, i hope, or maybe i should make it char?

EDIT:: nvm this as this is about windows c++ programming...xD
Last edited on
Topic archived. No new replies allowed.