9

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). Normally, for each defined function in the header file, I would write in myClassName::myFunctionName(parameter 1 ..) in the .cpp file. Is there a way to define and use functions only in .cpp file? For example, I wrote a program to see if two words are anagrams (have the same letters).

My header file is:

#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:

#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. If I copy and paste the code twice in Anagrams::areTwoAnagrams(string word1, string word2), the program works. 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.

4

2 回答 2

13

您的 cpp 文件中绝对可以有非成员函数。但是,这些函数在声明或定义之前不得使用。

要声明一个函数,请提供其原型,如下所示:

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

将此行放在调用decomposeWordIntoLetters. 这应该可以解决您看到的编译问题。

当您定义这样的函数时,您可能不仅要从标题中隐藏它们,还要从链接到您的库的其他模块中隐藏它们。为此,请声明函数static

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

请注意,当您对独立函数执行此操作时, 的含义static完全不同:该函数不会像类作用域函数那样成为static函数;相反,它变成了一个可见性仅限于翻译单元的函数(即定义它的单个 cpp 文件)。

于 2013-06-14T01:29:14.290 回答
3

没有办法让类成员函数不出现在类声明中,因此如果类的声明对使用它的代码可见,则所有成员函数都是可见的。

有一些方法可以隐藏整个类的实现,例如“指向实现”模式:您公开一个只是一个接口的类。它拥有一个指向某个不透明对象的指针(其完整的类型声明不向用户公开),并且所有公共函数都只是调用该对象上的函数的包装器。

class hidden_from_user;

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

在 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);
}

你有很大的自由来改变hidden_from_user. 由于客户端代码从不定义该类型的实例(您正在管理内存),因此可以在代码版本之间更改对象的布局,即使 API 在二进制级别保持向前和向后兼容。

于 2013-06-14T01:30:34.793 回答