Re: [Dev-C++] public/private data decision
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: <or...@vp...> - 2004-04-13 09:24:10
|
Krzysztof Kapuscik wrote: > But for above example you could use: > return (c1.Save() && c2.Save() && c3.Save()); >=20 > Just remember (yeah, I know you remember this) that && operators are > evaluated as long as there is 0 value as operator argument, so if > you'll get c2.Save() =3D=3D false then c3.Save() will never be done (fo= r > all standard complaint compilers - if I good remember, I'm not so > sure, 'cause it is 1:38 AM in my time zone ;) ). Yes, it is ANSI standard, the shortcut-evaluation, already present in C. I have been reading here and there about this public-private stuff, but=20 still, there are very diverse styles. I have read this somewhere: -- Class variables should never be declared public. The concept of C++ information hiding and encapsulation is violated by=20 public variables. Use private variables and access functions instead.=20 One exception to this rule is when the class is essentially a data=20 structure, with no behavior (equivalent to a C struct). In this case it=20 is appropriate to make the class' instance variables public [2]. Note that structs are kept in C++ for compatibility with C only, and=20 avoiding them increases the readability of the code by reducing the=20 number of constructs used. Use a class instead. -- It states that use public data ONLY if the whole thing is a data class=20 (a struct). But what if there ARE a few methods present for the class,=20 but mostly, it contains data? Another question: I used the m_ (or simply m) prefix for variables in a=20 class, and they were mostly private or protected. What if I put the data=20 into the public section? Should I still use this (or other) prefix for=20 those too? (I don't use any other prefixes. Though I'm Hungarian, I DON'T use=20 Hungarian-notation :) -- Greetings, Bal=E1zs P.S.: Maybe i spend too much time pondering on unimportant things... |