C ++中的int *和int []之间的差异 [英] Difference between int* and int[] in C++

查看:143
本文介绍了C ++中的int *和int []之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上下文:C ++
考虑下面的例子

Context: C++ Consider the example below

class TestClass
{
private:
    int A[];
    int *B;

public:
    TestClass();
};

TestClass::TestClass()
{
    A = 0; // Fails due to error: incompatible types in assignment of `int' to `int[0u]'
    B = 0; // Passes
}

A = 0失败,但B = 0成功。什么是捕获? A是什么?常量指针?

A = 0 fails but B = 0 succeeds. What's the catch? What exactly is A? A constant pointer? How do I initialize it then?

推荐答案

它们之间的唯一区别是 int A [] 不能编译,而且不应该编译!

The only difference between them is that int A[] in a class would not compile, and should not compile!


"ComeauTest.c", line 4: error:   
incomplete type is not allowed   
      int A[];
          ^ 


Wikipedia 说,


Comeau C / C ++已被视为
最符合标准的C ++
编译器。

会建议:即使您的编译器编译,也不要编写此类代码。

I therefore would suggest : Don't write such code even if your compiler compiles it.

这篇关于C ++中的int *和int []之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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