如何在将const分配给const引用时让C ++编译器引发警告或错误。 [英] How to let C++ compiler raise a warning or error when a const is assigned to a const reference.

查看:62
本文介绍了如何在将const分配给const引用时让C ++编译器引发警告或错误。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我有以下哪个有错误(请参阅注释行)。


有bug的人有输出

10

-1073752704


没有bug的那个有输出

10

10


然而,我正在使用的编译器无法生成任何警告或

错误对于这个bug。我想知道是否有选项可以选择这样的

编译器可以告诉这个错误吗?


谢谢,

Peng


#include< iostream>


A级{

public:

A(const int a):_ a(a){//应该是:A(const int& a):_ a(a){

}

void print()const {

std :: cout<< _a<< std :: endl;

}

私人:

const int& _a;

};


B级{

公开:

B(const A& a):_ a(a){

};

void print()const {

_a.print();

}

私人:

const A& _a;

};


int main(){

int i = 10;

a a(i);

a.print();

B b(a);

b.print();

}

Hi,

I have the following which has a bug (see the commented line).

The one with the bug has the output of
10
-1073752704

The one without the bug has the output of
10
10

However, the compiler that I''m using can''t generate any warning or
error for this bug. I''m wondering if there is a option to select such
that the compiler can tell this error?

Thanks,
Peng

#include <iostream>

class A{
public:
A(const int a) : _a(a) { //should be: A(const int &a) : _a(a) {
}
void print() const {
std::cout << _a << std::endl;
}
private:
const int &_a;
};

class B{
public:
B(const A &a) : _a(a) {
};
void print() const {
_a.print();
}
private:
const A &_a;
};

int main(){
int i = 10;
A a(i);
a.print();
B b(a);
b.print();
}

推荐答案

Pe ******* @ gmail.com 写道:

Pe*******@gmail.com wrote:


我有以下哪个有bug(请参阅注释行)。


有bug的那个有输出

10

-1073752704


没有错误的那个有输出

10

10


然而,我正在使用的编译器无法生成任何警告或

这个错误的错误。我想知道是否有选项可以选择这样的

,编译器可以告诉这个错误?
I have the following which has a bug (see the commented line).

The one with the bug has the output of
10
-1073752704

The one without the bug has the output of
10
10

However, the compiler that I''m using can''t generate any warning or
error for this bug. I''m wondering if there is a option to select such
that the compiler can tell this error?



也许是这样,也许不是。既然你没有识别编译器,

虽然,怎么会有人帮你?您最好的选择是查阅编译器的

文档(例如,
http://gcc.gnu.org/onlinedocs/gcc-4....l#Invoking-GCC

for gcc 4.1.1)或该编译器的邮件列表或论坛。


祝你好运,


汤姆

Perhaps so, perhaps not. Since you don''t identify the compiler,
though, how could anyone help you? Your best bet is to consult the
documentation for the compiler (e.g.,
http://gcc.gnu.org/onlinedocs/gcc-4....l#Invoking-GCC
for gcc 4.1.1) or a mailing list or forum for that compiler.

Best regards,

Tom


也许是这样,也许不是。既然你没有识别编译器,
Perhaps so, perhaps not. Since you don''t identify the compiler,

虽然,怎么可能有人帮你?您最好的选择是查阅编译器的

文档(例如,
http://gcc.gnu.org/onlinedocs/gcc-4....l#Invoking-GCC

for gcc 4.1.1)或该编译器的邮件列表或论坛。
though, how could anyone help you? Your best bet is to consult the
documentation for the compiler (e.g.,
http://gcc.gnu.org/onlinedocs/gcc-4....l#Invoking-GCC
for gcc 4.1.1) or a mailing list or forum for that compiler.



我正在使用gcc-3.4。阅读GCC文档太长了。你知道我应该读哪个部分吗?


谢谢,



I''m using gcc-3.4. It is too long to read the GCC documentation. Do you
know which section I should read?

Thanks,
Peng


Pe*******@gmail.com 发布:
Pe*******@gmail.com posted:

A(const int a):_ a(a){//应该是:A(const int& a):_ a(a)
A(const int a) : _a(a) { //should be: A(const int &a) : _a(a)



你唯一的错误是:


你没有读回你的代码。


我想我会生产高质量的代码,但有时候,我会发布有缺陷的代码。我生成的这种有缺陷的代码和_real_代码

之间的区别在于我回读了我的代码。


如果你不能读回来你的代码并注意星号和&符号的位置(或

缺少),然后拿起植物学。


-


Frederick Gotham


The only bug you have is:

"You didn''t read back over your code."

I would like to think that I produce quality code, but from time to time, I
post flawed code. The difference between such flawed code and _real_ code
that I produce is that I read back over my code.

If you can''t read back over your code and pay attention to the placement (or
lack thereof) of asterisks and ampersands, then take up botany.

--

Frederick Gotham


这篇关于如何在将const分配给const引用时让C ++编译器引发警告或错误。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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