C ++是一种类型安全的语言吗? [英] Is C++ a type-safe language ??

查看:110
本文介绍了C ++是一种类型安全的语言吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


据说C ++是一种强类型语言,因此是一种类型安全的
语言(与C不同)。那么如何解释以下行为:


int main(无效)

{

char * p = NULL;

p =" A String Literal" ;; //编译器在这里没有错误/警告

//但理想情况下它应该......因为p是非const

//指针和字符串文字的类型为

// const char *

//所以,从const-ptr转换为非-const

//应该从编译器中引出警告/错误!!


返回0;

}


我已经在Windows 2000(Intel P IV)上的MSVC ++ 6编译器上尝试了它。
和GNU C ++编译器gcc 3.x RedHat GNU \ Linux(英特尔) P IV)


提前致谢。

Hi all,

It is said that C++ is a strongly typed language and thus a type-safe
language (unlike C). So how does one explain the following behaviour :

int main(void)
{
char *p = NULL;
p = "A String Literal";//the compiler isuues no error/warning here
// but ideally it should...as p is a non-const
// pointer and the string literal has the type
// const char *
// So, a conversion from const-ptr TO non-const
// should elicite warning/error from the compiler !!

return 0;
}

I''ve tried it on both MSVC++ 6 compiler on Windows 2000 ( Intel P IV )
and GNU C++ compiler gcc 3.x RedHat GNU\Linux ( Intel P IV )

Thanks in advance.

推荐答案

2004年7月1日21:58:16 - 0700,Nitin Bhardwaj< ni ************* @ hotmail.com>

写道:
On 1 Jul 2004 21:58:16 -0700, Nitin Bhardwaj <ni*************@hotmail.com>
wrote:
大家好,

据说C ++是一种强类型语言,因而是一种类型安全的语言(与C不同)。那么如何解释以下行为:

int main(void)
{* * char * p = NULL;
p =" A String Literal" ;; //编译器在这里没有错误/警告
//但理想情况下它应该......因为p是非const
//指针而字符串文字具有类型
// const char *
//因此,从const-ptr转换为非const
//应该从编译器中引发警告/错误!!

返回0;
}
我已经在Windows 2000(Intel P IV)上的MSVC ++ 6编译器和GNU C ++编译器gcc 3.x RedHat GNU \ Linux(Intel P)上尝试过它IV)

提前致谢。
Hi all,

It is said that C++ is a strongly typed language and thus a type-safe
language (unlike C). So how does one explain the following behaviour :

int main(void)
{
char *p = NULL;
p = "A String Literal";//the compiler isuues no error/warning here
// but ideally it should...as p is a non-const
// pointer and the string literal has the type
// const char *
// So, a conversion from const-ptr TO non-const
// should elicite warning/error from the compiler !!

return 0;
}

I''ve tried it on both MSVC++ 6 compiler on Windows 2000 ( Intel P IV )
and GNU C++ compiler gcc 3.x RedHat GNU\Linux ( Intel P IV )

Thanks in advance.




它显然是非类型安全的,但是为了向后兼容。当C

被发明时,没有const,当C引入const时,感觉

如果禁止上述内容,过多的现有代码就会破坏。

C ++保留了这个。


john



It''s clearly non-type safe but is done for backward compatibility. When C
was invented there was no const and when C introduced const it was felt
that too much existing code would break if the above was forbidden.
C++ has retained this.

john




" Nitin Bhardwaj" < NI ************* @ hotmail.com>在留言中写道

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

"Nitin Bhardwaj" <ni*************@hotmail.com> wrote in message
news:17**************************@posting.google.c om...
大家好,

据说C ++是一种强类型语言,因此是一种类型安全的语言(与C不同)。那么如何解释以下行为:

int main(void)
{* * char * p = NULL;
p =" A String Literal" ;; //编译器在这里没有错误/警告
//但理想情况下它应该......因为p是非const
//指针而字符串文字具有类型
// const char *
//因此,从const-ptr转换为非const
//应该从编译器中引发警告/错误!!

返回0;
}
我已经在Windows 2000(Intel P IV)上的MSVC ++ 6编译器和GNU C ++编译器gcc 3.x RedHat GNU \ Linux(Intel P)上尝试过它IV)
Hi all,

It is said that C++ is a strongly typed language and thus a type-safe
language (unlike C). So how does one explain the following behaviour :

int main(void)
{
char *p = NULL;
p = "A String Literal";//the compiler isuues no error/warning here
// but ideally it should...as p is a non-const
// pointer and the string literal has the type
// const char *
// So, a conversion from const-ptr TO non-const
// should elicite warning/error from the compiler !!

return 0;
}

I''ve tried it on both MSVC++ 6 compiler on Windows 2000 ( Intel P IV )
and GNU C++ compiler gcc 3.x RedHat GNU\Linux ( Intel P IV )




const-ness绝对是类型安全中棘手的问题之一。


下一个问题?


Rufus



const-ness is definitely one of the thornier issues in type safety.

next question?

Rufus


* Nitin Bhardwaj:
* Nitin Bhardwaj:

据说C ++是强类型的语言,因此是一种类型安全的语言(与C不同)。


这是不正确的。


但是,C ++在更多方面支持类型安全。


因此可以用(或多或少)类型安全的方式用C ++编程。

那么如何解释以下行为:

int main(void)
{* * char * p = NULL;
p =" A String Literal" ;; //编译器在这里没有错误/警告
//但理想情况下它应该......因为p是一个非const
//指针,字符串文字的类型为
// const char *
//所以,从const-ptr转换为非const
//应该从编译器中引出警告/错误!!

返回0;
}

我试过了Windows 2000(Intel P IV)上的MSVC ++ 6编译器和GNU C ++编译器gcc 3.x RedHat GNU \ Linux(Intel P IV)

It is said that C++ is a strongly typed language and thus a type-safe
language (unlike C).
That is incorrect.

However, C++ supports type-safety in more ways than C.

So it''s possible to program in C++ in (more or less) type-safe ways.
So how does one explain the following behaviour :

int main(void)
{
char *p = NULL;
p = "A String Literal";//the compiler isuues no error/warning here
// but ideally it should...as p is a non-const
// pointer and the string literal has the type
// const char *
// So, a conversion from const-ptr TO non-const
// should elicite warning/error from the compiler !!

return 0;
}

I''ve tried it on both MSVC++ 6 compiler on Windows 2000 ( Intel P IV )
and GNU C++ compiler gcc 3.x RedHat GNU\Linux ( Intel P IV )




它与C的向后兼容性。


还有其他更可怕的兼容性派生问题。


例如,在对象数组的上下文中,自动将数组类型转换为指针类型和将指针视为数组。$ br / >

另一方面,在某些方面,C ++比类型更安全。

Java。


例如,模板支持在许多

情况下启用静态类型检查,在这种情况下(旧的非泛型)Java是不可能的; C ++ har

链接时更严格的类型检查(尽管不是100%); C ++

构造函数强制执行类不变量,而Java构造函数则不执行。


-

答:因为它弄乱了订单人们通常会阅读文字。

问:为什么这么糟糕?

A:热门帖子。

问:什么是usenet和电子邮件中最烦人的事情是什么?



It''s backward compatibility with C.

There are other far more horrendous compatibility-derived issues.

For instance, automatic conversion of array type to pointer type and
treating pointers as arrays, in the context of an array of objects.

On the other hand, in some respects C++ is more type-safe than e.g.
Java.

For example, template support enables static type-checking in many
situations where it''s not possible in (old non-generic) Java; C++ har
more stringent type-checking (although not 100%) at link time; C++
constructors enforce class invariants while Java constructors do not.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


这篇关于C ++是一种类型安全的语言吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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