> JLBorges, why do you use struct instead of class?
> Is it just a sense of what different programmers like or is there a difference?
When the keyword
struct
or
class
is used to declare or define a user-defined type, there is only one difference: the default access specifier for members and base classes is
public
with
struct
and
private
with
class
.
If the coding convention specifies that public members of a class must be declared at the beginning, I use the keyword
struct
; if it specifies that private members must be declared first, I use the keyword
class
. What you use depends your personal preference, and that of the other programmers who work on the same program.
It is a non-difference similar to:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
int* pointer = nullptr ;
/* or */
int *pointer = nullptr ;
if( condition ) {
// ...
}
/* or */
if( condition )
{
// ...
}
|
Just be consistent in whatever you choose. Once you start reading real-life code, once you are past the amateur stage, you would have to read, write and maintain code written using different styles - K&R, Allman, and so on. Very soon, you would realize that code written in a consistent style is both readable and understandable; it doesn't really matter what the style is as long as it is consistent.
If you modify or extend a program that you didn't write, preserve the style that is already there - the program's overall consistency of style is more important than your own predilections. Ideally, don't waste time over arguments about
struct
vs.
class
,
int* p
vs
int *p
, which brace and indentation style to use etc.