Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Lifetime of References

Status
Not open for further replies.

Nosferatu

Programmer
Jun 9, 2000
412
RO
I am using a C++ library that extensively uses references when returning objects, instead of pointers.

My question is, how can I keep those references from being destroyed? For example, in a method of an object, I am trying to build a hash table with a plethora of nodes, obtained from a class by references (actually copy-constructed objects). After I get out of that method, all the stored references are... gone... HOW CAN I KEEP THEM, without using over and over and over again the copy constructor, which really takes some time, when talking about thousands of objects...

Thanks... [red]Nosferatu[/red]
We are what we eat...
There's no such thing as free meal...
once stated: methane@personal.ro
 
Can you clarify, maybe with a little code or pseudo-code? naturaly when variables go out of scope they are deleted... do you want to keep them in scope?? is this your own method? why not change them to pointers when you call the copy-constructor the first time? MYenigmaSELF:-9
myenigmaself@yahoo.com
 
Well, here's the situation:

Inside the library I use (and which I'm not supposed to modify it)
Code:
class LibraryClass {
 SomeMember mem;
...
  SomeMember& getMember(){
    return SomeMember(mem); // copy constructor called.
};

Inside my class, I have:

Code:
class MyClass {
 LibraryClass lc; 
 HashTable ht;
};

MyClass::someMethod()
{  ht.addHashElement(lc.getMember());
// or
   LibraryClass& getMem = lc.getMember;
}

In either case, since the LibraryClass::getMember method returns a reference, it will be deleted when exiting my method, since it is a new created object inside the library method. I know this is the way things work, but... I have to have those objects inside the hash table. So far, I used another copy of the object returned to store it into the hash table, a pointer, like you (MYenigmaSELF) said.

The point is that I HATE TO DO THE SAME THING OVER AND OVER AGAIN, or, actually, to let the program construct objects like crazy. This is slowing down the program :~/ ...

So, is there a way to get around the deletion of the object
returned, without using, e.g., global variables? [red]Nosferatu[/red]
We are what we eat...
There's no such thing as free meal...
once stated: methane@personal.ro
 
Well from your code it looks like LibraryClass lc contains more LibraryClasses... that doesn't seem right? But in any respect your LibraryClass::getMember method returns a reference, but in essence a reference is just a variable that's not truly in scope. Or that's been forced into scope, depending on how you look at things. It's been a long time since I've done stuff like this but can't you just make a pointer to the object? Sometimes I get a bit confused because I work extensively in java but so long as there is a pointer pointing to an object it won't go out of scope and be garbage collected, correct? So make a pointer to the reference. I hope I'm not totaly off base on this. If you need me to clarify anything just say so. Good Luck. MYenigmaSELF:-9
myenigmaself@yahoo.com
 
Well, what exaclty does the library class do? Do you really need to make a hash table of "library classes" or just a hash table of what type "SomeMember" is? What is the design for your hash table? What will remain constant in the library class from one to the next (if anything). By setting "SomeMember" does the class adjust itself to reflect the changes (i.e. by changing other data members in the class).

HERE IS ANOTHER IDEA BUT NOT A GOOD ONE (unless LibraryClass was designed properly):

Just a thought, if you are not supposed to modify the library class, check and see if the data is protected or private... if it is protected you could use inheritance (though it does not call for inheritance with YOUR class). By making a new library class, you could inherit this library class and make any changes you need. PLEASE KEEP IN MIND THAT THIS MOST LIKELY IS NOT A SITUATION TO USE INHERITANCE. If however the library class does have virtual functions as well then inheritance may have been an option to the designer of the class.


Matt
 
The post above was just an "on-the-fly" sample. The library I am using is Xerces C++ DOM which returns all kinds of copy-constructed objects which go out of scope as soon as I exit the function the library methods were called.
I guess I have to stick with the solution I chosen so far that is, creating again new objects, this time as pointers, which I will delete manually at the appropriate time.
Thanks for your posts! [red]Nosferatu[/red]
We are what we eat...
There's no such thing as free meal...
once stated: methane@personal.ro
 
hi Nosferatu,
Could u tell me how to get the text value of a particular node.I am using xerces parsing.
Should it be
xmldomdoc.getelementbytagname("para").item(0).text
Could you help on this...Pls
Thanks
Khushi.
 
Just use getNodeValue on the node found. [red]Nosferatu[/red]
We are what we eat...
There's no such thing as free meal...
once stated: methane@personal.ro
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top