Class: Instantiate array in constructor?

Hello, I'm sort of new to C++. I've written C++ in primitive codes before and then learnt Java because of course requirement.

I need confirmation of am I heading in the right direction. I've look at quite a lot of places but I can't seem to find a good example on instantiating an array of int type in a class with a header file.

Header code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
private:
  		static const int DEFAULT_CAPACITY = 10;
  		int num[DEFAULT_CAPACITY];
  	public:
  		/** Default constructor
		*/
   		Stuff();
	
		/** Overloaded constructor
	 	 * @param integer value used to initialize the class data member
	 	 */
   		Stuff(int[]);
   		/** Accessor method
	 	 * @return integer value
		 */
   		int getNum();
   		/** Mutator method: changes the private class member
	  	  * @param integer value
	  	  */
   		void setNum(int[]);


.cpp code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

  /** Default constructor
		*/
   		Stuff()
   		{
   			num[DEFAULT_CAPACITY] = 0;
		}
	
		/** Overloaded constructor
	 	 * @param integer value used to initialize the class data member
	 	 */
   		Stuff(int[] newNum)
   		{
   			newNum[DEFAULT_CAPACITY] = num[DEFAULT_CAPACITY]
		}
		   
   		/** Accessor method
	 	 * @return integer value
		 */
   		int getNum()
   		{
   			return num[DEFAULT_CAPACITY];
		}
		   
   		/** Mutator method: changes the private class member
	  	  * @param integer value
	  	  */
   		void setNum(int[] newNum)
   		{
   			num[DEFAULT_CAPACITY] = newNum[DEFAULT_CAPACITY]
		}
Careful in your default constructor. You're attempting to access the tenth index of 'num', but that is an invalid index. Perhaps what you meant to do was to iterate through the array and set each element to zero?
Seems I'm in for a steep learning curve.

Yes, I was wondering would a for loop work to iterate each element to zero. I'm not sure if my call to DEFAULT_CAPACITY through the constructor is valid either.

This is how I would iterate through java
1
2
for(int i = 0; i < years.length; i++)
         this.years[i] = years[i];


What would be the equivalent usage in C++?
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.
Last edited on
Topic archived. No new replies allowed.