如何在g ++中初始化引用 [英] how to initialise a reference in g++

查看:68
本文介绍了如何在g ++中初始化引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家一起,


以下代码在MS VC上编译正常但在gcc上没编译。我做了什么?b $ b做错了什么?是否有通用编码标准?


干杯,

Arik


---

class abc {

etc.etc。

}


void def(){

abc& ref = abc();

etc.etc。

}

Hi together,

following code does compile fine on MS VC but not on gcc. What am I
doing wrong? Is there a universal coding standard?

Cheers,
Arik

---
class abc {
etc.etc.
}

void def() {
abc &ref = abc();
etc.etc.
}

推荐答案

Arik Funke发布:
Arik Funke posted:
大家好,

以下代码在MS VC上编译正常,但在gcc上没编译。我做错了什么?是否有通用编码标准?

干杯,
Arik

---等级abc {
等等。
}

void def(){
abc& ref = abc();
etc.etc。
}
Hi together,

following code does compile fine on MS VC but not on gcc. What am I
doing wrong? Is there a universal coding standard?

Cheers,
Arik

---
class abc {
etc.etc.
}

void def() {
abc &ref = abc();
etc.etc.
}



像往常一样,Visual C ++是错误的。

abc const& ref = abc();

是有效的。这是(奇怪的)原因:

首先,几点:


1)Temporaries *不是* const。


2)临时是一个r值,*不是一个l值。


对于一个类,这只会产生* * *差异,是:


你不能绑定对临时的引用...除非引用是const。

-JKop


As usual, Visual C++ is wrong.
abc const &ref = abc();
would be valid. Here''s the (weird) reason:
Firstly, a few points:

1) Temporaries are *not* const.

2) A temporary is an r-value, *not* an l-value.

For a class, this makes only *one* difference, which is:

You cannot bind a reference to a temporary... unless the reference is const.
-JKop


2004年10月26日星期二12:47:58 -0700,Arik Funke写道:
On Tue, 26 Oct 2004 12:47:58 -0700, Arik Funke wrote:
嗨在一起,
以下代码在MS VC上编译正常但在gcc上没编译。我做错了什么?是否有通用编码标准?
干杯,
Arik

---等级abc {
等等。
}

void def(){
abc& ref = abc();
等等。
}
Hi together,

following code does compile fine on MS VC but not on gcc. What am I
doing wrong? Is there a universal coding standard?
Cheers,
Arik

---
class abc {
etc.etc.
}

void def() {
abc &ref = abc();
etc.etc.
}



编译器说错了什么? -Wall标志也很有用。


What does the compiler say is wrong ? -Wall flag can be useful as well.


Arik Funke写道:
Arik Funke wrote:
下面的代码在MS VC上编译正常但在gcc上没编译。我做错了什么?


你试图将对非const''abc''的引用绑定到临时。

是否有通用编码标准?


是的,它被称为INCITS / ISO / IEC 14882-2003编程语言 - C ++。

它是'国际标准。

欢呼,
Arik

---等级abc {
等等。


你的意思是

//等等。

}
;
void def() {
abc& ref = abc();


不能这样做。必须这样做


abc const& ref = abc();

等。


你的意思是

//等等。

}
following code does compile fine on MS VC but not on gcc. What am I
doing wrong?
You''re trying to bind a reference to non-const ''abc'' to a temporary.
Is there a universal coding standard?
Yes, it''s called "INCITS/ISO/IEC 14882-2003 Programming Languages - C++".
It''s an international standard.

Cheers,
Arik

---
class abc {
etc.etc.
You mean
// etc.etc.
} ;
void def() {
abc &ref = abc();
Can''t do that. Have to do

abc const & ref = abc();
etc.etc.
You mean
// etc.etc.
}




V



V


这篇关于如何在g ++中初始化引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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