954,504 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

class Array of pointers to objects

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

Kanvas
Newbie Poster
9 posts since Mar 2006
Reputation Points: 10
Solved Threads: 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.

dougy83
Posting Whiz in Training
275 posts since Jun 2007
Reputation Points: 85
Solved Threads: 45
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You