Why can't this friend access private variables?

class Student{ public: Student(int test) :key(705) { if(test == key) {cout << "A student is being verified with a correct key: "<< test << endl; allow=1; } else { cout << "Wrong key" ; } } friend void printResult(); private: const int key; int allow; }; void printResult() { if(allow==1) { cout<< " Maths: 75 \n Science:80 \n English: 75" << endl; } } int main() { int testkey; cout << "Enter key for Bob: "; cin >> testkey; Student bob(testkey); printResult(); } 

The printResult function cannot access the allow variable, which is private, from the Student class. Did I use the printResult prototype in the wrong place or was the syntax incorrect? AFAIK, we can prototype friends anywhere in the class.

+4
source share
5 answers

Here is the code that works:

 #include <iostream> class Student { public: Student(int test) : key(705) { if(test == key) { std::cout << "A student is being verified with a correct key: "<< test << std::endl; allow=1; } else { std::cout << "Wrong key" ; } } friend void printResult(Student* student); private: const int key; int allow; }; void printResult(Student* student) { if(student->allow==1) { std::cout<< " Maths: 75 \n Science:80 \n English: 75" << std::endl; } } int main(int argc, char* argv[]) { int testkey; std::cout << "Enter key for Bob: "; std::cin >> testkey; Student bob(testkey); printResult(&bob); } 

I changed it to keep the print function in global space (only based on how it looked the way you wanted). It accepts the Student * argument, and since it is declared as a friend, it will see the "allow" variable. This, however, is not the part of C ++ that you want to abuse. Be careful how you use a friend. Using it like this is dangerous in a larger code base. Global functionality is usually not suitable. Having a print function as an open member function in a student class is likely to be the best way to do something. Other answers provided code that shows this implementation. I decided to show you this implementation, because it seemed closer to what you were looking for in your question.

I also use 'std ::' when I refer to cout and endl. This eliminates the need to use 'namespace std;' on the top. This is just a good future programming practice for more complex projects.

+2
source

This is because allow belongs to an instance of the class, but the instance is not referenced.

You should printResult make printResult a member function of the class instead of making it an external function, or for the function to reference the Student instance so that you can access allow through instance.allow , where instance is a parameter of type const Student& .

+5
source

printResult not a member function, so you need to give a Student instance to act. for instance

 void printResult(const Student& s) { if(s.allow==1) { cout<< " Maths: 75 \n Science:80 \n English: 75" << endl; } } 

then

 Student student(1); printResult(student); 
+5
source

A friend's class function is allowed to access members of the class instance. You must pass an instance of the function, for example:

 void printResult(Student s) { if(s.allow==1) { cout<< " Maths: 75 \n Science:80 \n English: 75" << endl; } } 
+2
source
 class Student{ private: const int key; int allow; public: Student(int test) :key(705) { if(test == key) {cout << "A student is being verified with a correct key: "<< test << endl; int allow=1; } else { cout << "Wrong key" ; } } friend void printResult(); void printResult() //<--- printResult() is a member function, so keep it inside the class { if(allow==1) { cout<< " Maths: 75 \n Science:80 \n English: 75" << endl; } } }; int main() { int testkey; cout << "Enter key for Bob: "; cin >> testkey; Student bob(testkey); bob.printResult(); // <--- you need to specify the owner of printResult() } 
+2
source

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


All Articles