469,160 Members | 1,679 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,160 developers. It's quick & easy.

questions about pointers in container

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;

}

Aug 3 '08 #1
7 1243
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
Aug 4 '08 #2
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;

}
Aug 4 '08 #3
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;
Aug 4 '08 #4
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
Aug 4 '08 #5
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
Aug 4 '08 #6
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.
Aug 4 '08 #7
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.
Aug 6 '08 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

14 posts views Thread by Roland Bengtsson | last post: by
4 posts views Thread by matthurne | last post: by
7 posts views Thread by joevandyk | last post: by
5 posts views Thread by edward.birch | last post: by
3 posts views Thread by Paminu | last post: by
9 posts views Thread by Timothee Groleau | last post: by
54 posts views Thread by Boris | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.