简体   繁体   English

这句话是什么意思?

[英]What does this statement mean?

I dont undestand this statement: f().array; 我不反对这句话: f().array; ... is a function call? ...是函数调用? or an access an attribute of the class? 或访问该类的属性? The statement data f(); 声明data f(); is constructor's call? 是构造函数的调用? that's right? 那就对了?

#include <iostream>
using namespace std;

void f()
{
    cout << "hello!" << endl;
}

struct data 
{ 
    int array[10]; 
};

int main()
{
    data f();

    f().array;

    return 0 ;
}

The void f() and following lines define a function, f which takes parameters and return nothing. void f()和后面的行定义了一个函数f ,它接受参数并且不返回任何内容。

The line data f(); data f(); declares a function f, which takes no parameters but return an object of type data . 声明一个函数f,它不接受任何参数,但返回一个data类型的对象。 Since we've already seen the definition of f , we know that this line is actually lying to us, but the compiler lets it lie, because it wants to remain compatible with C where the definition could have been the one who's lying. 既然我们已经看过f的定义,我们知道这条线实际上是骗我们的,但是编译器让它说谎,因为它想要保持与C的兼容,而定义可能就是那个说谎的人。

Got that? 了解?

UPDATE: 更新:

Originally, C didn't have the void keyword. 最初,C没有void关键字。 So, to say that f() didn't return anything, you just left off the return type -- except the designers were lazy typists, so they decided to actually make "type not specified" mean "default to int" because they were returning ints more often than nothing. 所以,假设f()没有返回任何东西,你只是停止了返回类型 - 除了设计师是懒惰的打字员,所以他们决定实际上使“未指定类型”意味着“默认为int”因为它们是返回的内容往往比什么都没有。 So, f() could be defining a function which returned nothing or an int. 因此, f()可以定义一个不返回任何内容的函数或一个int。 I think compilers were actually cool with you writing something like if (x==1) return 5; else return "five"; 我认为编译器实际上很酷,你写的是if (x==1) return 5; else return "five"; if (x==1) return 5; else return "five"; and letting the calling function figure out the return value. 并让调用函数计算出返回值。

Which brings us to data f(); 这带给我们data f(); . Since you've just told the compiler that f() returns a data object, it trusts you, and ignores what you told it before. 由于您刚刚告诉编译器f()返回一个data对象,它会信任您,并忽略您之前告诉它的内容。 Of course, after the main() function, the new declaration of f() goes out of scope, and the compiler goes back to using the original signature for f() . 当然,在main()函数之后, f()的新声明超出了范围,编译器返回使用f()的原始签名。

The bottom line is, while legal, which is VERY BAD CODE (tm), and whoever wrote it should be slapped. 最重要的是,虽然是合法的,但它是非常糟糕的代码(tm),任何写它的人都应该被打耳光。

I dont undestand this statement: f().array; 我不反对这句话: f().array; ... is a function call? ...是函数调用? or an access an attribute of the class? 或访问该类的属性?

Both. 都。 It calls the function f , then accesses the return value's array member (but does nothing with it, making the member access pointless). 它调用函数f ,然后访问返回值的array成员(但对它没有任何作用,使成员访问无意义)。

The statement data f(); 声明data f(); is constructor's call? 是构造函数的调用?

No, it's a function declaration. 不,这是一个功能声明。 In this case, it should give an error because you've already declared f with void return type. 在这种情况下,它应该给出一个错误,因为你已经用void返回类型声明了f

To declare an object, you want: 要声明一个对象,您需要:

data f;          // default-initialisation: leaves the array uninitialised
data f{};        // value-initialisation (since C++11): zero-initialise the array
data f = data(); // value-initialisation (historic): zero-initialise the array

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM