as this example,
question:
If my purpose is initialize data from xml files and store them in the
vector, so they can be used in class B by other member functions, do
you think functionP is a viable function(will a could go away after
out of the function)?
If not, is there a better solution than using functionPt?
I read that it is not a good design to have container for pointers(C++
FAQs), but I cannot see how I can get around it in my situation.
Newbie questions, thanks for the help
Chris
class A{
....
}
class B{
void functionPt(){
...
A* aPt ;
vPt.push_back(aPt);
}
void functionP(){
...
A a;
v.push_back(a);
}
private:
vector<A*vPt;
vector<A v;
} 7 1402
On Aug 3, 4:38 pm, trade...@yahoo.com wrote:
If my purpose is initialize data from xml files and store them in the
vector, so they can be used in class B by other member functions, do
you think functionP is a viable function(will a could go away after
out of the function)?
void functionP(){
...
A a;
v.push_back(a);
}
That would work. Yes, 'a' would be terminated at the end of functionP.
Since 'v' would be containing a *copy* of 'a' you are good.
If not, is there a better solution than using functionPt?
void functionPt(){
...
A* aPt ;
vPt.push_back(aPt);
}
You would need a vector of pointers if the objects that you create a
polymorphic. Otherwise go with functionP.
I read that it is not a good design to have container for pointers(C++
FAQs), but I cannot see how I can get around it in my situation.
If you have polymorphic objects, store them either as smart pointers
in a vector, or use a "pointer vector" that knowns to delete the
objects when itself is going away.
Using the Boost library's reference counted pointer:
#include <boost/shared_ptr.hpp>
typedef boost::shared_ptr<AAPtr;
typedef vector<APtrAPointers;
APointers my_a_collection;
my_a_collection.push_back(APtr(new SomeDecendentOfA()));
See the Boost library for their pointer vector.
Ali tr******@yahoo.com wrote:
as this example,
question:
If my purpose is initialize data from xml files and store them in the
vector, so they can be used in class B by other member functions, do
you think functionP is a viable function(will a could go away after
out of the function)?
If not, is there a better solution than using functionPt?
I read that it is not a good design to have container for pointers(C++
FAQs), but I cannot see how I can get around it in my situation.
Newbie questions, thanks for the help
Chris
class A{
....
}
class B{
void functionPt(){
...
A* aPt ;
aPt has not been initialized. Copying it into a vector is technically
undefined behavior.
vPt.push_back(aPt);
}
void functionP(){
...
A a;
v.push_back(a);
}
private:
vector<A*vPt;
vector<A v;
}
On Aug 3, 7:50 pm, "Daniel T." <danie...@earthlink.netwrote:
trade...@yahoo.com wrote:
as this example,
question:
If my purpose is initialize data from xml files and store them in the
vector, so they can be used in class B by other member functions, do
you think functionP is a viable function(will a could go away after
out of the function)?
If not, is there a better solution than using functionPt?
I read that it is not a good design to have container for pointers(C++
FAQs), but I cannot see how I can get around it in my situation.
Newbie questions, thanks for the help
Chris
class A{
....
}
class B{
void functionPt(){
...
A* aPt ;
aPt has not been initialized. Copying it into a vector is technically
undefined behavior.
vPt.push_back(aPt);
}
void functionP(){
...
A a;
v.push_back(a);
}
private:
vector<A*vPt;
vector<A v;
}
Thanks. My fault. My intention is A* aPt = new A;
On Aug 3, 7:07 pm, acehr...@gmail.com wrote:
On Aug 3, 4:38 pm, trade...@yahoo.com wrote:
If my purpose is initialize data from xml files and store them in the
vector, so they can be used in class B by other member functions, do
you think functionP is a viable function(will a could go away after
out of the function)?
void functionP(){
...
A a;
v.push_back(a);
}
That would work. Yes, 'a' would be terminated at the end of functionP.
Since 'v' would be containing a *copy* of 'a' you are good.
If not, is there a better solution than using functionPt?
void functionPt(){
...
A* aPt ;
vPt.push_back(aPt);
}
You would need a vector of pointers if the objects that you create a
polymorphic. Otherwise go with functionP.
I read that it is not a good design to have container for pointers(C++
FAQs), but I cannot see how I can get around it in my situation.
If you have polymorphic objects, store them either as smart pointers
in a vector, or use a "pointer vector" that knowns to delete the
objects when itself is going away.
Using the Boost library's reference counted pointer:
#include <boost/shared_ptr.hpp>
typedef boost::shared_ptr<AAPtr;
typedef vector<APtrAPointers;
APointers my_a_collection;
my_a_collection.push_back(APtr(new SomeDecendentOfA()));
See the Boost library for their pointer vector.
Ali
Thanks Ali. Basically, when push_back is called, A's copy constructor
is triggered and there is a copy of a is made and stored in v. And
since v's scope is the class, the copy will not be destroyed as long
as the class instance is alive. please correct me if i am wrong.
Heard about boost, but never used it before. Will auto_ptr do the same
thing in this case (in case A could be polymorphic )?
thanks
Chris
On Aug 3, 6:30*pm, trade...@yahoo.com wrote:
Will auto_ptr do the same
thing in this case (in case A could be polymorphic )?
Unfortunately, std::auto_ptr can not be used with standard containers
because it doesn't satisfy the requirement that the copies are
equivalent. Source auto_ptr becomes "null" when auto_ptr objects are
copied.
Ali
On Aug 3, 9:20*pm, trade...@yahoo.com wrote:
On Aug 3, 7:50 pm, "Daniel T." <danie...@earthlink.netwrote:
trade...@yahoo.com wrote:
as this example,
question:
If my purpose is initialize data from xml files and store them in the
vector, so they can be used in class B by other member functions, do
you think functionP is a viable function(will a could go away after
out of the function)?
If not, is there a better solution than using functionPt?
I read that it is not a good design to have container for pointers(C++
FAQs), but I cannot see how I can get around it in my situation.
Newbie questions, thanks for the help
Chris
class A{
* *....
}
class B{
void functionPt(){
*...
* A* aPt ;
aPt has not been initialized. Copying it into a vector is technically
undefined behavior.
* vPt.push_back(aPt);
}
void functionP(){
* ...
* A a;
* *v.push_back(a);
}
private:
* *vector<A*vPt;
* *vector<A*v;
}
Thanks. My fault. My intention is A* aPt = new A;
Now you have a leak.
On Aug 4, 1:48*pm, "Daniel T." <danie...@earthlink.netwrote:
On Aug 3, 9:20*pm, trade...@yahoo.com wrote:
On Aug 3, 7:50 pm, "Daniel T." <danie...@earthlink.netwrote:
trade...@yahoo.com wrote:
as this example,
question:
If my purpose is initialize data from xml files and store them in the
vector, so they can be used in class B by other member functions, do
you think functionP is a viable function(will a could go away after
out of the function)?
If not, is there a better solution than using functionPt?
I read that it is not a good design to have container for pointers(C++
FAQs), but I cannot see how I can get around it in my situation.
Newbie questions, thanks for the help
Chris
class A{
* *....
}
class B{
void functionPt(){
*...
* A* aPt ;
aPt has not been initialized. Copying it into a vector is technically
undefined behavior.
* vPt.push_back(aPt);
}
void functionP(){
* ...
* A a;
* *v.push_back(a);
}
private:
* *vector<A*vPt;
* *vector<A*v;
}
Thanks. My fault. My intention is A* aPt = new A;
Now you have a leak.
not really. my ~B will iterate the vector and call delete. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Roland Bengtsson |
last post by:
I have a class Conception and I have this in a vector, it should be:
vector<Conception> vek; // vector
vector<Conception>::iterator vek; // iterator to vek
But what if I want to have pointers...
|
by: matthurne |
last post by:
I am working through exercise 8-2 in Accelerated C++...I am
implementing the function equal(b, e, b2) where b is an iterator for
the first element in a container, e is an iterator pointing to one...
|
by: joevandyk |
last post by:
Below, I have a class Container that contains a vector. The vector
contains pointers to a Base class. When the Container destructor is
called, I would like to delete all the objects that the...
|
by: Aaron Walker |
last post by:
Greetings,
I'm attempting to write my first *real* template function that also deals with
a map of strings to member function pointers that is making the syntax a little
tricky to get right.
...
|
by: edward.birch |
last post by:
Can anyone see anything wrong with the following code?
(CONTAINER can be list, vector, set, ...)
template <class T> void Destroy(T * p) { delete p; }
void...
|
by: Paminu |
last post by:
If I have this struct:
typedef struct test{
int x;
int y;
}container;
Now I would like to make an array of 5 pointers to this struct:
int main(void){
|
by: Timothee Groleau |
last post by:
Hi all,
My name is Tim, I'm just getting started with C++ and this is my first post
to the group.
Is there a standard recommended approach to use a vector of pointers along
with <algorithm>?...
|
by: Jess |
last post by:
Hello,
I tried several books to find out the details of object
initialization. Unfortunately, I'm still confused by two specific
concepts, namely default-initialization and...
|
by: Boris |
last post by:
I had a 3 hours meeting today with some fellow programmers that are partly
not convinced about using smart pointers in C++. Their main concern is a
possible performance impact. I've been explaining...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
|
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,...
|
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: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
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,...
| |