Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Protected variables naming and the standard

Tags:

c++

c++11

I came across this post where an iterable queue was introduced. The OP used a protected variable called c from the std::queue in the implementation.

Is this totally valid? Would this variable have the same name over all implementations? In other words, does the standard state clearly that this variable must be named c?

like image 586
Humam Helfawi Avatar asked Aug 11 '16 09:08

Humam Helfawi


People also ask

What is the naming standard for a variable?

The choice of a variable name should be mnemonic — that is, designed to indicate to the casual observer the intent of its use. One-character variable names should be avoided except for temporary "throwaway" variables. Common names for temporary variables are i, j, k, m, and n for integers; c, d, and e for characters.

What are the 3 rules that need to be considered while naming a variable?

A variable name must start with a letter or an underscore character (_) A variable name cannot start with a digit. A variable name can only contain alpha-numeric characters and underscores ( a-z, A-Z , 0-9 , and _ ) Variable names are case-sensitive (age, Age and AGE are three different variables)

How do you set a protected variable?

Protected variables can be set by an extended class without any function support. Just go $this->protVariable = "stuff"; But you will need a function that may be protected to set ClassOne's private variable from the second class. Likewise a function must be made in ClassOne to actually retrieve its value.

How do you name a private variable?

We have to start a variable name with a double underscore to represent it as a private variable (not really). Example:- one, two, etc..,. As we already said the variables whose names start with a double underscore are not private.


1 Answers

For reference, the exact definition of std::queue is listed here. So in answer to

In other words, does the standard state clearly that this variable must be named c?

Yes, it is in this case (and it is similar for other container adapters);

template <class T, class Container = deque<T>>
  class queue {
  protected:
    Container c;
    // ...
  };

In general, however, the names of the protected and private names and members are not standardised since the types are not all built to be derived from and the implementation is an implementation detail (and doesn't form part of the public API); e.g. std::vector doesn't list any protected names.

Some std containers and classes do define the names of protected members, in particular the iostreams library comes to mind - basically the types that are intended to be derived from.


As a follow up - do all the compilers/libraries use c? It appears as though at least the mainstream ones do (libstdc++, libc++ and MSVC). libstdc++ is interesting in that it includes the follow comment on the variable;

/**
 *  'c' is the underlying container.  Maintainers wondering why
 *  this isn't uglified as per style guidelines should note that
 *  this name is specified in the standard, [23.2.3.1].  (Why?
 *  Presumably for the same reason that it's protected instead
 *  of private: to allow derivation.  But none of the other
 *  containers allow for derivation.  Odd.)
 */
_Sequence c;
like image 119
Niall Avatar answered Sep 28 '22 02:09

Niall