1. In C++,
this
is a pointer, not a reference. Use the indirect member access operator:
2. Array-to-pointer conversion (AKA "array-type decay") means it is conventional to pass a size argument along with a pointer to an array's first element; there's no "length" member associated with a pointer. You can avoid this issue by templating the array size and passing the array by reference, but this is unconventional and often impossible.
[/code]
Here's one equivalent:
1 2 3 4
|
void my_class::foo(int const* const years, std::size_t const size) {
for(int i = 0; i < size; ++i)
this->years[i] = years[i];
}
|
-- Stupidly, array brackets appear after the identifier at the point of declaration.
type identifier[size_constexpr];
-- Arrays require some understanding of pointer conversions and arithmetic. Because of these drawbacks, raw arrays should be avoided in favor of
std::vector
or
std::array
.
std::vector
is a dynamically-sized array object;
std::array
is an aggregate fixed-size analogue.
--
std::vector
is the preferred container.
-- Both objects are templates, not generics; templates are more powerful than Java's generics and incur no runtime cost.
-- A significant design goal of C++ is to ensure that user-defined types can be made to appear syntactically equivalent to ("syntactically dual") to primitive types. Therefore if you had the following function:
void my_class::bar(std::vector<int> years) { this->years = years; }
No loop is required and in this case a deep copy is performed; both objects remain valid.
-- It is bad practice to name symbolic constants in ALL_UPPER_CASE. Names in this style conventionally refer to preprocessor macros, which have different semantics.
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Res-ALL_CAPS
-- You should replace the
const
in symbolic constants in favor of
static constexpr
.
constexpr
is a "stronger" version of
const
, and
static
should almost always go with
constexpr
(but
not often with
const
).
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#con5-use-constexpr-for-values-that-can-be-computed-at-compile-time
-- Prefer initialization to assignment in constructors. @xismn already pointed out the out-of-bounds access, but this is how you would zero-initialize the elements of an array:
Stuff::Stuff(): my_array() {}
(std::vector is constructed empty by default.)
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Rc-initialize
-- Trivial accessors and mutators should be avoided.
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Rh-get
-- Mark methods (e.g., accessors)
const
if possible.
P.S.:
There's a well-known tool called Doxygen which uses very similar (almost identical, once configured) syntax to Javadoc, so if you don't want to give up on an in-source documentation generator that's the way to go.
http://www.stack.nl/~dimitri/doxygen/
P.P.S.: If you don't have anything to say about a method, don't document it.