Skip to main content
Post Closed as "Duplicate" by Arseni Mourzenko, CommunityBot, Ampt, Doc Brown, Bart van Ingen Schenau
C++ tag not necessary
Link
Ampt
  • 4.7k
  • 2
  • 25
  • 47
deleted 484 characters in body; edited title
Source Link
Arseni Mourzenko
  • 139.4k
  • 32
  • 359
  • 544

This video shows that you Why shouldn't I make private variables public DIRECTLY but it's ok indirectly, whybut should use public getters/setters?

So I amI'm watching this video, it's part 13 of something like 73 videos ona C++ tutorial video. It is talking about variables inside classes and assert that to use the var, Ivariables should make it PRIVATE and not public..be marked private. But then, at timesIt explains that if I might want to use itthem publicly outside the class so then there is this way to MAKE, I should do it public indirectly usingthrough functions.

That's all good and all, I understand WHY a variable SHOULD be private, this keeps that chances the programmer will accidentally cause an error low.

The part I do not understand is if the programmer does what this guy shows and finds a way AROUND making the variable private and converts it to public using this way, what'sWhy? What's the difference. Now? It looks like the variableresult is public... He just did the same thing, except that instead of having one line of code, I now he madehave like 9 or 10 MORE lines of code which seems the opposite direction that a programmer would like to go. Less is more usuallyLOC to do the same thing.

Can someone explain why converting a private variable in a class to public within the class rather than just making it public right out the simple way is any different and more efficient?

This video shows that you shouldn't make private variables public DIRECTLY but it's ok indirectly, why?

So I am watching this video, it's part 13 of something like 73 videos on C++. It is talking about variables inside classes and that to use the var, I should make it PRIVATE and not public... But then, at times I might want to use it publicly outside the class so then there is this way to MAKE it public indirectly using functions.

That's all good and all, I understand WHY a variable SHOULD be private, this keeps that chances the programmer will accidentally cause an error low.

The part I do not understand is if the programmer does what this guy shows and finds a way AROUND making the variable private and converts it to public using this way, what's the difference. Now the variable is public... He just did the same thing, except now he made like 9 or 10 MORE lines of code which seems the opposite direction that a programmer would like to go. Less is more usually.

Can someone explain why converting a private variable in a class to public within the class rather than just making it public right out the simple way is any different and more efficient?

Why shouldn't I make variables public, but should use public getters/setters?

I'm watching a C++ tutorial video. It is talking about variables inside classes and assert that variables should be marked private. It explains that if I want to use them publicly, I should do it indirectly through functions.

Why? What's the difference? It looks like the result is the same, except that instead of having one line of code, I now have like 9 or 10 more LOC to do the same thing.

Can someone explain why converting a private variable in a class to public within the class rather than just making it public right out the simple way is any different and more efficient?

Source Link

This video shows that you shouldn't make private variables public DIRECTLY but it's ok indirectly, why?

So I am watching this video, it's part 13 of something like 73 videos on C++. It is talking about variables inside classes and that to use the var, I should make it PRIVATE and not public... But then, at times I might want to use it publicly outside the class so then there is this way to MAKE it public indirectly using functions.

That's all good and all, I understand WHY a variable SHOULD be private, this keeps that chances the programmer will accidentally cause an error low.

The part I do not understand is if the programmer does what this guy shows and finds a way AROUND making the variable private and converts it to public using this way, what's the difference. Now the variable is public... He just did the same thing, except now he made like 9 or 10 MORE lines of code which seems the opposite direction that a programmer would like to go. Less is more usually.

Can someone explain why converting a private variable in a class to public within the class rather than just making it public right out the simple way is any different and more efficient?