Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get/Set in the c++ world, faux-pas?

Tags:

c++

oop

I notice that get/set is not the c++ way as far as I can tell by looking at boost/stl, and even reading the writings of some of the top c++ experts.

Does anyone use get/set in their c++ class design, and can someone suggest a rule of thumb on where if at all this paradigm belongs in the c++ world?

It is obviously very popular in Java, and imitated by c# using Property syntactic sugar.

EDIT:

Well after reading more about this in one of the links provided in an answer below, I came across at least one argument for keeping the accessors of a member field the same name as the member field. Namely that you could simply expose the member field as public, and instead make it an instance of a class, and then overload the () operator. By using set/get, you'd force clients to not only recompile, but to actually change their code.

Not sure I love the idea, seems a bit too fine grained to me, but more details are here:

C++ Killed the Getter/Setter

like image 799
ApplePieIsGood Avatar asked Dec 16 '08 21:12

ApplePieIsGood


1 Answers

C++ does not have properties like C#. It is possible to write methods with "get" and "set" in the their names, but they don't automatically create properties. So is it good practice to use get and set methods then?

Using get and set methods in C++ is not bad for accessing class properties. The STL and boost (which was influenced by the STL) just chose one design -- it's perfectly fine to have a different design for your classes.

At my job, we do use get and set. The reason is primarily that it makes it possible to write parsers to automatically wrap the C++ classes by other interfaces -- ActiveX, .NET, LabVIEW, Python, etc.... When using Visual Studio's intellisence (and similar technologies on other IDEs), I find that using get and set methods makes finding what I was easier. So there are times where it is benefficial to use get and set methods.

The most important thing is to choose a code style standard for your library and be consistent with it.

like image 192
Uhall Avatar answered Oct 07 '22 16:10

Uhall