意外的行为 [英] Unexpected behaviour

查看:75
本文介绍了意外的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码

#include< iostream>


class Blah

{

int i;


public:


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>

是为第二个对象生成的,而没有为第一个生成

的第一个。

为什么没有为第一个生成消息,为什么只有一个消息默认

构造函数消息是否为第二个产生?


我预计:


C:\c> temp

默认构造函数调用!

复制构造函数调用!

默认构造函数调用!

复制构造函数调用!


C:\c>


-

Ioannis Vranos

< a rel =nofollowhref =http://www23.brinkster.com/noicystarget =_ blank> http://www23.brinkster.com/noicys

解决方案



Ioannis Vranos < iv*@guesswh.at.grad.com>在消息中写道

news:ci *********** @ ulysses.noc.ntua.gr ...

代码

#include< iostream>

班级Blah
{
int i;

公开:
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 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?

I expected:

C:\c>temp
Default 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 message
news: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 this

Blah poo1(Blah());

is a function prototype. You can check this by adding the function call at
the end of main.

poo1(0);

Have you been listening to JKop again?

john


Ioannis 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 statement

Blah 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 the function call at the end of main.

poo1(0);

Have you been listening to JKop again?

john


Now that''s embarassing...
-JKop


这篇关于意外的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆