一对问题 [英] a pair of questions

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

问题描述

您好,


1)符合标准的编译器是否应接受以下代码:


void Foo(){/ * .. 。* /

class Foo {/ * ... * /};


int main()

{

Foo();

返回0;

}


如果是,你怎么看?语言允许它?顺便说一下,你可以预测程序会做什么 - 构建对象或者调用

函数吗?


2)你能说出这个短节目的确切打印吗?


#include< iostream>

使用std :: cout;


class MyClass {

public:

MyClass():i_(2){}

MyClass(const MyClass& obj){i_ * = obj.i_; }

~MyClass(){cout<< i_<<的std :: ENDL; }

私人:

int i_;

};


int main()

{

MyClass(MyClass());

返回0;

}


-Alexander

Hello,

1) Should a standard compliant compiler accept the following code:

void Foo() { /* ... */ }
class Foo { /* ... */ };

int main()
{
Foo();
return 0;
}

If yes, what do you think the language allows it for? By the way, can
you predict what the program will do -- construct the object or call
the function?

2) Can you tell what exactly this short program will print?

#include<iostream>
using std::cout;

class MyClass {
public:
MyClass() : i_(2) {}
MyClass(const MyClass& obj) { i_ *= obj.i_; }
~MyClass() { cout << i_ << std::endl; }
private:
int i_;
};

int main()
{
MyClass( MyClass() );
return 0;
}

-Alexander

推荐答案

2003年7月12日07:28:49 -0700, ko ****** @ rambler.ru (Alexander Korovyev)写道:
On 12 Jul 2003 07:28:49 -0700, ko******@rambler.ru (Alexander Korovyev) wrote:
1)符合标准的编译器是否应该接受以下代码:

void Foo(){/ * ... * /}
类Foo {/ * ... * /};

int main()
{
Foo();
返回0;
}
如果是,您认为该语言允许的是什么?顺便说一句,你可以预测程序会做什么 - 构建对象或调用函数吗?


如果这是一个真正的问题,建议是使用

不同的名字。


但是作为一个家庭作业问题的建议是(1)检查你的

教科书,以及(2)检查标准。找到相关的

页面。在你的答案中,不回答是或否,并且

不仅仅引用一个权威来源,还包括

的推理_why_就像它是。

2)你能说出这个短节目的确切印刷品吗?

#include< iostream>
使用std :: cout;

class MyClass {
public:
MyClass():i_(2){}
MyClass(const MyClass& obj){i_ * = obj.i_; }
~MyClass(){cout<< i_<<的std :: ENDL; }
私人:
int i_;
};

int main()
{
MyClass(MyClass());
返回0;
}
1) Should a standard compliant compiler accept the following code:

void Foo() { /* ... */ }
class Foo { /* ... */ };

int main()
{
Foo();
return 0;
}

If yes, what do you think the language allows it for? By the way, can
you predict what the program will do -- construct the object or call
the function?
If this was a real question the advice would be to use
different names.

But as a HOMEWORK PROBLEM the advice is to (1) check your
textbook, and (2) check the standard. Find the relevant
pages. In your answer, don''t answer just yes or no, and
don''t just quote an authoritative source, but also include
the reasoning _why_ it is like it is.
2) Can you tell what exactly this short program will print?

#include<iostream>
using std::cout;

class MyClass {
public:
MyClass() : i_(2) {}
MyClass(const MyClass& obj) { i_ *= obj.i_; }
~MyClass() { cout << i_ << std::endl; }
private:
int i_;
};

int main()
{
MyClass( MyClass() );
return 0;
}




为什么不尝试编译它,如果编译,运行它,

如果它运行且你不理解它,在

a调试器中尝试它?


注意:一个程序用一个特定的程序编译编译器和

编译器选项并不意味着它已根据标准定义了行为,但无论哪种方式你都知道更多。

但你甚至没试过,所以,你是傻吗?



Why don''t you try to compile it, and if it compiles, run it,
and if it runs and you don''t understand it, trying it in
a debugger?

Note: that a program compiles with one specific compiler and
compiler options doesn''t mean it has defined behavior according
to the standard, but either way you know a lot more.

But you didn''t even try, so, are you stupid?




亚历山大Korovyev" < KO ****** @ rambler.ru>在消息中写道

news:26 ************************** @ posting.google.c om ...

"Alexander Korovyev" <ko******@rambler.ru> wrote in message
news:26**************************@posting.google.c om...
您好,

1)符合标准的编译器是否应接受以下代码:

void Foo(){/ * ... * / }类foo {/ * ... * /};

int main()
{
Foo();
返回0;
}


我的编译器VC ++ 6编译得很好。当然,VC ++以其

非标准惯例而闻名,但似乎它是完全合法的,并且

应该是。

如果是,您认为该语言允许的是什么?


这与使用Foo()和foo()来表示两个不同的
函数的原因相同。它只是为您提供了更多可以使用的名称。因为它总是明确你的意思,所以这样做是可以的。

顺便说一下,你能预测一下程序是什么吗?会做 - 构建对象或调用
函数?


我刚才说它总是毫不含糊。因此,稍微阅读你的代码将告诉你。它不可能构造对象,因为那里没有

变量,所以没有什么可构建的。因此,它将函数称为

。 IIRC,在这种情况下,defauly构造函数必须被称为Foo :: Foo(),以使其明确无误。

2)你能告诉我们什么这个简短的程序将打印出来吗?

#include< iostream>
使用std :: cout;

类MyClass {
public:
MyClass():i_(2){}
MyClass(const MyClass& obj){i_ * = obj.i_; }
~MyClass(){cout<< i_<<的std :: ENDL; }
私人:
int i_;
};

int main()
{
MyClass(MyClass());
返回0;
}
-Alexander
Hello,

1) Should a standard compliant compiler accept the following code:

void Foo() { /* ... */ }
class Foo { /* ... */ };

int main()
{
Foo();
return 0;
}
My compiler, VC++6, compiles it fine. Of course, VC++ is known for its
non-standard conventions, but it seems like it is perfectly legal, and
should be.

If yes, what do you think the language allows it for?
It''s the same reason you could use Foo() and foo() to mean two different
functions. It just gives you more names you could use. As it will always be
unambiguous to what you mean, it''s ok to do this.
By the way, can
you predict what the program will do -- construct the object or call
the function?
I just said it''s always unambiguous. Thus, reading into your code a bit will
tell you. It could not possibly be constructing the object as there is no
variable there, so there is nothing to construct. It is, therefore, calling
the function. IIRC, the defauly constructor, in this case, would have to be
called as Foo::Foo(), to make it unambiguous.

2) Can you tell what exactly this short program will print?

#include<iostream>
using std::cout;

class MyClass {
public:
MyClass() : i_(2) {}
MyClass(const MyClass& obj) { i_ *= obj.i_; }
~MyClass() { cout << i_ << std::endl; }
private:
int i_;
};

int main()
{
MyClass( MyClass() );
return 0;
}

-Alexander




好​​我的编译器打印2.恕我直言,你很幸运,虽然。在我看来




1)第一个被调用的构造函数使其成为i_ 2.

2 )复制构造函数然后获取它的i_并将其乘以2.由于它的i_

未定义,看起来这个代码也是未定义的。


然后再一次,这是另一种看待它的方式:


MyClass(MyClass());


作为构造函数没有回报价值,这就像说的那样


MyClass(无效);


这跟说的一样


MyClass();


哪个会使i_ 2.

然后再次,因为有两个构造函数被调用

输出不应该有两个2吗?我只见过一个。也许编译器对它进行了优化?


这真的令人困惑,但是这是一个家庭作业还是什么?你有好奇吗?这对我来说似乎没有实际意义......


-

MiniDisc_2k2



Well my compiler prints 2. IMHO, you''re lucky, though. It appears to me
that:

1) the first constructor that''s called makes its i_ 2.
2) the copy constructor then takes its i_ and multiplies it by 2. As its i_
is undefined, it appears that this code is also undefined.

Then again, here''s another way of looking at it:

MyClass(MyClass());

As the constructor(s) don''t return a value, this is the same as saying

MyClass(void);

Which would be the same as saying

MyClass();

Which would make i_ 2.
Then again, as two constructors were called, shouldn''t there be two 2s on
output? I only saw one. Perhaps the compiler optimized it out??

That''s really confusing, but is this a homework assignment or something? Are
you curious? This seems to have no practical purpose to me...

--
MiniDisc_2k2




" Alf P. Steinbach"写道:

[...]

"Alf P. Steinbach" wrote:
[...]
但你甚至没试过,所以,你是傻吗?
But you didn''t even try, so, are you stupid?




小心。 Alf。


问候,

亚历山大。



Careful. Alf.

regards,
alexander.


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

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