1

I'm working on a project for school and I've hit a bit dead end. Part of the project requires us to have a class that uses an array. We must use an array (sadly we can't use vectors). I'm trying to figure out how to construct an array in the class at run time. I don't need to actually put anything in it initially, i just need to constructor to make the array a certain size. Any feed back or help is greatly appreciated. Here's what i have so far for the class and the constructor. This project is being done in c++.

#pragma once
#include <iostream>
#include <string>
using namespace std;

class Node
{

public:
    int size;
    string container[];

    Node ( int s, string c[]);
};

Node::Node (int s, string c[])
{
    size=s;
        ***I need something here that will give string container[] the size of "size"***
}

Thank you in advance.

Xeo
  • 129,499
  • 52
  • 291
  • 397
user1185736
  • 27
  • 1
  • 6
  • "(sadly we can't use vectors)" -- run away from there. – Xeo Mar 08 '12 at 23:28
  • 1
    @Xeo: at some point, they need to learn how to use array `new` and array `delete`. You can't say you taught someone how to code in C++ if they are helpless without the STL. – StilesCrisis Mar 08 '12 at 23:32
  • @trutheality: `calloc` wouldn't help here. – StilesCrisis Mar 08 '12 at 23:32
  • @StilesCrisis: Sure, but that should be a seperate topic, and it seems to be conflated with a tree. It's better to learn it seperated by building your own `vector`. – Xeo Mar 08 '12 at 23:33
  • @StilesCrisis Well, technically it *could*, but yes, `new` is better. – trutheality Mar 08 '12 at 23:39
  • this project is about array lists and linked lists. For now, im just trying to get the constructor working and i'll end up changing array to a template. – user1185736 Mar 08 '12 at 23:41

3 Answers3

2

You need a dynamically allocated array:

class Node
{

public:
    int size;
    string* container;

    Node ( int s, string c[])
    {
       container = new string[s];
       //copy c to container
    }
    ~Node ()
    {
       delete[] container;
    }
};

Also, remember to free the memory in the destructor.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • And don't forget the [Rule of Three](http://stackoverflow.com/questions/4172722/what-is-the-rule-of-three). – Xeo Mar 08 '12 at 23:35
0

I would use a pointer. When you get the size just call new with that size.

char* myArray;

constructor(int size) {
    myArray = new char[size];
}

You'll have to call delete in the destructor as well.

evanmcdonnal
  • 46,131
  • 16
  • 104
  • 115
-1
class Node 
{ 

public: 
    int size; 
    string* container; 

    Node ( int s, string c[]); 
    ~Node() { if (container != NULL) delete [] container; }
}; 

Node::Node (int s, string c[]) : container(NULL)
{ 
    size=s; 
    container = new string[size];
    // Copy list values into container
} 
Nick Banks
  • 4,298
  • 5
  • 39
  • 65
  • Why `if (container != NULL)`? And why are you assigning the `container` to NULL in the initializer list? – Luchian Grigore Mar 08 '12 at 23:32
  • Why not check if NULL? What if he changes the code later to add a constructor and doesn't intialize the list. What if the allocation fails? – Nick Banks Mar 09 '12 at 00:19
  • If the allocation fails, an exception is raised and the object doesn't get constructed, and so the destructor doesn't get called. (unless I'm having a terrible lapse of memory tonight....) The thing to worry about, actually, is the allocation *succeeding*, but an exception gets raised later in the constructor. –  Mar 09 '12 at 01:02
  • Whether new returns NULL, or throws an exception depends on the environment built in: http://msdn.microsoft.com/en-us/magazine/cc164087.aspx. For example, I being a Windows/WP OS developer all allocations we do with new are checked for NULL, not exceptions. – Nick Banks Mar 09 '12 at 17:30