本文介绍了复制建设的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我在关于C ++中的复制构造的章节中代码中的(见下文),作者说如果你按值传递对象 ,如HowMany h2 = f(h); ....然后创建一个按位对象w / o 调用类的构造函数。然而他说当我们离开 函数范围HowMany f(HowMany x)然后调用析构函数。 为什么这个不一致?根据我的意见,即使是析构函数也应该*不* 。我可以理解,有点明智的复制意味着一个''C''就像只需将参数推入堆栈的方法一样。当fn''f''是结束时,它应该只是意味着堆栈(由 值生成的本地对象)弹出而不调用它detructor。 i希望我不要问一个愚蠢的qn。但我很困惑这个根本。 ----------------- *继承人代码* --------- --------------------- class HowMany { static int objectCount; public: HowMany(){objectCount ++; } static void print(const string& msg =""){ if(msg.size()!= 0)out<< msg<< ":"; out<< " objectCount =" << objectCount<<结束; } ~HowMany(){ objectCount--; print(&〜; HowMany ()"); } }; int HowMany :: objectCount = 0; //传递和返回BY VALUE: HowMany f(HowMany x){ x.print(&x; f()中的参数); 返回x; } int main(){ HowMany h; HowMany :: print(构造h之后); HowMany h2 = f(h); HowMany :: print(" ;在调用f()之后; } ///:〜 解决方案 原因是,因为你没有用户定义的副本 类的构造函数,编译器为你生成了一个( 当然你看不到任何被调用的迹象)。当h通过 值时,会调用自动增强的复制构造函数,它在 中的行为只是执行按位复制,虽然它看不到 被调用。所以实际上对称性仍然存在,副本 构造函数对你来说是不可见的。 HowMany :: HowMany(void):objectCount = 1 构建h后的:objectCount = 1 HowMany :: HowMany(const HowMany&): objectCount = 2 x里面的参数f(HowMany):objectCount = 2 HowMany :: HowMany(const HowMany&):objectCount = 3 HowMany ::〜HowMany(void):objectCount = 2 调用f()后:objectCount = 2 HowMany ::〜HowMany(void):objectCount = 1 HowMany ::〜HowMany(void):objectCount = 0 不,我认为你有一个愚蠢的作者试图让简单的 变得复杂。 /> john i am on the chapter on copy construction in C++in the code (see below), the author says if u pass the object by valueas in HowMany h2 = f(h); ....then a bitwise object is created w/ocalling the constructor of the class. However he says when we leavescope of function HowMany f(HowMany x) then the destructor is called.why this inconsistency?. Accdng to me even the destructor should *not*be called. i can understand that bit wise copy means a ''C'' like methodof simply pushing the arguments into stack. accdng to me when fn ''f'' isover, it should simply mean that stack (local object that was made byvalue)is popped out without calling its detructor. i hope im not asking a stupid qn. but im confused abt this fundamental. -----------------*heres the code*------------------------------ class HowMany {static int objectCount;public:HowMany() { objectCount++; }static void print(const string& msg = "") {if(msg.size() != 0) out << msg << ": ";out << "objectCount = "<< objectCount << endl;}~HowMany() {objectCount--;print("~HowMany()");}};int HowMany::objectCount = 0; // Pass and return BY VALUE:HowMany f(HowMany x) {x.print("x argument inside f()");return x;} int main() {HowMany h;HowMany::print("after construction of h");HowMany h2 = f(h);HowMany::print("after call to f()");} ///:~ 解决方案 The reason is that, because you don''t have a user defined copyconstructor for class HowMany, the compiler generated one for you (ofcourse you cannot see any sign of it being called). When h is passed byvalue, the automatically augmented copy constructor, whose behavior inthis case is simply doing bitwise copy, is called, though it cannot seeit being called. So actually the symmetry still exists, with the copyconstructor invisible to you.HowMany::HowMany(void): objectCount = 1after construction of h: objectCount = 1HowMany::HowMany(const HowMany&): objectCount = 2x argument inside f(HowMany): objectCount = 2HowMany::HowMany(const HowMany&): objectCount = 3HowMany::~HowMany(void): objectCount = 2after call to f(): objectCount = 2HowMany::~HowMany(void): objectCount = 1HowMany::~HowMany(void): objectCount = 0 No I think you have a stupid author who is trying to make the simplecomplicated. john 这篇关于复制建设的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-31 08:28