0

I have an array of pointers to objects

Room *rooms[MAX_ROOMS];

rooms[0] = new Room(101, 1, RT_CLASSIC, 200.00);
rooms[1] = new Room(102, 2, RT_CLASSIC, 280.00);
rooms[2] = new Room(103, 4, RT_FAMILY_SUITE, 360.00);

Class Room has an overloaded friend operator <<:

std::ostream& operator<<(std::ostream &out, const Room &room) {
    return out << room.toString();
}

How do I output rooms array like this:

for(int i = 0; i < num_of_rooms; i++) {
    cout << rooms[i] << "\n";
}

Because now it outputs addresses to Room objects. I need it to call my Room << operator.

Thank you for your answers.

3
  • Why pointers and why new... this is painful code. Commented Oct 23, 2012 at 15:43
  • @Kerrek SB Because I use function CreateRoom() for creating rooms which adds a new room to rooms array therefore I must use new Commented Oct 23, 2012 at 16:38
  • Hmm... I don't think that's a real argument, but that's off topic. No worries. Commented Oct 23, 2012 at 16:42

1 Answer 1

4

Like so:

cout << *(rooms[i]) << "\n";

rooms[i] returns a pointer to a Room, that's why cout is printing the address. To get the object itself, you have to dereference it (like above).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.