[smart_ptr]why scoped_ptr c ann't used in this case, yet shared_ptr ca n

//cpeer.h
#ifndef CPEER_H_
#define CPEER_H_
#include <string>
struct CPeer
{
std::string name;
};
#endif /*CPEER_H_*/
//cpeermanager.h
#ifndef CPEERMANAGER_H_
#define CPEERMANAGER_H_
class CPeer;
#include

On Fri, 13 Jul 2007 13:52:18 +0800 (CST), hongleij@126.com wrote: <...>
class CPeer;
#include
#include class CPeerManager { public: CPeerManager(); boost::scoped_ptr<CPeer> cp_; //error // boost::shared_ptr<CPeer> cp_; // ok };
<...> The scoped_ptr is defined to be simple and to load as little as possible the code using it. To uses all potimizations possible, it's been designed in such a way that the struct pointed byt it must be completely defined when the pointer is declared, so you should replace the forward declaration "class C>Peer;" with the full declaration (typically, ibcluding its corresponding header regrads, Zara

Zara wrote:
The scoped_ptr is defined to be simple and to load as little as possible the code using it. To uses all potimizations possible, it's been designed in such a way that the struct pointed byt it must be completely defined when the pointer is declared, so you should replace
No, the type needs to be complete at destruction time, not at declaration time of the pointer.
the forward declaration "class C>Peer;" with the full declaration (typically, ibcluding its corresponding header
Markus

//cpeer.h
#ifndef CPEER_H_ #define CPEER_H_
#include <string> struct CPeer { std::string name; }; #endif /*CPEER_H_*/
//cpeermanager.h
#ifndef CPEERMANAGER_H_ #define CPEERMANAGER_H_
class CPeer;
#include
#include class CPeerManager { public: CPeerManager();
+ ~CPeerManager();
boost::scoped_ptr<CPeer> cp_; //error // boost::shared_ptr<CPeer> cp_; // ok };
#endif /*CPEERMANAGER_H_*/
//cpeermanager.cpp
#include "cpeer.h" #include "cpeermanager.h"
CPeerManager::CPeerManager(): cp_ (new CPeer() ) {
}
+ // define a destructor at the point where CPeer is complete + CPeerManager::~CPeerManager() + {}
//main.cpp
#include "cpeermanager.h"
[snip]

hongleij@126.com wrote: [...]
/// why boost::shared_ptr can used to Minimize compilation dependencies between files, yet boost::scoped_ptr cannot. I needn't to use reference cout.may someboby help me out of the trouble.thanks!
scoped_ptr can be used like you want. From the smart_ptr documentation: 'Note that scoped_ptr requires that T be a complete type at destruction time, but shared_ptr does not.' IOW, you need to define a possible empty destructor of your class in a source file, and only there the complete type of T must be available. See the example referenced in http://boost.org/libs/smart_ptr/scoped_ptr.htm, section 'Handle/Body Idiom'. HTH, Markus
participants (4)
-
hongleij@126.com
-
Ilya Sokolov
-
Markus Schöpflin
-
Zara