-
February 11th, 2011, 06:11 PM
#1
Best way to store an array of pointers
Hi
I have usually created stuff in Visual Studio 6.0 but have now moved on to Visual Studio 2010. The compiler are more strict and I have trouble using "old hacks" and need to learn something new..
I have three classes; A an MFC based class and B & C which is Non-MFC.
In A I have a CPtrArray list over pointers of class B, this works as in VS6.0
In B I tried CPtrArray for class C, but since it is not MFC I get an error saying:
error C2248: 'CObject::CObject' : cannot access private member declared in class 'CObject'
(...)
This diagnostic occurred in the compiler generated function 'CPtrArray::CPtrArray(const CPtrArray &)'
So I tried vector<ClassC *> m_myclassC;
But this one is **** slooow and I figured it out since it called the copy-function ClassB (const ClassB & v) then the destructor. And since each class uses some GDI+ stuff it is doing its work several times.
Isn't the vector<void *> be equal to CPtrArray? If so, why does it call the copy & destructor all the time. Any advice would be great!
-
February 11th, 2011, 07:08 PM
#2
Re: Best way to store an array of pointers
What operation are you trying to do that causes the compiler
error ? I suspect it involves a copy of A or B, which contain
a CPtrArray ... which are not copyable.
You can hold pointers to CPtrArray instead.
or define operator = and copy constructor for A and B
-
February 11th, 2011, 07:09 PM
#3
Re: Best way to store an array of pointers
Originally Posted by Large
In B I tried CPtrArray for class C, but since it is not MFC I get an error saying:
error C2248: 'CObject::CObject' : cannot access private member declared in class 'CObject'
(...)
That is because you're trying to make copies of the object somewhere.
So I tried vector<ClassC *> m_myclassC;
But this one is **** slooow and I figured it out since it called the copy-function ClassB
How can it do copies when the vector contains pointers? I think you need to actually post your code, because what you state is impossible.
When you create a vector of pointers, the only thing that is copied is a pointer value, and that is either 4 or 8 bytes. That will not automatically call copy constructors of objects.
If instead, you had a vector of objects (not pointers), then of course, the vector will need to move things around when you add or remove items. That isn't the fault of vector -- that is how C++ works. To copy an object from one place to another will invoke the copy constructor and/or assignment op.
Isn't the vector<void *> be equal to CPtrArray?
I don't know if a vector< void * > is exactly the equivalent of a CPtrArray, but if CPtrArray is based on void pointers, then I would suggest not using CPtrArray and use type-safe containers. A vector<T*>, or even a CArray<T*>, where T is the type you want to store, is a much better choice than CPtrArray.
If so, why does it call the copy & destructor all the time. Any advice would be great!
A vector of pointers does not do this.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; February 11th, 2011 at 07:12 PM.
-
February 13th, 2011, 02:20 PM
#4
Re: Best way to store an array of pointers
Paul & Philip you are both right...
I got time to test it out of the project.
The main program (which is too complex atm) does have a vector of objects several places.
Replacing them to vector<whatever *>-fixed it, but a massive reprogramming had to be done.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|