473,325 Members | 2,870 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

Print names of polymorphic objects?

I am experimenting with polymorphism and have a list of polymorphic
objects. I want to be able to print out the list of objects. How do
I do this?

Do I need to implement eg an GetName member function? Or is there an
inbiult way to print? Eg some way to convert an object to a string
name?
Jul 14 '08 #1
2 1334
Angus wrote:
I am experimenting with polymorphism and have a list of polymorphic
objects. I want to be able to print out the list of objects. How do
I do this?

Do I need to implement eg an GetName member function? Or is there an
inbiult way to print? Eg some way to convert an object to a string
name?
http://en.wikipedia.org/wiki/Typeid
--
ToMo
Jul 14 '08 #2
Angus wrote:
I am experimenting with polymorphism and have a list of polymorphic
objects. I want to be able to print out the list of objects. How do
I do this?

Do I need to implement eg an GetName member function?
That's one way of doing it.
Or is there an inbiult way to print?
Not really.
Eg some way to convert an object to a string name?
You can define a string conversion operator, e.g.:

class Person
{
public:
operator std::string() const { return name; }
private:
std::string name;
};

Person person;
std::string some_string = person;

Of course, this is not all that different from defining a "GetName"
member function, just that the syntactic shortcut may lead to unexpected
problems in certain situations, which is why you should use it with caution.
However, I think what you are really looking for is a way to send your
objects to an output stream like this:

std::cout << person;

You can achieve this by overloading operator<<. Here's more information
on how to implement it for a hierarchy of classes:

http://www.parashift.com/c++-faq-lit...html#faq-15.11
--
Christian Hackl
Jul 14 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
by: stewart.midwinter | last post by:
No doubt I've overlooked something obvious, but here goes: Let's say I assign a value to a var, e.g.: myPlace = 'right here' myTime = 'right now' Now let's say I want to print out the two...
7
by: James Fortune | last post by:
In response to different users or situations (data context) I transform the appearance and characteristics of Access Forms through code. This seems to fit in with the idea of polymorphism. Do...
1
by: Michal Piatkowski | last post by:
I have a problem with serialization of a polymorphic array. It has attributes defined XmlArray and XmlArrayItem for XML serialization. The attribute specifies Type1 but as my array is polymorphic...
8
by: Angelwings | last post by:
Hi everyone, I've to write my own definition of a BST with polymorphic data, as an university course project. I have troubles about comparing data when it's defined as polymorphic pointer. In my...
4
by: Owen Zhang | last post by:
I am trying to debug a problem using dbx in sun 5.9. I want to print the virtual table from dbx. Find the real address of the virtual function of the object.
7
by: karl | last post by:
I've got a abstract class CBase and two classes CPoly1 and CPoly2 that inherit from CBase. Is there a way to find out the classtype (CPoly1 or CPoly2) when iterating over a list of objects for...
7
by: Arindam | last post by:
#include <cstdio> struct Test { void bar() { foo(); } private: virtual void foo() { printf("Test\n"); }
2
by: Daniel Pitts | last post by:
I'm trying decide on the best way to structure the memory management in my program. I have a class (lets call it World), which contains a collection of Entity objects. Entity in turn,...
7
by: bukzor | last post by:
I was trying to change the behaviour of print (tee all output to a temp file) by inheriting from file and overwriting sys.stdout, but it looks like print uses C-level stuff to do its writes which...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.