Accessors/modifiers naming convention

O

Ook

Is there any kind of naming convention for accessor and modifiers? What I've
been doing is something like this:

// accessor
int getSize();

// Modifier
void setSize( int newsize);

private:
int _size;
 
K

Kristo

Ook said:
Is there any kind of naming convention for accessor and modifiers? What I've
been doing is something like this:

Naming conventions are a coding standards concept, which is something
not covered by the C++ standard. I believe the FAQ has links to a few
good ones that you could take a look at.
// accessor
int getSize();

// Modifier
void setSize( int newsize);

Sure, a lot of people name accessor/modifier functions this way.
private:
int _size;

Now *that* isn't allowed. Leading underscores are reserved for the
implementation. I suggest changing that to size_.

Kristo
 
O

Ook

private:
Now *that* isn't allowed. Leading underscores are reserved for the
implementation. I suggest changing that to size_.

Kristo

Seriously? I've always been taught that you should use leading underscores
for your private variables.
 
K

Kristo

Ook said:
Seriously? I've always been taught that you should use leading underscores
for your private variables.

You've been taught wrong. ;-) *Trailing* underscores, however, are a
common practice to indicate private member variables.

Kristo

P.S. Please attribute your quotes when posting a follow-up.
 
R

red floyd

Ook said:
Seriously? I've always been taught that you should use leading underscores
for your private variables.

Technically no. Per 17.4.3.1.2/1, identifiers with a leading underscore
and a *LOWER CASE* letter are only reserved in the global and std
namespaces.

Per 17.4.3.1.2/1, identifiers witha leading underscore followed by an
UPPER CASE letter are reserved, period.

In either case, you're better off not using leading underscores at all.
 
G

Greg Comeau

Naming conventions are a coding standards concept, which is something
not covered by the C++ standard. I believe the FAQ has links to a few
good ones that you could take a look at.


Sure, a lot of people name accessor/modifier functions this way.


Now *that* isn't allowed. Leading underscores are reserved for the
implementation. I suggest changing that to size_.

Not quite, there is some rules about how to use leading underscores,
and the above does not violate them as per any requirements of
Standard C++. However, it may violate other standards, or some
other convention, and besides, it's easier to not have to remember
the Standard C++ rules, so in short, the above is probably best
avoided, and instead some other convention be used (like trailing _'s)
if indeed some convention at all is necessary.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top