0

How do i make an array class that contains pointers to objects? (in this case, object of type travel.) right now all i got is array of object, not pointer......

class Array
{
public:
	Array();
	Array(int size);
	Array(const Array & anArray);
	~Array();

	friend ostream& operator<< (ostream& out, const Array & anArray);
	const Array & operator = (const Array & anArray);
	int getSize (void)const;
	void setElem (int index, const travel & aTrip);
	travel & getElem (int index) const;
	


private:
	travel * travelArray;
	int size;

};
Array::Array()
{
	travelArray = new travel[1];
	size = 0;
}

void Array::setElem(int index, const travel &aTrip)
{
	if (index >= 0 && index <= size)
	{
		travelArray[index] = aTrip;
	}

	else
		cout <<"INVALID INDEX"<<endl;
}

i'm thinking

private:
{
     travel ** travelArray;
.......

Am I even close?
thanks

2
Contributors
1
Reply
2
Views
8 Years
Discussion Span
Last Post by dougy83
0

yes. You'll be using travel ** travelArray That said, your assignment line will of course be travelArray[index] = &aTrip; I noticed you check if index <= size. If the array declared has size elements, this will be a problem; if the array declared has size+1 elements, then it is not a problem.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.