简体   繁体   English

C ++-可以在标头之外声明类函数吗?

[英]C++ - Can Class Functions be Declared Outside of Header?

I'm fairly new to C++ and trying to write a class that's purpose is to solve a mathematical problem when the class is instantiated with an input. 我是C ++的新手,尝试编写一个类的目的是在使用输入实例化该类时解决数学问题。 I've broken those calculations up over a couple of sub-functions and so the header/source code looks something like this: 我已经将这些计算分解为几个子功能,因此标头/源代码如下所示:

mySolver.h mySolver.h

#ifndef __mySolver_h__

class mySolver
{
private:
    double mInput, mOutput; //member variables
    double intermediateCalculation1(double input);
    double intermediateCalculation2(double intermediateValue1);
public:
    mySolver(double input);
};

#endif

mySolver.cpp mySolver.cpp

#include "mySolver.h"

mySolver::mySolver(double input)
{
    mInput = input;

    double intermediateValue1 = intermediateCalculation1(mInput);
    double mOutput = intermediateCalculation2(intermediateValue1);

    cout << "Output is: " << mOutput << endl;
 }
double mySolver::intermediateCalculation1(double input)
{
    //do stuff
}

double mySolver::intermediateCaluclation2(double intermediateValue1)
{
    //do stuff while accessing value of member variable mInput
}

This works but I have to list the methods intermediateCalculation1/2 in the header file despite their being purely implementation details. 这可行, 但是我必须在头文件中列出方法intermediateCalculation1 / 2,尽管它们只是实现细节。 As a result, if I want to change some of the details of how the calculations are performed (eg split things into 3 intermediate calculations rather than 2) I would have to change the header file and re-compile every file that includes mySolver.h which seems to defeat the purpose of separating interface from implementation. 结果,如果我想更改一些计算方式的详细信息(例如,将内容分成3个中间计算而不是2个),则必须更改头文件并重新编译每个包含mySolver.h的文件。这似乎破坏了将接口与实现分开的目的。

My question is: 我的问题是:

1) Is there a simple way to do this without having to include the intermediate functions in the header file? 1)是否有一种简单的方法,而不必在头文件中包含中间函数?

2) Is there a simple way to do this without having to include the intermediate functions in the header file that still lets me access member variables in the intermediate functions ? 2)有没有一种简单的方法,而不必在头文件包含中间函数,仍然允许我访问中间函数中的成员变量

I've come across some references to the pImpl technique that could be a solution, but for my purposes that seems unnecessarily complicated. 我遇到过一些有关pImpl技术的引用,它们可能是一个解决方案,但对我而言,这似乎不必要地复杂。

EDIT: Regarding why this is in a class at all, I've simplified my example for clarity but in my actual code I have multiple outputs, some of which are intermediate results I only want to access some of the time (ie for error checking), hence my choice of class structure. 编辑:关于为什么这根本不在一个类中,为了清楚起见,我简化了我的示例,但是在我的实际代码中,我有多个输出,其中一些是中间结果,我只想访问某些时间(即,用于错误检查) ),因此我选择了类结构。

1) Is there a simple way to do this without having to include the intermediate functions in the header file? 1)是否有一种简单的方法,而不必在头文件中包含中间函数?

Yes. 是。 In addition to this answer , if you put these into an anonymous (unnamed) namespaces you will really hide those implementation details from any access outside of the translation units. 除了此答案之外 ,如果将它们放入匿名(未命名)的名称空间中,您将真的在翻译单元之外的任何访问中隐藏那些实现细节。 It also helps not to confuse the linker in case of name clashes: 它还有助于避免名称冲突时混淆链接器:

namespace { // <<<< unnamed namespace
    double intermediateCalculation1(double input) {
        //do stuff
    }

    double intermediateCaluclation2(double intermediateValue1, double input) {
        // the member variable mInput should be passed as parameter
    }
}

mySolver::mySolver(double input) {
    mInput = input;

    double intermediateValue1 = intermediateCalculation1(mInput);
    mOutput = intermediateCalculation2(intermediateValue1,mInput);

    cout << "Output is: " << mOutput << endl;
}

2) Is there a simple way to do this without having to include the intermediate functions in the header file that still lets me access member variables in the intermediate functions? 2)是否有一种简单的方法可以不必在头文件中包含中间函数,而仍然允许我访问中间函数中的成员变量?

Not really, these would need to be friend ed to do so, which requires them to appear at the class declaration. 并非如此,需要将它们作为friend来这样做,这要求它们出现在类声明中。

You still have the option as your code example does now and pass them the necessary parameters, or go with the pimpl idiom as you mentioned. 您仍然可以选择,就像您的代码示例现在一样,并为他们传递必要的参数,或者使用您提到的pimpl习惯用法。 Since pimpl only needs a forward declaration, I believe you can do the full declaration and definition in an anonymous namespace as well. 由于pimpl只需要一个前向声明,我相信您也可以在匿名名称空间中进行完整的声明和定义。

You can do that by just defining the function before you use them in the .cpp file. 您可以通过在.cpp文件中使用函数之前定义函数来实现。 No need to define them in the header file because there is no need to make them member functions. 无需在头文件中定义它们,因为不需要使其成为成员函数。 These are just free functions. 这些只是免费功能。

Also, you probably would want to put them in an anonymous namespace so they are only visible to the existing file. 另外,您可能希望将它们放在匿名名称空间中,以便它们仅对现有文件可见。

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

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