[英]How to handle a C++ header file with a extern call to C function and a class object
[英]how to add function in class but not in header file in C++?
我目前正在C++中練習編寫類和header文件。我有一個問題:假設在我的header文件中我有一個客戶端可以使用的公共function,我知道如何在相應的class中實現它。但是,讓我們說這個function分為幾個步驟可以寫成我不希望用戶看到的獨立函數(保護知識產權)。 通常,對於 header 文件中定義的每個 function,我會在 .cpp 文件中寫入 myClassName::myFunctionName(parameter 1..)。 有沒有辦法只在.cpp 文件中定義和使用函數? 例如,我編寫了一個程序來查看兩個單詞是否是變位詞(具有相同的字母)。
我的 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
我的 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]++;
}
}
請注意,header 文件中未定義 decomposeWordIntoLetters function。 如果我在 Anagrams::areTwoAnagrams(string word1, string word2) 中復制並粘貼代碼兩次,程序就可以運行。 否則,我會收到以下錯誤:
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
任何幫助將不勝感激。 謝謝你。
您絕對可以在cpp文件中具有非成員函數。 但是,在聲明或定義這些函數之前,不得使用它們。
要聲明一個函數,請提供其原型,如下所示:
void decomposeWordIntoLetters(string word, int array[], int size);
將此行放在調用decomposeWordIntoLetters
的成員函數上方。 這應該可以解決您看到的編譯問題。
當定義這樣的函數時,您可能不僅要對標頭隱藏它們,還要對鏈接到庫的其他模塊隱藏它們。 為此,將函數聲明為static
:
static void decomposeWordIntoLetters(string word, int array[], int size);
請注意,對獨立函數執行此操作時, static
的含義完全不同:該函數不會像類作用域中的static
函數一樣成為類函數; 取而代之的是,它變成一種功能,其可見性僅限於轉換單元(即定義了單個cpp文件)。
沒有辦法使類成員函數不會出現在類聲明中,因此,如果類的聲明對於使用它的代碼可見,則所有成員函數都是可見的。
有很多方法可以隱藏整個類的實現,例如“實現的指針”模式:公開一個僅是接口的類。 它擁有指向一些不透明對象的指針(不向用戶公開其完整類型聲明),並且所有公共函數只是包裝器,它們在該對象上調用函數。
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在二進制級別保持向前和向后兼容,也可以在代碼的發行之間更改對象的布局。
聲明 function decomposeWordIntoLetters(word1,array1,sizeOfWord1); 在 class in.h 文件之外
只是原型 void decomposeWordIntoLetters(string, int[], int );
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.