Problems with the default constructor attributes initialisation

This is my 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
  #include <iostream>
using namespace std;

class dumb_array
{
		int mSize;
		int* mArray;
	public:
		//default constructor
		dumb_array(int size = 0);
			: mSize(size)
			, mArray(0)
		{
			if(mSize!=0){
			mArray=new int [mSize];
			}
		}
		//copy constructor (def: non-template constructor whos first argument is T&, const T&, volatile T&, const volatile T&)
		dumb_array(const dumb_array& other);

		//destructor
		~dumb_array(){
			delete [] mArray;
		}

};


There are several build problems.
At the beginning the compiler is complaining about the attribute initialization in the default constructor:
1) The type of size (int) is not recognized although I declared it in the input argument of the constructor.
2) It is not possible to assign the null value to the int* mArray

Does somebody know why I am not allow to do 1) and 2)?

Thanks.
Last edited on
Remove the ; at the end of line 10.
If you remove the ; at line 10 it will compile.

BTW Does an array size of 0 make sense? Sensible minimum value would be 2.
Thanks. Got it! No semicolon after the constructor when we want to initialize attributes.

Does an array size of 0 make sense? Sensible minimum value would be 2


Why do you say that the sensible minimum value would be 2?
In my point of view if the constructor is instantiated without specifying the size there is no array. Therefore size 0...
No semicolon after the constructor when we want to initialize attributes.

No. That's not the rule. The rule is that you don't put a semicolon after the signature of the function, in a function definition.

So:

1
2
3
4
class MyClass
{
  int MyMethod();  // <----- this is a declaration, so the semicolon goes here.
}


but:

1
2
3
4
5
6
7
class MyClass
{
  int MyMethod()  // <----- this is a definition, so no semicolon here.
  {
    return 0;
  }
}


It has nothing to with whether or not the function is a constructor, or whether or not you're using an initialisation list.
Last edited on
Thanks @MikeyBoy
You're welcome - I hope that made things clearer?
Yes, I understood that the constructor is also a function that does not return a value. So for the semicolon rule it is the same as functions. When defining a function don't put semicolon after the signature =).

Why do you say that the sensible minimum value would be 2?
In my point of view if the constructor is instantiated without specifying the size there is no array. Therefore size 0...

Why would you create an array that you can't use? Just to waste CPU cycles?
If the size of the array is somewhere calculated zero might be just a bug.
An array with size one makes also no sense since you can save resources and use a single var.
Well, don't std::string and std::vector (just for example) by default get constructed with a size of zero? Though of course those have multiple different options, the empty object may or may not be the best choice depending on the situation.
Well, don't std::string and std::vector (just for example) by default get constructed with a size of zero?
I think the difference with string and vector is that you can add elements to them so a size zero is a good choice.
Topic archived. No new replies allowed.