Help creating a pointer to a dynamic integer array

Hi guys,

I'm having a little trouble understanding part of my assignment.

The part is: Create in the private section of the ServerGroup class a pointer to a dynamic integer array called servers.

I've been reading through my book and can't really find much on the subject. Does this look right to anyone?

1
2
3
4
5
6
7
8
9
10
11
#ifndef ServerGroup_h
#define ServerGroup_h

class ServerGroup
{
public:
private:
  int *ptr = new int; // needs pointer to a dynamic integer array called servers
};

#endif  
I think C++11 allows you to initialize your data variables in the class definition, but I've never been partial to it. But yes, it is correct... if I wasn't wrong about C++11.

Edit:
Oh, but you have a pointer to a single int, not an array.
 
Type* ptr = new Type[N];
Last edited on
If you are not updated to C++11 then maybe try doing this in the constructor:

1
2
3
4
5
6
7
class ServerGroup
{
public:
    ServerGroup(int size) : servers(new int[size]) {}
private:
   int *servers;
};
Last edited on
Topic archived. No new replies allowed.