Using accessories in the same class

I heard that in C ++, using accessor ( get...()) in a member function of the same class where accessor was defined is good programming practice? Is this true, and is it necessary to do this?

For example, this is preferable:

void display() {
    cout << getData();
}

over something like this:

void display() {
    cout << data;
}

datais a data member of the same class where the accessor was defined ... the same method display().

I think about the overhead for this, especially if you need to repeatedly call an accessory within the same class, and not just use the data element directly.

+3
source share
7 answers

, getData(), , data.

, ( ), .

+9

. , "" . , "", accessor.

- , , , - . , , , , .

, .

+1

, , . - , . , . , , , - . , . - , .

, , , - , . , . !

. ;) , , . , , , . , - , , . : "someVariable _".

!

+1

, , . , , . , .

, - - .

0

( -). getData() , - -. , .

0

, . .

, , , , , , - . , , , . , .

0

, , , , ? , , . , , cnuts.

0

Source: https://habr.com/ru/post/1708856/


All Articles