473,320 Members | 1,965 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,320 software developers and data experts.

object to string

hi
i have a function which returns object.
how to get the name of the object .
Jan 29 '08 #1
7 1920
sicarie
4,677 Expert Mod 4TB
Give the object a getName() method.
Jan 29 '08 #2
weaknessforcats
9,208 Expert Mod 8TB
I'm sorry, but of your object returns an object, then you already have the name of it. It's the return type. If you are asking about the name of the object in your source code, then you have to remember that there is no source code at run time.

Exactly, what are you trying to do??
Jan 29 '08 #3
thanks for reply
there are two classes CLASSA and CLASSB.
object of CLASSA is OBJA.
object of CLASSB is OBJB.

now
CLASSA contains function FA()(return type of FA()is string) which returns(OBJA.FA()) the name of object(ie.string, not the object itself)

how to convert this string to object.
Jan 30 '08 #4
weaknessforcats
9,208 Expert Mod 8TB
If OBJA.FA() returns a string wit a name in it and you mean to create an object based on that name, then you need to write a factory class that will create the object and return a pointer to it. Most likely, you will need polymorphism:
Expand|Select|Wrap|Line Numbers
  1. class Factory
  2. {
  3.      public:
  4.         Base* CreateObject(string& arg);
  5. };
  6. Base* Factory::CreateObject(string& arg)
  7. {
  8.      if (arg == "babu198649")
  9.      {
  10.  
  11.           return new Babu;
  12.      }
  13. }
  14.  
Another way to do this is to use a different kind of factory that does not require polymorphism. This is called a parameterized factory:
Expand|Select|Wrap|Line Numbers
  1. class Factory
  2. {
  3.      public:
  4.         Babu* CreateBabu();
  5. };
  6. Babu* Factory::CreateBabu()
  7. {
  8.  
  9.           return new Babu;
  10. }
  11.  
You use the parameterized factory by calling the correct create method:
Expand|Select|Wrap|Line Numbers
  1. Factory f;
  2. string arg = OBJA.FA();
  3. Babu* obj = 0;
  4. if (arg = "babu198649")
  5. {
  6.     obj = f.CreateBabu();
  7. }
  8.  
You can read up on the factory design pattern in a design patterns textbook.
Jan 30 '08 #5
RRick
463 Expert 256MB
This whole thing of getting the object to get the object name to create another object, seems to be a lot of work to just create a new object.

Since you have the object already, why not have that object make the copy, itself. This is a derivation of the Prototype pattern. Usually the prototype pattern makes a copy of the object including its data, but in this case, it sounds like you don't want to duplicate that object's data.

Here's what ClassA might look like. Clone is the method that creates a new ObjA.
Expand|Select|Wrap|Line Numbers
  1. ClassA
  2. {
  3.     .....
  4.     ObjA * createObjA( void) { return objA.clone(); }
  5. }
For more info try http://en.wikipedia.org/wiki/Prototype_pattern
Jan 30 '08 #6
thank u much for u r replys.

i wanted to create the objects of a class whose names should be the attribute name which are present in the xml file.

this is because as the attributes in the xml files gets increased the code should not be modified.

thanks for the examples provided.
Jan 31 '08 #7
RRick
463 Expert 256MB
There's also a design pattern called a composite that sounds like it will fit your needs.

It supports the concept of an attribute (that you described), it allows you to add new items without mods to the original code; and it also supports a hierarchical data structure you would find in an xml file.

For all of its capability, its pretty simple to write.
Jan 31 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Bijay Kumar | last post by:
Hi Guys, I was going through the source code of Object.cs in rotor. What I found is Equals() implemented as follows: public extern virtual bool Equals(Object obj); What I don't...
2
by: Ryan Mitchley | last post by:
Hi all I have code for an object factory, heavily based on an article by Jim Hyslop (although I've made minor modifications). The factory was working fine using g++, but since switching to the...
0
by: Bijay Kumar | last post by:
Hi Guys, I was going through the source code of Object class (Object.cs in rotor). What I found is Equals() implemented as follows: public extern virtual bool Equals(Object obj); What...
6
by: Scott Zabolotzky | last post by:
I'm trying to pass a custom object back and forth between forms. This custom object is pulled into the app using an external reference to an assembly DLL that was given to me by a co-worker. A...
5
by: Matthew | last post by:
I have a nice little Sub that saves data in a class "mySettings" to an XML file. I call it like so: Dim mySettings As mySettings = New mySettings mySettings.value1 = "someText" mySettings.value2...
16
by: anonymous.user0 | last post by:
The way I understand it, if I have an object Listener that has registered as a listener for some event Event that's produced by an object Emitter, as long as Emitter is still allocated Listener...
26
by: yb | last post by:
Hi, Is there a standard for the global 'window' object in browsers? For example, it supports methods such as setInterval and clearInterval, and several others. I know that w3c standardized...
6
by: tommaso.gastaldi | last post by:
Hi, does anybody know a speedy analog of IsNumeric() to check for strings/chars. I would like to check if an Object can be treated as a string before using a Cstr(), clearly avoiding the time...
11
by: Andrus | last post by:
I'm implementing entity object which should populate its properties from database when property is first referenced. In RDL reports I use object properties like MyObject.MyProperty MyObject...
21
by: phpCodeHead | last post by:
Code which should allow my constructor to accept arguments: <?php class Person { function __construct($name) { $this->name = $name; } function getName()
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...
1
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: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
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...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
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.