类声明问题. [英] A class declaration problem.
问题描述
我目前正在编写一个简单的C ++应用程序.我从网站上下载了一些源代码,但我不理解类声明.
这是代码;
I am currently programming a simple C++ application. I downloaded some source code from a website, but i don''t understand the a class declaration.
Here''s the code ;
class myClass
{
public:
myClass(int sub) : main(sub) {} //what's up with this line ?
int main;
int DoCalc();
};
请,需要帮助!
Please, Need help !
推荐答案
这些东西称为初始化列表".阅读有关此文章:
http://www.cprogramming.com/tutorial/initialization-lists-c++.html [ ^ ]
你可以说
That stuff is called the "initialization list". Read this article about it:
http://www.cprogramming.com/tutorial/initialization-lists-c++.html[^]
You could just say
main = sub;
如果是简单的int成员,则在构造函数主体中使用而不是在初始化程序列表中,但有时只有初始化程序列表才能完成此工作.例如,当您有一个基类或一个成员变量,其类型是没有默认构造函数的类时.对于属于您的班级成员的引用,也是如此.阅读链接文章,其中包含示例代码.
in the constructor body instead of the initializer list in case of a simple int member but sometimes only initializer lists can do the job. For example when you have a base class or a member variable whose type is a class that doesn''t have a default constructor. Same is true for a reference that is the member of your class. Read the linked article that has example code for these.
这基本上意味着,当使用该构造函数创建myClass的新成员时,变量main将获得值sub. 注意:您必须小心这些事情.它类似于调用基类的构造函数
It basically means that the variable main will get the value sub when the new member of myClass is created with that constructor
Note : you must be careful with these things . It resembles calling the constructor of base class
这篇关于类声明问题.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!