473,396 Members | 1,933 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
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...
4
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...
7
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...
4
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. ...
5
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...
3
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){
9
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>?...
4
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...
54
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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
1
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...
0
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...
0
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,...
0
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,...
0
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...
0
tracyyun
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...
0
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,...

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.