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_back(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 4 1342
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_back(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
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_back(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
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_back(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(name, 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(Node(name, value)) to go to that definition and not have
to rewrite an AddNode(Node) method?
--MathStuf
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_back(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(name, 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(Node(name, value)) to go to that definition and not have
to rewrite an AddNode(Node) method?
Make it AddNode(const Node&).
--
Erik Wikström This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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...
|
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...
|
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...
|
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...
| |
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...
|
by: daniell |
last post by:
/*
Triangle.cpp
*/
// Use a pure virtual function.
#include <iostream>
#include <cstring>
using namespace std;
|
by: sachingoel82 |
last post by:
Hi All,
I find following C++ behaviour regarding temporary objects quite
contradictory. Consider this:
class A
{
...
public:
|
by: coolguyaroundyou |
last post by:
Consider the following codes:
class abc
{
int i;
public:
abc() : i(0) {}
void func() { .....some code....}
};
|
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...
|
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...
| |
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...
|
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,...
|
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...
|
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...
|
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...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |