Re: [Dev-C++] public/private data decision
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Daniel K. O. <dan...@ya...> - 2004-04-12 22:27:06
|
OROSZI Balázs wrote: > Suppose I have a class, which has some data. Mostly string data. Like > name, path, etc. Uptil now I always put these things into the private > part of my class, and I provided Get/Set methods for accessing them. > I ask: does it worth the extra overhead? I mean, what if I simply put > the data into the public section, and then I have 2 (or more) member > functions less, and the same access to the data. > > Can someone with more experience with C++ give some guidelines to me > about this. Posting personal experience would be the best! I recomend you to go search on Stroustrup's website for his interviews. I remember he answering your questions. It was almos like this: if you need to keep an invariant (like the rules about your data, to make sure it's always valid) then you have a class, with private members and set/get methods. But if all you need is to group the data togheter and there's no special invariant to take care of, you actually have a plain struct, and probably there's no need to the get/set methods. Stroustrup is (obviously) much more objective than me, so better read what he said about this. :) Daniel K. O. |