简体   繁体   English

我是否必须在类的头文件中提及私有方法?

[英]Do I have to mention private methods in the header file of a class?

For now I do not use header files at all. 现在我根本不使用头文件。 Classes are each in a single .cpp file completely. 类完全位于单个.cpp文件中。 But to save compile time I want to make use of header files now. 但是为了节省编译时间,我现在想要使用头文件。 My hope is that Visual Studio won't compile classes which weren't modified for debug builds then. 我希望Visual Studio不会编译那些未针对调试版本进行修改的类。

Is there a way to mention only public methods and members in the header file. 有没有办法在头文件中仅提及公共方法和成员。 In theory that would be enough information for the compiler. 从理论上讲,这将是编译器的足够信息。 If another file, say main.cpp includes the class header there is no need for private methods and members, is it? 如果另一个文件,比如main.cpp包含类头,则不需要私有方法和成员,是吗?

How can I use header files without retyping the names of private methods and members? 如何在不重新输入私有方法和成员名称的情况下使用头文件? The reasons for me to want so is coding productivity. 我想要的原因是编码效率。 When I want do add a small helper function to the class used by another method, I don't want to have to also add it's signature to the header file. 当我想要为另一个方法使用的类添加一个小帮助函数时,我不想也必须将它的签名添加到头文件中。

If another file, say main.cpp includes the class header there is no need for private methods and members, is it? 如果另一个文件,比如main.cpp包含类头,则不需要私有方法和成员,是吗?

No, public methods and members aren't necessarily enough. 不, public方法和成员不一定足够。 For example, if another .cpp file were to try and create an instance of your class: 例如,如果另一个.cpp文件尝试创建您的类的实例:

SomeClass instance;

the compiler will need to know, among other things, how much memory to allocate for SomeClass . 除了其他方面,编译器需要知道为SomeClass分配多少内存。 For that it requires full knowledge of SomeClass 's private data members. 为此,它需要完全了解SomeClass的私有数据成员。

The way you are framing the question makes it sound as if you were intent on fighting the language. 你构建问题的方式让人觉得你好像打算用语言来对抗。 I don't think that's a good way to go about it. 我认为这不是一个很好的方法。 I think the best way is to do things the way things are usually done in the language of your choice, and depart from that only when there is a specific, clearly understood need. 我认为最好的方法是按照您选择的语言以通常的方式做事情,并且只有在有明确理解的特定需求时才会离开。

The way things are usually done in C++ is that the entire class declaration goes in the header file, and the definition is in some way split between the header file and the corresponding .cpp file. 通常在C ++中完成的方式是整个类声明都在头文件中,并且定义在某种程度上分割在头文件和相应的.cpp文件之间。 The exact split is determined by various technical considerations. 确切的划分由各种技术考虑决定。 For example, templates and inline functions normally have to appear in the header file. 例如,模板和内联函数通常必须出现在头文件中。 On the other hand, placing code in header files increases dependencies and potentially build times. 另一方面,将代码放在头文件中会增加依赖性并可能增加构建时间。

There are ways to address these issues. 有办法解决这些问题。 However, since this involves extra complexity, I'd argue that this should only be done if there is a clearly identifiable need. 然而,由于这涉及额外的复杂性,我认为只有在有明确可识别的需求时才应该这样做。

All function declarations should go in header files and all function definitions should go in cpp files. 所有函数声明都应该放在头文件中,所有函数定义都应该放在cpp文件中。 It's not good coding practice to put declarations inside the cpp files. 将声明放在cpp文件中是不好的编码实践。

You could put definitions inside headers though, when you write templates or inline functions. 当您编写模板或内联函数时,您可以将定义放在标题内。

Once you declare the class in the header file, you have to declare all its methods and members inside the class' declaration in the header, given that your class is no longer declared in the cpp file . 一旦在头文件中声明了类,就必须在头中的类'声明中声明它的所有方法和成员,因为你的类不再在cpp文件中声明了

I don't know of a way to do what you're asking, but there is another way to give some isolation. 我不知道如何做你要问的方法,但还有另一种方法可以让你有些孤立。 You might want to take a look at the pimpl idiom as it offers isolation about private information. 您可能想看一下pimpl习语,因为它提供了有关私人信息的隔离。 It's a little bit of extra work, but it can be extremely useful, especially in large projects. 这是一些额外的工作,但它非常有用,特别是在大型项目中。

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

相关问题 定义和使用不在头文件中的私有方法 - Define and use private methods not in header file 避免在头文件中包含私有方法所需的内容 - Avoid includes required by private methods in header file 我是否必须在每个头文件上输入typedef? - Do I have to typedef on every Header file? 为什么我们必须在对象地址前面提到数据类型才能访问C ++中类的私有成员? - Why we have to mention data type in front of address of the object to access the private members of a class in C++? 头文件中类的私有成员的链接器错误 - Linker errors with private members of class in header file 私有继承:如何创建基类的对象(具有纯虚方法)? - Private Inheritance: How do I make object of the Base Class ( which has got pure virtual methods)? 使用外部库时是否必须包含 header 文件? - Do i have to include a header file when using an external library? 我是否需要在类头C ++中定义所有私有函数和变量 - Do I need to define all private functions and variables in class header C++ 我如何获得一个类成员函数来访问另一个类成员函数的私有成员? - How do i get a class member function to have access to another class member function's private member? 如何从头文件中访问班级的私人成员? - How Can I Access The Private Member Of A Class From Within a Header File?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM