编译时关于 1.constructor 和 2.array 定义的疑问很少

发布于 2024-11-29 03:01:55 字数 646 浏览 0 评论 0原文

请帮我理清以下问题的概念: (在linux、gcc上执行和测试)

问题1:

在下面的简单例子中,A a()到底是什么意思?

我发现这不是默认构造函数的定义,而是 a() 是一个返回类型为 A 的函数。

如果它是正确的,那么为什么这段代码没有给我任何链接器错误或运行时错误。这段代码运行和链接都很顺利,就好像它知道函数 a() 的定义一样。

class A
{
  public:
     void print()
     {
       printf("In class A\n");
     }
};


int
  main()
  {
    A a();
    //a.fun();  //throws error "request for member ‘fun’ in ‘a’, which is of type ‘A()’"
  } 

问题2.

在下面的代码中,数组b 的定义抛出错误。 我无法找到这种行为的确切原因。

int a[]={3,4,21,5,7,86};
 int b[a[3]];     //this throws error why???

 int
  main() { ... }

Please help me clear the concepts with following problems:
(executed and tested on linux,gcc)

Problem 1:

In the following simple example what exactly meant by A a()??

I found that this is not a definition of default constructor but a() is a function with return type is A.

If it is correct then why this code does not give me any linker error or runtime error.This code runs and links smoothly as if it knows the definition of function a().

class A
{
  public:
     void print()
     {
       printf("In class A\n");
     }
};


int
  main()
  {
    A a();
    //a.fun();  //throws error "request for member ‘fun’ in ‘a’, which is of type ‘A()’"
  } 

problem 2.

In the following code the definition of array b throws error.
I am not able to find the exact reason for this behavior.

int a[]={3,4,21,5,7,86};
 int b[a[3]];     //this throws error why???

 int
  main() { ... }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

メ斷腸人バ 2024-12-06 03:01:55

问题 1

这被称为最令人烦恼的解析< /a>.以下:

A a();

是函数声明。您没有调用该函数,因此链接器没有义务解析它。这解释了为什么您没有看到任何错误。

问题 2

数组的大小必须用常量表达式指定。 a[3] 不是常量表达式。

Problem 1

This is known as the most vexing parse. The following:

A a();

is a function declaration. You are not calling that function, so the linker is not obliged to resolve it. That explains why you don't see any error.

Problem 2

The size of an array must be specified with a constant expression. a[3] is not a constant expression.

为人所爱 2024-12-06 03:01:55

A a(); 这告诉编译器函数 a 存在。编译器说OK!但由于它从未被使用,链接器从不检查它,因此不存在链接器错误。

int a[]={3,4,21,5,7,86}; 不幸的是,数组的元素不被视为编译时常量,因此无法使用初始化数组或模板参数。您必须以另一种方式设置 B 的大小,或者在运行时动态设置:int *b = new int[a[3]];

A a(); This tells the compiler that a function a exists. Compiler says OK! But since it's never USED, the linker never check its, and so there's no linker error.

int a[]={3,4,21,5,7,86}; Unfortunately, the elements of array are not considered compile time constants and thus cannot be used to initialize arrays nor template parameters. You'll have to set the size of B another way, or do it dynamically at runtime: int *b = new int[a[3]];

像你 2024-12-06 03:01:55

关于问题2:c++不支持可变数组大小

Regarding problem 2 : c++ doesn't support variable array size

二货你真萌 2024-12-06 03:01:55
A a();

它是一个名为 a 的函数声明,不带参数并返回 A。它不是变量声明。


int a[]={3,4,21,5,7,86};
int b[a[3]];     //this throws error why???

在 C++ 中,数组的大小应该是 const 表达式,但 a[3] 不是 const 表达式。因此出现了错误。

但是,如果你这样做:

const int a=21;
int b[a];   //okay - now a is const expression.

那么就可以了。

A a();

Its a function declaration with name a which takes no argument and returns A. Its not a variable declaration.


int a[]={3,4,21,5,7,86};
int b[a[3]];     //this throws error why???

In C++, size of an array should be a const-expression, but a[3] is NOT a const expression. Hence the error.

However if you do this:

const int a=21;
int b[a];   //okay - now a is const expression.

Then its okay.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文