pre,post increment标准行为和friend函数声明 [英] pre, post increment standard behaviour, and friend function declaration

查看:77
本文介绍了pre,post increment标准行为和friend函数声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在玩耍时检查< blah>对于我的个人项目,我发现

我的代码异常,使用(cringe)visual studio ver 6编译:

#include< iostream>


使用命名空间std;


int main(无效)

{

int k = 3;

cout<< k;

cout<< " " << k ++<< " " << k<< " " << ++ k<< " " ;;

cout<< k<< " \ n";

}


产生预期的输出:

3 4 4 4 5




#include< iostream>


使用命名空间std;


int main(无效)

{

int k = 3;

cout<< k;

cout<< " " << k ++<< " " << k<< " " << ++ k<< " " << k<< " \ n";

}


产生的输出

3 4 4 4 3


问:这是非标准微软编译器的另一个例子,还是链接函数调用的结果




和另一个问题:

问:是否可以将类B的构造函数声明为朋友函数

到A类,如果是,那么语法是什么?


提前感谢,

ed。

while playing around to check <blah> for my personal project, I discovered
an anomaly with my code, compiled using (cringe) visual studio ver 6:
#include<iostream>

using namespace std;

int main( void )
{
int k = 3;
cout << k;
cout << " " << k++ << " " << k << " " << ++k << " ";
cout << k << "\n";
}

produces the expected output of:
3 4 4 4 5

while

#include<iostream>

using namespace std;

int main( void )
{
int k = 3;
cout << k;
cout << " " << k++ << " " << k << " " << ++k << " " << k << "\n";
}

produced the output of
3 4 4 4 3

Q: Is this another example of non-standard microsoft compilers, or a result
of the chained function calls?

And another question:
Q: Is it possible to declare the constructor of class B as a friend function
to class A, and if so what is the syntax?

thanks in advance,
ed.

推荐答案

eddiew_AUS写道:
eddiew_AUS wrote:
在玩游戏时检查< blah>对于我的个人项目,


什么是< blah>?

我发现我的代码异常,使用(cringe)visual编译
工作室版本6:

#include< iostream>
使用命名空间std;

int main(void)
{
int k = 3;
cout<< k;
cout<< " " << k ++<< " " << k<< " " << ++ k<< " " ;;
cout<< k<< \ n;
}
产生预期的输出:
3 4 4 4 5


我不喜欢我明白为什么你会这么想。

#include< iostream>

使用命名空间std;

int main(void)
{k / 3;
cout<< k;
cout<< " " << k ++<< " " << k<< " " << ++ k<< " " << k<< \ n;
}
产生了输出
3 4 4 4 3
问:这是非另一个例子吗标准的微软编译器,或链接函数调用的结果?


这是多次改变k'值而没有序列的结果

点。该代码的行为是未定义的。

另一个问题:
问:是否可以将类B的构造函数声明为A类的朋友
函数,如果那么语法是什么?
while playing around to check <blah> for my personal project,
What is "<blah>"?
I discovered an anomaly with my code, compiled using (cringe) visual
studio ver 6:
#include<iostream>

using namespace std;

int main( void )
{
int k = 3;
cout << k;
cout << " " << k++ << " " << k << " " << ++k << " ";
cout << k << "\n";
}

produces the expected output of:
3 4 4 4 5
I don''t see why you would expect that.

while

#include<iostream>

using namespace std;

int main( void )
{
int k = 3;
cout << k;
cout << " " << k++ << " " << k << " " << ++k << " " << k << "\n";
}

produced the output of
3 4 4 4 3

Q: Is this another example of non-standard microsoft compilers, or a
result of the chained function calls?
It''s the result of changing k''s value multiple times without a sequence
point in between. The behaviour of that code is undefined.
And another question:
Q: Is it possible to declare the constructor of class B as a friend
function to class A, and if so what is the syntax?




我不这么认为。构造函数没有名字。



I don''t think so. Constructors don''t have a name.


eddiew_AUS写道:
eddiew_AUS wrote:

int k = 3;
cout<< k;
cout<< " " << k ++<< " " << k<< " " << ++ k<< " " ;;
cout<< k<< \ n;;
}
产生预期的输出:
3 4 4 4 5


没有预期输出,因为上面已经有了b $ b未定义的行为问:这是非标准微软编译器的另一个例子,还是链接函数调用的结果? ?


这可能是未定义行为的结果。请在google上查看

群组存档。这个话题已经被打死了。

还有一个问题:
问:是否可以将B类的构造函数声明为A类的朋友函数,如果那么语法是什么?

int k = 3;
cout << k;
cout << " " << k++ << " " << k << " " << ++k << " ";
cout << k << "\n";
}

produces the expected output of:
3 4 4 4 5
There is no expected output, since the above has
undefined behaviour

Q: Is this another example of non-standard microsoft compilers, or a result
of the chained function calls?
It is a possible result of undefined behaviour. Please check the
groups archive at google. This topic has been beaten to death.

And another question:
Q: Is it possible to declare the constructor of class B as a friend function
to class A, and if so what is the syntax?




不,不是。建筑师没有名字。


-

Karl Heinz Buchegger
kb ****** @ gascad.at




" Karl Heinz Buchegger" ; < KB ****** @ gascad.at>在消息中写道

news:40 *************** @ gascad.at ...

"Karl Heinz Buchegger" <kb******@gascad.at> wrote in message
news:40***************@gascad.at...
eddiew_AUS写道:
eddiew_AUS wrote:

int k = 3;
cout<< k;
cout<< " " << k ++<< " " << k<< " " << ++ k<< " " ;;
cout<< k<< \ n;
}
产生预期的输出:
3 4 4 4 5

int k = 3;
cout << k;
cout << " " << k++ << " " << k << " " << ++k << " ";
cout << k << "\n";
}

produces the expected output of:
3 4 4 4 5



没有预期输出,因为上面有未定义的行为



There is no expected output, since the above has
undefined behaviour


问:这是非标准微软编译器的另一个例子,还是
的结果链接函数调用?

Q: Is this another example of non-standard microsoft compilers, or a result of the chained function calls?



这可能是未定义行为的结果。请检查谷歌的
组档案。这个话题被打死了。



It is a possible result of undefined behaviour. Please check the
groups archive at google. This topic has been beaten to death.


还有一个问题:
问:是否可以将B类的构造函数声明为朋友
函数为A类,如果是,那么语法是什么?

And another question:
Q: Is it possible to declare the constructor of class B as a friend function to class A, and if so what is the syntax?



不,不是。建筑师没有名字。

-
Karl Heinz Buchegger
kb ****** @gascad.at




感谢您的回复。我现在明白两个答案背后的原因。 :/


欢呼,编辑。



Thanks for the replies. I now understand the reasons behind both answers. :/

cheers, ed.


这篇关于pre,post increment标准行为和friend函数声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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