简体   繁体   English

如何在 class 中添加 function 而不是在 C++ 中的 header 文件中?

[英]how to add function in class but not in header file in C++?

I am currently practicing writing classes and header files in C++. I have a question: let's say that in my header file I have a public function that a client can use, and I know how to implement it in the respective class. However, let's say that this function is divided into several steps that can be written as independent functions that I don't want the user to see (protect intellectual property).我目前正在C++中练习编写类和header文件。我有一个问题:假设在我的header文件中我有一个客户端可以使用的公共function,我知道如何在相应的class中实现它。但是,让我们说这个function分为几个步骤可以写成我不希望用户看到的独立函数(保护知识产权)。 Normally, for each defined function in the header file, I would write in myClassName::myFunctionName(parameter 1..) in the.cpp file.通常,对于 header 文件中定义的每个 function,我会在 .cpp 文件中写入 myClassName::myFunctionName(parameter 1..)。 Is there a way to define and use functions only in.cpp file?有没有办法只在.cpp 文件中定义和使用函数? For example, I wrote a program to see if two words are anagrams (have the same letters).例如,我编写了一个程序来查看两个单词是否是变位词(具有相同的字母)。

My header file is:我的 header 文件是:

#ifndef _Anagrams_h
#define _Anagrams_h
#include <string>
using namespace std;

class Anagrams{
    public:
        Anagrams(string &s);
        static bool areTwoWordsAnagrams(string s1, string s2) ; 
        string getWord()const; 
        void setWord(string &s);

    private:
        string word;

};
#endif

My class is:我的 class 是:

#include "Anagrams.h"
#include <string>
using namespace std;

Anagrams::Anagrams(string &s){
    word = s;
}

bool Anagrams::areTwoWordsAnagrams(string word1, string word2){
    int sizeOfWord1 = word1.size();
    int sizeOfWord2 = word2.size();

    int array1[26];
    int array2[26];

    for (int i = 0; i < 26; i++){ //Initialize both arrays
        array1[i] = 0;
        array2[i] = 0;
    }


    decomposeWordIntoLetters(word1,array1,sizeOfWord1);
    decomposeWordIntoLetters(word2,array2,sizeOfWord2);

    return true;
}

string Anagrams::getWord() const{
    return word;
}

void Anagrams::setWord(string &s){
    word = s;
}

void decomposeWordIntoLetters(string word, int array[], int size){
    for (int i = 0; i < size; i++){
        char letter = word[i];
        array['z' - letter]++;
    }
}

Notice that the decomposeWordIntoLetters function is not defined in the header file.请注意,header 文件中未定义 decomposeWordIntoLetters function。 If I copy and paste the code twice in Anagrams::areTwoAnagrams(string word1, string word2), the program works.如果我在 Anagrams::areTwoAnagrams(string word1, string word2) 中复制并粘贴代码两次,程序就可以运行。 Otherwise, I get the following error:否则,我会收到以下错误:

Anagrams.cpp: In static member function ‘static bool Anagrams::areTwoWordsAnagrams(std::string, std::string)’:
Anagrams.cpp:22: error: ‘decomposeWordIntoLetters’ was not declared in this scope

Any help would be greatly appreciated.任何帮助将不胜感激。 Thank you.谢谢你。

You can definitely have non-member functions in your cpp file. 您绝对可以在cpp文件中具有非成员函数。 However, these functions may not be used before they declared or defined. 但是,在声明或定义这些函数之前,不得使用它们。

To declare a function, provide its prototype, like this: 要声明一个函数,请提供其原型,如下所示:

void decomposeWordIntoLetters(string word, int array[], int size);

Put this line above the member function that calls decomposeWordIntoLetters . 将此行放在调用decomposeWordIntoLetters的成员函数上方。 This should fix the compile problem that you are seeing. 这应该可以解决您看到的编译问题。

When you define functions like that, you may want to hide them not only from the header, but also from other modules that link to your library. 当定义这样的函数时,您可能不仅要对标头隐藏它们,还要对链接到库的其他模块隐藏它们。 In order to do that, declare the function static : 为此,将函数声明为static

static void decomposeWordIntoLetters(string word, int array[], int size);

Note that when you do that to a free-standing function, the meaning of static is completely different: the function does not become a class function as class-scoped static functions; 请注意,对独立函数执行此操作时, static的含义完全不同:该函数不会像类作用域中的static函数一样成为函数; instead, it becomes a function with the visibility limited to the translation unit (ie a single cpp file where it is defined). 取而代之的是,它变成一种功能,其可见性仅限于转换单元(即定义了单个cpp文件)。

There is no way to have class member functions which do not appear in the class declaration, and so if the declaration of a class is visible to the code which uses it, all of the member functions are visible. 没有办法使类成员函数不会出现在类声明中,因此,如果类的声明对于使用它的代码可见,则所有成员函数都是可见的。

There are ways to hide the implementations of entire classes, like the "pointer to implementation" pattern: you expose a class which is just an interface. 有很多方法可以隐藏整个类的实现,例如“实现的指针”模式:公开一个仅是接口的类。 It holds a pointer to some opaque object (whose full type declaration is not exposed to the user), and all of the public functions are just wrappers which call functions on that object. 它拥有指向一些不透明对象的指针(不向用户公开其完整类型声明),并且所有公共函数只是包装器,它们在该对象上调用函数。

class hidden_from_user;

class exposed_to_user {
private:
  hidden_from_user *impl;
public:
  // constructors, destructors, and so on.
  void frob(int howmuch);   
};

Inside the CPP file: 在CPP文件中:

// full declaration of hidden_from_user is available here
#include "hidden_from_user.h"  // private header, not shipped as part of API
#include "exposed_to_user.h"

// ...

void exposed_to_user::frob(int howmuch)
{
   impl->frob(howmuch);
}

You have a lot of freedom to change the internals of hidden_from_user . 您有很大的自由来更改hidden_from_user的内部。 Since the client code never defines instances of that type ( you are managing the memory), it is possible to change the layout of the object between releases of the code, even while the API remains forward and backward compatible at the binary level. 由于客户端代码永远不会定义该类型的实例(您正在管理内存),因此即使API在二进制级别保持向前和向后兼容,也可以在代码的发行之间更改对象的布局。

Declare function decomposeWordIntoLetters(word1,array1,sizeOfWord1);声明 function decomposeWordIntoLetters(word1,array1,sizeOfWord1); outside class in.h file在 class in.h 文件之外

Just the prototype void decomposeWordIntoLetters(string, int[], int );只是原型 void decomposeWordIntoLetters(string, int[], int );

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

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