views:

171

answers:

5

New to C++, and for that matter OO programming.

I have a class with fields like firstname, age, school etc. I need to be able to store other information like for instance, where they have travelled, and what year it was in. I cannot declare another class specifically to hold travelDestination and what year, so I think a struct might be best. This is just an example:

struct travel {
    string travelDest;
    string year;
};

The issue is people are likely to have travelled different amounts. I was thinking of just having an array of travel structs to hold the data. But how do I create a fixed sized array to hold them, without knowing how big I need it to be?

Perhaps I am going about this the completely wrong way, so any suggestions as to a better way would be appreciated.

Edit: I realise there is essentially no difference between a class and struct, but for the purposes of assignment criteria I am not allowed a "class", so yeah.

A: 

To create an array of 100 elements you can do this:

travel* pTravelArray = new travel[100];

You can make that variable

int numberOfElements = 100;
travel* pTravelArray = new travel[numberOfElements];

Just don't forget to get rid of it when you're done:

delete [] pTravelArray;

As other have suggested though, the STL library has some much nicer (less easy to do something stupid with memory allocation) collections in it you can use.

Jon Cage
Please use a `vector` instead. This code with `new[]/delete[]` should be taken as the reason to do so, not as a suggestion ;-)
Steve Jessop
The problem with this approach, is I won't know how many places the person has travelled, beforehand. And I don't really want to allocate a fixed number HOPING it's enough.
Dominic Bou-Samra
I see what you're getting at, but in fairness you did ask "how do I create a fixed sized array to hold them" :-)
Jon Cage
+1  A: 

Try learning about the C++ Standard Template Library (STL). For example, you could use a list.

Peter K.
+7  A: 

You could try associating a std::vector with each person, with each entry in the vector containing a struct:

typedef struct travel {
    string travelDest;
    string year;
} travelRecord;

std::vector<travelRecord> travelInfo;

You can then add items to the vector as you see fit:

travelRecord newRecord1 = {"Jamaica", "2010"};
travelInfo.push_back(newRecord1);

travelRecord newRecord2 = {"New York", "2011"};
travelInfo.push_back(newRecord2);

Some more information about vector operations can be found here.

LeopardSkinPillBoxHat
`std::vector<travel>` does the job. The separate space for struct names does exist in C++, but you don't need to use it.
Steve Jessop
typedef and class names live in the same namespace in C++. `struct A { int dummy; }; typedef int A;` is not possible in C++ like it is in C. If you write `struct A { }; typedef struct A A;` in C++ (which as a special case is allowed) it *redefines* that name: Was it a class-name before, it is then a typedef-name in addition. There is no class name that isn't also a typedef name anymore afterwards.
Johannes Schaub - litb
@Steve - I'm not sure that I understand your comment. What do you mean by "separate space for struct names"?
LeopardSkinPillBoxHat
(so on a very pedantic C++ compiler, `typedef struct A { } A; struct A a;` will fail to compile, because the elaborated type specifier `struct A` will resolve the identifier to a typedef-name (see 7.1.5.3/1). However, none of comeau/EDG, gcc and clang do that - presumably because it would break a lot of code).
Johannes Schaub - litb
Ah, thanks litb. I should have said, "the ability to stick `struct` before a class name does exist in C++..." ;-).
Steve Jessop
@Steve @Johannes - thanks that makes more sense to me now :-)
LeopardSkinPillBoxHat
@Steve see this issue report which they haven't resolved yet: http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#407 :)
Johannes Schaub - litb
+1  A: 

You probably want to use one of the STL container classes to hold your struct (or better yet, refactor your struct into a class to hide the data members and make get/set accessors)

std::vector

std::list

...

Which one you pick depends entirely on what operations you anticipate doing on them. A vector is fine for basic storage, and random access. If you want to perform any sorting or searching functions, you probably want a list, or potentially even a map or multimap.

Here's a good starting point for more information on STL: http://www.parashift.com/c++-faq-lite/class-libraries.html

holtavolt
+1  A: 

I would agree with the others, use an stl container like list. A list (or any other container) will grow as you push data onto it, like this:


#include <list>
#include <string>
using namespace std;

struct travel {
    string travelDest;
    string year;
};

int main() {
    list<travel> travels;
    travel t = {"Oslo", "2010"};
    travels.push_back(t);
}

Also note that while you are saying you cannot create a class, you are perfectly willing to create a struct. In C++, these are for all practical purposes the exact same thing, except that struct variables are public by default, while class variables are private by default.

knatten