How to add a function to a class, but not to a header file in C ++?

I am currently 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 the client can use, and I know how to implement it in the corresponding class. However, let's say that this function is divided into several stages, which can be written as independent functions that I do not want to see by the user (protect intellectual property). Usually for each specific function in the header file I have to write in myClassName :: myFunctionName (parameter 1 ..) in the .cpp file. Is there a way to define and use functions only in a .cpp file? For example, I wrote a program to see if two words are anagrams (have the same letters).

My header file:

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

 #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]++; } } 

Note that the decposeWordIntoLetters function is not defined in the header file. If I copy and paste the code twice into 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. Thanks.

+4
source share
2 answers

In the cpp file, you can definitely have functions other than members. However, these functions cannot be used until they are declared or defined.

To declare a function, specify its prototype, for example:

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

Place this line above the member function that calls decomposeWordIntoLetters . This should fix the compilation problem you see.

When you define such functions, you can hide them not only from the header, but also from other modules that reference your library. To do this, declare a static function:

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

Note that when you do this for a stand-alone function, the static value is completely different: the function does not become a class function as a function of a static class; instead, it becomes a function with visibility limited to a translation unit (i.e. one cpp file where it is defined).

+8
source

There is no way to have member functions of a class that are not displayed in the class declaration, and therefore, if the class declaration is visible to the code that uses it, all member functions are visible.

There are ways to hide implementations of entire classes, for example, the "pointer to implementation" template: expose a class that is just an interface. It contains a pointer to some opaque object (the full declaration of which is not displayed to the user), and all public functions are just shells that call functions on this 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:

 // 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 hidden_from_user internal elements. Since client code never defines instances of this type (you manage memory), you can change the layout of the object between versions of the code, even if the API remains in the opposite direction and backward compatible at the binary level.

+2
source

Source: https://habr.com/ru/post/1486144/


All Articles