5 Replies - 5104 Views - Last Post: 27 May 2009 - 06:10 PM Rate Topic: -----

#1 machoolah  Icon User is offline

  • D.I.C Head

Reputation: -6
  • View blog
  • Posts: 87
  • Joined: 17-May 09

dynamic arrays as class members

Posted 27 May 2009 - 04:47 PM

Hello,

I am a totally novice programmer. I want to define a dynamic array within a class (index is a pointer to the dynamic array I want to define). The code I use follows. My program crashes when run.
The question I have is if it is at all possible to use dynamice arrays within a class, if yes, I am on the right track? and again if I am not on the right track, what is an appropriate method to do that?

I appreciate any help.

in the meanwhile, don't hesitate to ask if you need further details.

Thank you all,

#include <iostream>
using namespace std;
class NODE
{
public:
	int * index;

	NODE (int dim)
	{
		int * index = new int [dim];
	};
};

int main()
{
	NODE sampleNode(5);
	sampleNode.index[0]=1;
	cout << sampleNode.index[0];

	return 0;
}


This post has been edited by machoolah: 27 May 2009 - 04:54 PM


Is This A Good Question/Topic? 0
  • +

Replies To: dynamic arrays as class members

#2 Mowgef  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 245
  • Joined: 01-May 09

Re: dynamic arrays as class members

Posted 27 May 2009 - 05:11 PM

#include <iostream>
using namespace std;
class NODE
{
public:
	int *index;

	NODE (int dim)
	{
		index = new int[dim];
	};
};

int main()
{
	NODE sampleNode(5);
	sampleNode.index[0]=1;
	cout << sampleNode.index[0];

	return 0;
}


Try this instead.
Was This Post Helpful? 1
  • +
  • -

#3 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: dynamic arrays as class members

Posted 27 May 2009 - 05:32 PM

MEMORY LEAK!!!!!!!!

You need a destructor to free the memory:

~NODE()
{
	 if (index != NULL) delete [] index;
}


Was This Post Helpful? 0
  • +
  • -

#4 machoolah  Icon User is offline

  • D.I.C Head

Reputation: -6
  • View blog
  • Posts: 87
  • Joined: 17-May 09

Re: dynamic arrays as class members

Posted 27 May 2009 - 06:00 PM

MowGef, thank you,

that solved my problem.

KYA, thank you too, the node won't be deleted until the program ends. still thank you for reminding me that I need a destructor for safe computing!
Was This Post Helpful? 0
  • +
  • -

#5 crazyjugglerdrummer  Icon User is offline

  • GAME OVER. NERD WINS.
  • member icon

Reputation: 119
  • View blog
  • Posts: 690
  • Joined: 07-January 09

Re: dynamic arrays as class members

Posted 27 May 2009 - 06:02 PM

Yes, you can definitely use dynamic arrays in classes. You could use a vector (but that's slightly different). I think whenever you declare an array, you're actually declaring a pointer to it, so you could get away with

int index[];

NODE (int dim)
{
index = new int[dim];
} <--- no semicolon here

I'm not 100% sure about the code, see if it compiles
Was This Post Helpful? 0
  • +
  • -

#6 scooterfort  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 24-May 09

Re: dynamic arrays as class members

Posted 27 May 2009 - 06:10 PM

View Postcrazyjugglerdrummer, on 27 May, 2009 - 05:02 PM, said:

Yes, you can definitely use dynamic arrays in classes. You could use a vector (but that's slightly different). I think whenever you declare an array, you're actually declaring a pointer to it, so you could get away with

int index[];

NODE (int dim)
{
index = new int[dim];
} <--- no semicolon here

I'm not 100% sure about the code, see if it compiles

:angry: you forgot your code braces!!! :P
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1