-1

So this is the original code:

class IntegerArray { 
public:   
   int *data;   
   int size; 
};  
int main() {   
   IntegerArray arr;   
   arr.size = 2;   
   arr.data = new int[arr.size];   
   arr.data[0] = 4; arr.data[1] = 5;   
   delete[] a.data; 
}

After moving arr.data = new int[arr.size] to a constructor, it becomes

class IntegerArray { 
public:   
   int *data;   
   int size;   
   IntegerArray(int size) {     
      data = new int[size];     
      this->size = size;   
      } 
   };  
int main() {   
   IntegerArray arr(2);   
   arr.data[0] = 4; arr.data[1] = 5;      
   delete[] arr.data; 
}

I'm fairly lost on what the code is trying to do. For

IntegerArray(int size) {     
   data = new int[size];     
   this->size = size;   
   } 

Is int size just the same as the int size that was declared in the class IntegerArray?

Does data = new int[size] just tell us that data is pointing to the output of the array at int size, with new saying that the size of the array is variable?

Is this-> size = size just a pointer that tells us that the size value of the constructor is just equal to the size parameter of the class?

Why are arr.data[0] and arr.data[1] even mentioned after IntegerArray arr(2)? They don't seem to follow the constructor, but I'm probably just too tired to comprehend that part.

Liju Thomas
  • 1,054
  • 5
  • 18
  • 25
  • Why use `new` and `delete`? This is 2016! – Lightness Races in Orbit Jun 06 '16 at 09:30
  • Oh, is my reading source too old? I'm going off of MIT's slides for their C++ class. I thought arr[] meant returning the result for whatever number away from the beginning of the array, so I'm being thrown off by data = new int[size]. – AttemptingTo Jun 06 '16 at 09:32
  • You're not wrong but this is kind of an unsafe, confusing and error-prone way to do arrays in C++! You wouldn't have the problem if you used tools like `std::vector` (which has existed since 1998 and pre-standard for years before that!) This doesn't answer your question though. – Lightness Races in Orbit Jun 06 '16 at 09:33
  • @AttemptingTo Usually you would use a `std::vector` in c++. – πάντα ῥεῖ Jun 06 '16 at 09:33
  • @LightnessRacesinOrbit Could you elaborate on that? I'm not sure how to improve the code through std::vector, and it would greatly help my understanding if that's more common than the new operator. – AttemptingTo Jun 06 '16 at 09:37
  • http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list :) – Lightness Races in Orbit Jun 06 '16 at 09:40
  • I began answering the question, but there are too many fundamental misconceptions for it to be useful here. You should read up more about what function parameters are, what constructors are. e.g. "the size value of the constructor" makes no sense. Since you're "too tired", perhaps come back to this after some sleep :) – Lightness Races in Orbit Jun 06 '16 at 09:42
  • @LightnessRacesinOrbit Sorry, I think I'm just wording it poorly as well. I recognize that parameters would be the data and the size. The constructor is IntegerArray(int size) and it's a method that's called when the class is created. What I meant by size value of the constructor is the value of size in the constructor IntegerArray comparatively to the value of size in the class IntegerArray. – AttemptingTo Jun 06 '16 at 09:50
  • @Gibet Thank you. I think I understand everything except this->size = size still. So, it's written in that fashion in order to avoid confusion between size in the constructor, and size in the class, and to show that (int size) from the constructor would ultimately affect size in the class? – AttemptingTo Jun 06 '16 at 09:57
  • @AttemptingTo: That's right. – Lightness Races in Orbit Jun 06 '16 at 09:57
  • @LightnessRacesinOrbit In the bundle of comments I left, I'm lost as to which response I'm right about, haha. Are you referring to my response to Gibet in order to clarify, or my response to you? – AttemptingTo Jun 06 '16 at 10:02
  • @AttemptingTo: Both. – Lightness Races in Orbit Jun 06 '16 at 10:04
  • @πάνταῥεῖ Quite obviously, these are the first 3 lines of a *re-implementation* of something like a `vector`, so the suggestion to use `std::vector` misses the point. I would contend that that is a very instructive exercise which will touch a lot of non-trivial issues. – Peter - Reinstate Monica Jun 06 '16 at 10:13
  • @PeterA.Schneider I'm certainly not missing the point, as OP was asking what should be used instead of `new` and `delete` in modern c++. Using a `std::vector` would render the whole `IntegerArray` class useless though. – πάντα ῥεῖ Jun 06 '16 at 11:05
  • @πάνταῥεῖ The answer to the point would be to use a smart pointer in order to simplify moving and copying the container one is going to implement. – Peter - Reinstate Monica Jun 06 '16 at 11:59

2 Answers2

2
IntegerArray(int size) {     
    data = new int[size];     
    this->size = size;   
} 

Is int size just the same as ...

This int size:

IntegerArray(int size) {
             ^^^^^^^^

is an argument to the constructor

Does data = new int[size] just tell us ...

new int[size] dynamically allocates an array that contains size number of int objects. data pointer is then assigned to point to that newly created array.

Is this-> size = size just a pointer ...

No. this is a special pointer, that within the constructor points to the object that is being constructed. this->size is the member variable that was declared here:

class IntegerArray { 
public:   
   int *data;   
   int size;
   ^^^^^^^^

The complete expression this->size = size assigns the value size that is the constructor argument, to the member variable size.

Why are arr.data[0] and arr.data[1] even mentioned after IntegerArray arr(2)?

The constructor does not initialize the contents of the array (the integer objects within the array). The mentioned code does assigns a values to them.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • Well, no, the mentioned code doesn't "initialise" anything; it only assigns. – Lightness Races in Orbit Jun 06 '16 at 10:05
  • One could add that imo it is poor style to name arguments to member functions (here: `size`) the same as members of that class. The OP's confusion is not surprising. There are a couple naming conventions (although afaics there is no agreement in the the C++ community) to prevent that: Name public members with capital starting letters, name non-public members with a starting underscore, or name arguments with a suffix "Arg". All these would quite naturally avoid naming duplications. – Peter - Reinstate Monica Jun 06 '16 at 10:06
  • @LightnessRacesinOrbit fixed. – eerorika Jun 06 '16 at 10:07
  • 1
    @PeterA.Schneider: I've never heard of any of those conventions. They all sound bizarre to me. – Lightness Races in Orbit Jun 06 '16 at 10:09
  • 1
    In fact, if you write your constructor properly, you don't need any "tricks" to workaround the naming problem and can just use the normal, original, meaningful, simple names: `IntegerArray(const int size) : data(new int[size]), size(size) {}` – Lightness Races in Orbit Jun 06 '16 at 10:10
  • @PeterA.Schneider I don't consider it poor style. It might confuse people who have never programmed ALGOL family of languages, but the again, many things do. I personally prefer to not use any pre-/suffixes on member variables nor on arguments because they add unnecessary verbosity. I especially dislike prefixing with underscore because if accidentally mixed with the "convention" of starting capital, will lead to ill-formed code. – eerorika Jun 06 '16 at 10:14
  • @user2079303 You are right wrt the leading underscore -- a trailing one is more common. – Peter - Reinstate Monica Jun 06 '16 at 10:50
1
this->size = size //the fist size is your member variable which is inside IntegerArray. the 2nd size is the size you give over to the Method
data = new int[size];  // here you are creating a new array with the size of "size"

the method IntegerArray(int size) is a constructor method, you only can call it once per object (even at the time when you create the object)

int main() //the startpoint of your program
{   
    IntegerArray arr(2);   //you are creating a new object with a size of 2
    arr.data[0] = 4; arr.data[1] = 5;   //because of your array is public, you can call it direct from outside (you sould define it as private ore protected). arr.data[0] is the first item of the array, arr.data[1] is the 2nd item
    delete[] arr.data; 
} 

delete[] arr.data; should be inside the destructor of your class...

Thomas
  • 2,093
  • 3
  • 21
  • 40
  • You can never "call" a constructor yourself. And if you're going to suggest putting `delete[]` in the destructor, you _must_ also suggest putting a deep copy inside the copy constructor and assignment operator. – Lightness Races in Orbit Jun 06 '16 at 09:57
  • @LightnessRacesinOrbit As the class stands it's broken anyway (assignment leaks), so we can assume that one shouldn't assign or copy these arrays. (I assume that fixing that will be next week's lesson.) – Peter - Reinstate Monica Jun 06 '16 at 10:11
  • @Peter: Then we should state that in the answer rather than providing dangerously broken code to somebody who doesn't know better! Rather than just leaving it to someone else to maybe inform them about it at some time in the future maybe. – Lightness Races in Orbit Jun 06 '16 at 10:18