Class 范围内都考虑了什么?
What all are considered in the Class scope?
只是想抓住围绕 C++ 中 Class 范围的概念。如果我们举个例子:
#include <iostream>
using namespace std;
string barValue="OUTSIDE CLASS";
class foo
{
public:
void print_bar()
{
cout<<barValue<<endl;
}
};
int main()
{
foo f;
f.print_bar();
return 0;
}
我在想:
barValue
变量是否在 Class 范围内?
- 即使
barValue
在包含的文件之一中定义,也可以接受吗?
- 文件和所有包含的文件是否可以作为
foo
Class的范围调用?
- 我能理解如果
barValue
定义在Class体之后,将不会被考虑。要跟进的是,这意味着所有包含的文件都在 Class 正文之前,并且通常在当前文件内容之前?
问题 1
Is the barValue variable is in the class scope?
barValue
是一个 全局变量 因为你已经在任何函数和 class 之外定义了它。因此 barValue
可以像您一样在 class 中使用。
问题二
Can the barValue be accepted even if it is defined in one of the included files?
是的,即使在头文件中定义了 barValue
它也可以在其他文件中使用 because/if 它具有 外部链接 如下所示例如。
file.h
int barvalue = 34;//barvalue has external linkage
main.cpp
#include <iostream>
#include "file.h"
int main()
{
std::cout<<barvalue<<std::endl;//prints 34
return 0;
}
问题3
Is the file and all the included files can be called as the scope of the foo class?
没有
问题4
I can understand that the barValue
will not be considered if it is defined after the class body. To follow up is that means all the included files comes before the Class body and in general before the current file content?
你应该进一步澄清这个问题,因为我认为你在这里问的问题还不清楚,但我会尽力回答我从你的问题中理解的内容。
如果您在 class foo
定义之后定义 barValue
那么该程序将无法运行,因为您在 class 定义之后定义了它,因此它具有在你写 cout<<barValue<<endl;
时没有 defined/declared ,这是错误的意思,如下所示:
#include <iostream>
using namespace std;
class foo
{
public:
void print_bar()
{
cout<<barValue<<endl;//error: ‘barValue’ was not declared in this scope
}
};
int main()
{
foo f;
f.print_bar();
return 0;
}
string barValue="OUTSIDE CLASS";
只是想抓住围绕 C++ 中 Class 范围的概念。如果我们举个例子:
#include <iostream>
using namespace std;
string barValue="OUTSIDE CLASS";
class foo
{
public:
void print_bar()
{
cout<<barValue<<endl;
}
};
int main()
{
foo f;
f.print_bar();
return 0;
}
我在想:
barValue
变量是否在 Class 范围内?- 即使
barValue
在包含的文件之一中定义,也可以接受吗? - 文件和所有包含的文件是否可以作为
foo
Class的范围调用? - 我能理解如果
barValue
定义在Class体之后,将不会被考虑。要跟进的是,这意味着所有包含的文件都在 Class 正文之前,并且通常在当前文件内容之前?
问题 1
Is the barValue variable is in the class scope?
barValue
是一个 全局变量 因为你已经在任何函数和 class 之外定义了它。因此 barValue
可以像您一样在 class 中使用。
问题二
Can the barValue be accepted even if it is defined in one of the included files?
是的,即使在头文件中定义了 barValue
它也可以在其他文件中使用 because/if 它具有 外部链接 如下所示例如。
file.h
int barvalue = 34;//barvalue has external linkage
main.cpp
#include <iostream>
#include "file.h"
int main()
{
std::cout<<barvalue<<std::endl;//prints 34
return 0;
}
问题3
Is the file and all the included files can be called as the scope of the foo class?
没有
问题4
I can understand that the
barValue
will not be considered if it is defined after the class body. To follow up is that means all the included files comes before the Class body and in general before the current file content?
你应该进一步澄清这个问题,因为我认为你在这里问的问题还不清楚,但我会尽力回答我从你的问题中理解的内容。
如果您在 class foo
定义之后定义 barValue
那么该程序将无法运行,因为您在 class 定义之后定义了它,因此它具有在你写 cout<<barValue<<endl;
时没有 defined/declared ,这是错误的意思,如下所示:
#include <iostream>
using namespace std;
class foo
{
public:
void print_bar()
{
cout<<barValue<<endl;//error: ‘barValue’ was not declared in this scope
}
};
int main()
{
foo f;
f.print_bar();
return 0;
}
string barValue="OUTSIDE CLASS";