Using ScopedPointer in map


#1

Hi,
I am using
typedef map<unsigned int , ScopedPointer<CommonParam> > PARAM_MAP;
when i use this PARAM_MAP.
error is saying “ScopedPointer is private”,
In case of Mac there is another copy constructor for ScopedPointer which is private and it tries to access that.

and definition is also not found for this another copyconstructor. ScopedPointer (const ScopedPointer&);
CommonParam is my own class.
How to use it???


#2

Don’t use it.

A ScopedPointer, by definition, cannot possibly provide a copy constructor, so can’t be used in situations where you need a class that has copy-by-value semantics. You could use a ReferenceCountedObjectPtr, or a shared_ptr, etc. instead.

(And BTW in C++ it’s good practice to use all-caps names for macros, but not classes)


#3

Let me add that the last place I worked had a semi-ban on reference counted pointers and shared pointers, because they had been implicated in both performance issues and other bugs.

The issue in both cases is that you’ve sort of lost track of your objects - you don’t know when the destructors are really going to be called, which means they might be called unexpectedly - perhaps in the wrong thread. Of course, there are plenty of cases where this isn’t an issue, and some cases where there isn’t any real alternative but I still always consider it a slight red flag when I have to use a shared or ref counted pointer - and so far this big project I’m on has managed to have zero of them.

I’d also agree with Jules’ comment on C++ naming conventions, which have become pretty standardized these days - ClassesLookLikeThis, functionsAreLikeThis(), THIS_IS_A_MACRO or A_STATIC_CONST_VARIABLE.