将变量声明为函数是什么意思?
What does it mean to declare a variable as a function?
在下面的例子中,我做了
MyClass a ();
我被告知a实际上是一个返回MyClass的函数,但下面两行都不起作用。
MyClass b = a();
a.a = 1;
那么a是什么,我可以用它做什么?
#include "stdafx.h"
#include "iostream"
using namespace std;
class MyClass {
public:
int a;
};
int _tmain(int argc, _TCHAR* argv[])
{
MyClass a ();
// a is a function? what does that mean? what can i do with a?
int exit; cin >> exit;
return 0;
}
这是一个函数声明。它只是声明了一个名为我被告知a实际上是一个返回MyClass[…]的函数
a
的函数,并让编译器知道它的存在及其签名(在本例中,该函数不接受任何参数,并返回一个类型为MyClass
的对象)。这意味着您可以稍后提供该函数的定义:
#include "iostream"
using namespace std;
class MyClass {
public:
int a;
};
int _tmain()
{
MyClass a (); // This *declares* a function called "a" that takes no
// arguments and returns an object of type MyClass...
MyClass b = a(); // This is all right, as long as a definition for
// function a() is provided. Otherwise, the linker
// will issue an undefined reference error.
int exit; cin >> exit;
return 0;
}
MyClass a() // ...and here is the definition of that function
{
return MyClass();
}
相关文章:
- 这个语法std::class<>{}(arg1, arg2) 在C++中是什么意思?
- 标准 N3337 5.2.10 第 7 条中的C++"类型"是什么意思?
- "类模板示例<int>;"语句对 C++11 是什么意思?
- 线应该是什么意思
- "CID"在AT+HTTPPARA= "CID" ,1中是什么意思
- 表达式"b=(b-x)&x"是什么意思?
- 这个表达是什么意思?
- 此代码验证公式是什么意思?
- 跟踪日志中的T.11803()是什么意思?
- 这个额外的关键字在这个 c++ 类声明中是什么意思?
- * 和 ** 在 C++ 函数声明中是什么意思?
- _T("xyz")是什么意思?
- #define Dbg(fmt,..) (0) 是什么意思? 警告:表达式无效
- 这行代码到底是什么意思?
- 在命名空间名称之前加上 :: 是什么意思?
- 从字符数组的元素中减去'a'是什么意思
- "friend"关键字在C++中是什么意思?
- 减法中的"0"是什么意思?
- 字符数组前面的加号是什么意思?
- 这里的字符串函数是什么意思