Blah(const Blah& obj) { std :: cout<<"复制构造函数名为!\ n" ;; i = obj.i; } Blah() { std :: cout<<"默认构造函数名为!\ n" ;; i = 0; } }; int main() { Blah poo1(Blah()); Blah poo2 = Blah(); } 我只能得到 C:\ c> temp 默认构造函数叫做! C:\c> 为第二个对象生成而第一个没有生成任何内容。 为什么没有消息是为第一个生成的,为什么只为第二个生成默认的构造函数消息? 因为这个 Blah poo1(Blah()); 是函数原型。你可以通过添加函数调用来检查这个。 在main结尾。 poo1(0); 你有没有再听过JKop? john Ioannis Vranos写道:代码 #include< iostream> 类Blah { int i; 公开: Blah(const Blah& obj) { std :: cout<<"复制构造函数名为!\ n" ;; i = obj。我; } Blah() { std :: cout<<"默认构造函数叫做!\ n" ;; i = 0; } }; int main() { Blah poo1(Blah()); Blah poo2 = Blah(); } 我只能得到 C:\ c> temp 默认构造函数叫做! C:\c> 是为第二个对象生成的,而第一个没有生成任何内容。 声明 Blah poo1(Blah()); 是_declaration_ of一个函数''poo1''。阅读常见问题解答。 为什么没有为第一个生成消息,为什么只有第二个生成默认的构造函数消息? 因为''poo1''不是一个对象。 我的预期: C:\ c> temp 默认构造函数调用!复制构造函数调用!默认构造函数调用!复制构造函数调用! 太糟糕了。学习声明语法。 C:\c> V >因为这个 Blah poo1(Blah()); 是一个函数原型。您可以通过在main的末尾添加函数调用来检查这一点。 poo1(0); 您是否再次收听JKop? 约翰 现在'尴尬...... -JKop For the code#include <iostream>class Blah{int i;public:Blah(const Blah &obj){std::cout<<"Copy Constructor called!\n";i=obj.i;}Blah(){std::cout<<"Default Constructor called!\n"; i=0;}};int main(){Blah poo1(Blah());Blah poo2=Blah();}I only getC:\c>tempDefault Constructor called!C:\c>which is produced for the second object while nothing is produced forthe first.Why no message is produced for the first, and why only a defaultconstructor message is produced for the second?I expected:C:\c>tempDefault Constructor called!Copy Constructor called!Default Constructor called!Copy Constructor called!C:\c>--Ioannis Vranos http://www23.brinkster.com/noicys 解决方案"Ioannis Vranos" <iv*@guesswh.at.grad.com> wrote in messagenews:ci***********@ulysses.noc.ntua.gr... For the code #include <iostream> class Blah { int i; public: Blah(const Blah &obj) { std::cout<<"Copy Constructor called!\n"; i=obj.i; } Blah() { std::cout<<"Default Constructor called!\n"; i=0; } }; int main() { Blah poo1(Blah()); Blah poo2=Blah(); } I only get C:\c>temp Default Constructor called! C:\c> which is produced for the second object while nothing is produced for the first. Why no message is produced for the first, and why only a default constructor message is produced for the second?Because thisBlah poo1(Blah());is a function prototype. You can check this by adding the function call atthe end of main.poo1(0);Have you been listening to JKop again?johnIoannis Vranos wrote: For the code #include <iostream> class Blah { int i; public: Blah(const Blah &obj) { std::cout<<"Copy Constructor called!\n"; i=obj.i; } Blah() { std::cout<<"Default Constructor called!\n"; i=0; } }; int main() { Blah poo1(Blah()); Blah poo2=Blah(); } I only get C:\c>temp Default Constructor called! C:\c> which is produced for the second object while nothing is produced for the first.The statementBlah poo1(Blah());is a _declaration_ of a function ''poo1''. Read the FAQ. Why no message is produced for the first, and why only a default constructor message is produced for the second?Because ''poo1'' is not an object. I expected: C:\c>temp Default Constructor called! Copy Constructor called! Default Constructor called! Copy Constructor called!Too bad. Study the declaration syntax. C:\c>V> Because this Blah poo1(Blah()); is a function prototype. You can check this by adding thefunction call at the end of main. poo1(0); Have you been listening to JKop again? johnNow that''s embarassing...-JKop 这篇关于意外的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-20 18:20