{ 类钻石 { 公开: 钻石(); ~Diamond(); 私人: //如果这些不是私人的,那么他们需要特殊的 //处理以制作下面指针的副本。 Diamond(const Diamond& ); //未实施 Diamond& operator =(const Diamond&); //未实现。 struct Impl; Impl * impl_; }; } #endif //!DIAMONDDIAMOND_H // diamond.cpp #include" diamond。 h" #include" north.h" #include" south.h" #include" east.h" #include" west.h" 名称空间钻石 { struct Diamond: :Impl { //你想要的任何构造者/额外成员。 East e; 北n; 西w; 南s; }; Diamond :: Diamond():impl_(new Impl){} Diamond :: ~Diamond(){delete impl_; } } 我个人使用boost :: scoped_ptr来保存impl_指针所以我 没有担心删除它。另请注意,如果对象可以复制,则需要额外的 护理。 如果你想使用scoped_ptr,你必须记住你的 构造函数和析构函数*在Impl类定义之后。否则 编译器会抱怨类型不完整。 另请参阅: http://www.gotw.ca/gotw/024.htm http://www.gotw.ca/gotw/028.htm http://c2.com/cgi/wiki?PimplIdiom ......还有许多谷歌提供的其他产品。本周档案的大师是 充满了这种东西( www.gotw.ca/gotw) 。 - Pete [snip] That''s because you stored them by value. The compiler needs to determinethe size of your class.. how can it do this for diamond without the fulldata about East, West, etc. You can only get away with forward declarations in the followingcircumstances (might have missed a few): 1) Your class/header never *contains* the forward declared class by value. 2) Your class/header never has a method/function that takes the forwarddeclared class by value (reference or pointer is fine, as is returningit by value). 3) Your header never uses the forward declared class for scoperesolution (e.g. East::whatever). 4) You do not inherit from the forward declared class. To get around this you can use the compiler firewall idiom: // diamond.h#ifndef DIAMONDDIAMOND_H#define DIAMONDDIAMOND_H namespace diamond{class Diamond{public:Diamond();~Diamond(); private:// If these aren''t private then they need special// handling to make a copy of the pointer below.Diamond( const Diamond& ); // Not implementedDiamond& operator=( const Diamond& ); // Not implemented. struct Impl;Impl* impl_;};} #endif // !DIAMONDDIAMOND_H // diamond.cpp#include "diamond.h"#include "north.h"#include "south.h"#include "east.h"#include "west.h" namespace diamond{struct Diamond::Impl{// Any constructor/extra members you want here. East e;North n;West w;South s;}; Diamond::Diamond() : impl_( new Impl ) {}Diamond::~Diamond() { delete impl_; }} Personally I use a boost::scoped_ptr to hold the impl_ pointer so Idon''t have to worry about deleting it. Note also that you need extracare if the object will be copyable. If you do want to use scoped_ptr, you must remember to have yourconstructor and destructor *after* the Impl class definition. Otherwisethe compiler complains about incomplete types. See also: http://www.gotw.ca/gotw/024.htm http://www.gotw.ca/gotw/028.htm http://c2.com/cgi/wiki?PimplIdiom ...and many others available from google. The guru of the week archive isfilled with this kind of stuff (www.gotw.ca/gotw). -- Pete 这篇关于#include verse class class_name的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-10 20:42