473,699 Members | 2,701 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(a Pt);

}

void functionP(){
...
A a;
v.push_back(a);
}

private:
vector<A*vPt;
vector<A v;

}

Aug 3 '08 #1
7 1425
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(a Pt);

}
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_p tr<AAPtr;
typedef vector<APtrAPoi nters;

APointers my_a_collection ;
my_a_collection .push_back(APtr (new SomeDecendentOf A()));

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(a Pt);

}

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...@earth link.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(a Pt);
}
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(a Pt);
}

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_p tr<AAPtr;
typedef vector<APtrAPoi nters;

APointers my_a_collection ;
my_a_collection .push_back(APtr (new SomeDecendentOf A()));

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...@earth link.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(a Pt);
}
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...@earth link.netwrote:
On Aug 3, 9:20*pm, trade...@yahoo. com wrote:
On Aug 3, 7:50 pm, "Daniel T." <danie...@earth link.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(a Pt);
}
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
3525
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 to class Conception instead? How can I do that? And how should I write to declare an iterator to this vector?
4
1884
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 past the last element in that same container, and b2 is an iterator for the first element in the second container. Here's what I have: template <class T> bool equal(T begin, T end, T begin2) { while (begin != end) { if (*begin != *begin2) {
7
3110
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 pointers in the vector are pointing to. It works fine, IF I comment out the destructors in Base and Inherited. If I leave the explicit destructors in there, it seg faults. Why is there a difference?
4
2060
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. The function in question: 36: template <typename Container, 37: typename OutputIterator,
5
2762
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 CleanUp(std::CONTAINER<ContainerType *> & Container) { std::foreach(Container.begin(), Container.end(), Destroy<ContainerType>);
3
3930
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
1672
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>? To be specific, here is my situation, I have 2 classes A and B. Class A contains a private vector of pointers to B objects and I need to sort this
4
3710
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 value-initialization. I think default-init calls default constructor for class objects and sets garbage values to PODs. Value-init also calls default constructor for class objects and sets 0s to POD types. This is what I've learned from the books (especially...
54
11977
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 the advantages of smart pointers endlessly (which are currently used in all our C++ software; we use the Boost smart pointers) as I'm seriously concerned that there is a shift to raw pointers. We are not developing system software but rather...
0
8686
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
8615
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
9173
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
9033
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
8911
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
8882
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7748
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...
0
5872
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();...
3
2009
bsmnconsultancy
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.