0

I need to print out a bunch of Objects from a dynamic array using the overloaded << operator.

I've overloaded the << operator this way:

ostream& operator<<(ostream& os, Person* dt)
{
    return os << dt->getName();//getName returns a char*
}

Now this works just fine with regular pointers, however as soon as I try doing something like this:

    Person *p = new Person("Dany", 4);
    Object** obj=new Object*[sizeof(p)];
    obj[0] = p;
    cout << obj << endl;

I get a meaningless value. Keep in mind the Person class is a derivative of the abstract Object class.

Alex
  • 85
  • 1
  • 8
  • 1
    Well does `Object` have a `getName()` member, and do you have an `operator<<` for `Object*`? If not, it will just print the pointer value - some memory address, like `0x20a1937d`. – BoBTFish Jan 18 '16 at 16:31
  • You only overloaded the output operator for pointers to `Person` , not for a pointer to a pointer to `Object`. – Some programmer dude Jan 18 '16 at 16:35
  • 1
    Also, `new Object*[sizeof(p)]` is *very* suspect, and will not do what you think it does (I think). – Some programmer dude Jan 18 '16 at 16:36
  • 4
    Why all these pointers, bro? – Lightness Races in Orbit Jan 18 '16 at 16:37
  • *I get a meaningless value* -- `sizeof(p)` is the same as `sizeof(Person *)`, or basically the size of a pointer (which is usually 4 or 8). Is this what you wanted to do? -- *regular pointers* -- So what would you consider an irregular pointer? – PaulMcKenzie Jan 18 '16 at 16:43
  • you overloaded << for a pointer, you can't expect it to find himself how to work on a pointer of pointer, have you ever try to add two int by adding their respective addresses ? if it's overloaded for a Person * , just give it a Person * – Guiroux Jan 18 '16 at 16:45
  • Well, Object is supposed to be an abstract class(I guess I should have mentioned that) and from what I gathered you can't overload operators there. The sizeof(p) thing will be fixed. – Alex Jan 18 '16 at 17:04
  • @BobTFish `obj` is `Object**`, not `Object*` – M.M Jan 18 '16 at 20:48

1 Answers1

0

the problem is that a person is an object(according to you, we can get philosophical about it but ok) and so he have access to all of the protected and public methods of an object. but an object is not a person so it dos not know how to use methods defined in person(such as the operator).

you can directly convert a person to an object, as you did. but not an object to a person, like you tried to imply.

usually one will use a virtual function to produce the behavior you are looking for but as you said this it tricky, because operator<< is a friend function and thous cannot be virtual. luckily a nice technique to make << virtual was posted some time ago.

rustypaper
  • 70
  • 10
  • From what I understood you can't have a virtual << overload in an abstract class. I've attempted it and got an error saying that the function has too many parameters. – Alex Jan 18 '16 at 17:11
  • yes i was totally wrong, [the solution](https://stackoverflow.com/questions/4571611/making-operator-virtual) was posted by Loki Astari. – rustypaper Jan 18 '16 at 18:47
  • Id figured that it's gonna be something like that, thats too bad. Thanks. – Alex Jan 19 '16 at 07:01