I just switched universities and am taking a data structures class now. I have the prerequisite intro to OOD class from my previous university, but they only taught Java, and this class has us do all projects in both Java and C++.
I'm trying to make a fairly simple linked list class but I want its link to the next element to be empty until the constructor initializes it.
Here's a simplified version to illustrate the problem:
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
|
class foo {
int val;
foo bar;
public:
foo (int[]);
int getValue() {return value;}
int getSum();
private:
foo() {val=0;}
}
foo::foo(int[] arr) {
val = arr[0];
// if more entries in arr, assign bar to be a new foo(rest of arr)
// otherwise make bar a new foo()
}
int foo::getSum() {
int temp = 0;
if(val!=0) // if val is zero, assume this the last link in the list
temp = bar.getSum();
return temp+val;
}
|
I just want to set aside a location in memory called bar for a foo object, without actually assigning it any initial content, the same way I can for the int val. If it automatically creates a new foo to go in bar, then it will of course make a foo bar' to go in bar, a foo bar'' to go in bar', and so on ad infinitum.
Should I be using pointers for this? I'm not really familiar with them since, as I said, all my past experience is with Java. Any help is appreciated!