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
?
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.
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)
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.
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.
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With