473,657 Members | 2,376 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Saving temporary objects

I am building a class which is like a tree of sorts. Each node can
hold numerous subnodes of itself. I have some constructors such as:

Node(string name, int value);
Node(string name, string value);

I would also like the following AddNode methods:

AddNode(Node &node);
AddNode(string name, int value);
AddNode(string name, string value);

The AddNode where another node is passed works fine. However, the ones
where new values are passed doesn't when set up as such:

AddNode(string name, int value)
{
Node add(name, value);
elements.push_b ack(add);
return &elements[elements.size() - 1];
}

I know it's because the instance of the new Node is temporary, but is
there a way to keep it around for future use?

--MathStuf

Apr 10 '07 #1
4 1347
On Apr 10, 9:07 am, "MathStuf" <MathS...@gmail .comwrote:
I am building a class which is like a tree of sorts. Each node can
hold numerous subnodes of itself. I have some constructors such as:

Node(string name, int value);
Node(string name, string value);

I would also like the following AddNode methods:

AddNode(Node &node);
AddNode(string name, int value);
AddNode(string name, string value);

The AddNode where another node is passed works fine. However, the ones
where new values are passed doesn't when set up as such:

AddNode(string name, int value)
{
Node add(name, value);
elements.push_b ack(add);
return &elements[elements.size() - 1];

}

I know it's because the instance of the new Node is temporary, but is
there a way to keep it around for future use?

--MathStuf
Hi,

Could you please elaborate the error that you get and the code snippet
for the method that works for you. If my guess is correct, "elements"
seems to be a vector of "Nodes". If that is the case, push backs on
the vector will invalidate the address that you are returning when the
vector gets resized.
Regards
SJ

Apr 10 '07 #2
On 10 Apr, 06:07, "MathStuf" <MathS...@gmail .comwrote:
I am building a class which is like a tree of sorts. Each node can
hold numerous subnodes of itself. I have some constructors such as:

Node(string name, int value);
Node(string name, string value);

I would also like the following AddNode methods:

AddNode(Node &node);
AddNode(string name, int value);
AddNode(string name, string value);

The AddNode where another node is passed works fine. However, the ones
where new values are passed doesn't when set up as such:

AddNode(string name, int value)
{
Node add(name, value);
elements.push_b ack(add);
return &elements[elements.size() - 1];

}

I know it's because the instance of the new Node is temporary, but is
there a way to keep it around for future use?
As SJ pointed out, if elements is a container then it will contain a
copy of the object you push back, not the object itself, so the fact
that it's temporary is irrelevant. And instead of using
elements[elements.size() - 1] you can use elements.back() .

--
Erik Wikström

Apr 10 '07 #3
On Apr 10, 2:47 am, "Erik Wikström" <eri...@student .chalmers.se>
wrote:
On 10 Apr, 06:07, "MathStuf" <MathS...@gmail .comwrote:


I am building a class which is like a tree of sorts. Each node can
hold numerous subnodes of itself. I have some constructors such as:
Node(string name, int value);
Node(string name, string value);
I would also like the following AddNode methods:
AddNode(Node &node);
AddNode(string name, int value);
AddNode(string name, string value);
The AddNode where another node is passed works fine. However, the ones
where new values are passed doesn't when set up as such:
AddNode(string name, int value)
{
Node add(name, value);
elements.push_b ack(add);
return &elements[elements.size() - 1];
}
I know it's because the instance of the new Node is temporary, but is
there a way to keep it around for future use?

As SJ pointed out, if elements is a container then it will contain a
copy of the object you push back, not the object itself, so the fact
that it's temporary is irrelevant. And instead of using
elements[elements.size() - 1] you can use elements.back() .

--
Erik Wikström- Hide quoted text -

- Show quoted text -
Thanks for the input. The returned pointer is more for use for
continual nesting so that its easy to add continually nested elements.
I've decided to cut it down to just AddNode(Node&) because as far as
I'm aware of, if you call AddNode(Node(na me, value)), it doesn't
matter because the compiler will optimize it so that its constructed
right in the method. Also, is there a way to force
node.AddNode(No de(name, value)) to go to that definition and not have
to rewrite an AddNode(Node) method?

--MathStuf

Apr 10 '07 #4
On 10 Apr, 14:29, "MathStuf" <MathS...@gmail .comwrote:
On Apr 10, 2:47 am, "Erik Wikström" <eri...@student .chalmers.se>
wrote:
On 10 Apr, 06:07, "MathStuf" <MathS...@gmail .comwrote:
I am building a class which is like a tree of sorts. Each node can
hold numerous subnodes of itself. I have some constructors such as:
Node(string name, int value);
Node(string name, string value);
I would also like the following AddNode methods:
AddNode(Node &node);
AddNode(string name, int value);
AddNode(string name, string value);
The AddNode where another node is passed works fine. However, the ones
where new values are passed doesn't when set up as such:
AddNode(string name, int value)
{
Node add(name, value);
elements.push_b ack(add);
return &elements[elements.size() - 1];
}
I know it's because the instance of the new Node is temporary, but is
there a way to keep it around for future use?
As SJ pointed out, if elements is a container then it will contain a
copy of the object you push back, not the object itself, so the fact
that it's temporary is irrelevant. And instead of using
elements[elements.size() - 1] you can use elements.back() .
--
Erik Wikström- Hide quoted text -
- Show quoted text -

Thanks for the input. The returned pointer is more for use for
continual nesting so that its easy to add continually nested elements.
I've decided to cut it down to just AddNode(Node&) because as far as
I'm aware of, if you call AddNode(Node(na me, value)), it doesn't
matter because the compiler will optimize it so that its constructed
right in the method. Also, is there a way to force
node.AddNode(No de(name, value)) to go to that definition and not have
to rewrite an AddNode(Node) method?
Make it AddNode(const Node&).

--
Erik Wikström

Apr 10 '07 #5

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

Similar topics

4
5447
by: Jacob H | last post by:
Hello list... I'm developing an adventure game in Python (which of course is lots of fun). One of the features is the ability to save games and restore the saves later. I'm using the pickle module to implement this. Capturing current program state and neatly replacing it later is proving to be trickier than I first imagined, so I'm here to ask for a little direction from wiser minds than mine! When my program initializes, each game...
5
1690
by: White Wolf | last post by:
Hi, I would like to double check how long a temporary returned by a function lives? Suppose I have an instance of a class type C, which has a member function returning some sort of wrapper-decorator by value - thereby creating an unnamed temporary. Assuming I start using the temporary (by calling its members) in the same expression, can I assume that it will live long enough to serve those calls?
17
10265
by: Henning Hasemann | last post by:
I have a function which gets the adress of an object as argument. It does some comparsion with the object's contents and then returns. no Reference or pointer to the object is stored or will be used after the function has returned. Say the function whould be named f and the objects class whould be T it'll look like this: bool f(T* thing) { return thing->foobar == 5; // Just a stupid example
1
2772
by: phil | last post by:
I have been developing some queries which will list the dependencies of Stored Procedures (this is with DB2/UDB v8). In doing so, I have noticed that dependencies are ignored if they occur in an insert into a temporary table. For instance, if you have the following procedure: CREATE PROCEDURE DBA.TEST BEGIN INSERT INTO SESSION.TEMP1 SELECT COUNT(*) FROM DBA.DEPARTMENT; END Then DBA.DEPARTMENT will not appear in the list of dependent...
2
2390
by: perdubug | last post by:
Somebody told me that Tasking C166 C++ compiler has problems with temporary objects in function call parameters. He gave me below examples: Case 1:Wrong: rc = foo(&bar()); Case 2:Right: bar b; rc=foo(&b);
3
1638
by: shuisheng | last post by:
Dear All, Assume I have two classes: material and shape, as follows class Material { double density; // material attribute, may have more vector<Shape*pShape; // shape objects assocaited the material. };
3
12883
by: daniell | last post by:
/* Triangle.cpp */ // Use a pure virtual function. #include <iostream> #include <cstring> using namespace std;
1
1487
by: sachingoel82 | last post by:
Hi All, I find following C++ behaviour regarding temporary objects quite contradictory. Consider this: class A { ... public:
5
2642
by: coolguyaroundyou | last post by:
Consider the following codes: class abc { int i; public: abc() : i(0) {} void func() { .....some code....} };
0
8399
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8312
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8827
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8732
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8504
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7337
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6169
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4159
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...

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.