Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using getter, setter in c++

Tags:

c++

c++11

I want to use a class in C++ that has an integer array like this:

class A{
private:
        int arr[50];

}

I will read something from text file like this:

sum i1 i2

That means: Sum of arrays index1 and index2 and store in index1.

How can I do it, with using getters and setters like:

seti2(geti1()+geti2())

or something like that, (because it's not very useful, I don't want to write getter and setter for every index geti1() geti2() ... geti50())

Do you have any idea?

By the way, my second question is that, is getter should not have any parameters and is setter should have only one parameter?

like image 516
Jonathan Cedric Avatar asked Oct 28 '16 08:10

Jonathan Cedric


1 Answers

One idea might be to use actual indexes. So you have a single get function which takes an index as an argument, and a single set function which takes an index and the value as arguments.

Another solution is to overload the operator[] function, to provide nice array-like indexing.

like image 81
Some programmer dude Avatar answered Oct 07 '22 05:10

Some programmer dude