2

Hello to all that read

I am self learning C++ from a text book:

A Question in the textbook asks me to make a function a friend of a class and therefore the friend function can have access to all of the classes members; which is fine I can do this. Problem is that the question then goes on to ask that the friend function can only read the class members (private members) but NOT write to them!

**Please note: I have looked at similar question/answers on 'stackoverflow' - but I could not find the relevant and simple straight forward answer that I am after.

Here is some relevant code that I have, any help would be appreciated:

Many thanks in advance

#include <iostream>
#include <cstdio>

using namespace std;

class classroom{

  private:
         char name[25];
         int student_id;
         float grades[10];
         float average;
         int num_tests;
         float letter_grade;
         static int last_student_id;
  public:       
         void enter_name_id(void);
         void enter_grade(void);
         void average_grades(void);
         void letter_grades(void);
         void output_name_id_grade(void);
         void last_id(void);
         classroom();


  friend void readers(classroom&);

  };

      int classroom::last_student_id=1;



   void readers(classroom& lee){ 

      cout<<"\n number tests: "<<lee.num_tests;//friend function is reading class member -This is O.K!

      lee.num_tests=15;//friend function is writing to class member - We don't want this to be allowed

      cout<<"\n number tests: "<<lee.num_tests;//Used to test that class members are NOT accessed!             



   } 

and in the main program we have:

int main()
{       

        classroom students[10];
        readers(students[0]);

//and so on...
Mr_leighman
  • 313
  • 1
  • 6
  • 22

2 Answers2

5

A function which is a 'friend of a Class' that is allowed to have 'read access' to its 'private members' but NOT 'write access'?

Once you declare a function as friend function, the access specifier rules are off, but you can prevent writing to members by passing in a const object to the friend function.

friend void readers(const classroom&);
                    ^^^^^

Note that access specifiers and constness are two different attributes, do not mix them.

Note that your friend function can still try to modify the object by casting away the constness of the object by using const_cast but that would result in Undefined Behavior as per the Standard.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • By *do not mix them* I intended to mean *do not confuse them*. – Alok Save Nov 15 '11 at 19:04
  • O.k, thanks, I will investigate and come back on this one in the mean time have some reputation – Mr_leighman Nov 15 '11 at 19:10
  • I am a bit confused, The Problem I see: If you are restricting access in the function i.e (const classroom&) - as you stated!, then won't it still allow the function to control the access! - But what is needed is for only class structure to have access control. Or am I interpreting your code wrongly? – Mr_leighman Nov 15 '11 at 19:15
  • @Mr_leighman: `const` does not restrict Access! It restricts Modification of the object. Access is already bypassed once you declared the function friend of the class.The const here means the function will not modify the object. – Alok Save Nov 15 '11 at 19:18
  • O I see now: you also declare the constant in the class it's self so then the class DOES have the access control – Mr_leighman Nov 15 '11 at 19:20
1

You'll want to read up on "const-ness", including constant variables, parameters, and member functions.

Specifically, you can specify that the friend function receive it's parameter as a const reference. The it will only be able to read private variables and call const functions.

// ...
friend void readers(classroom const &);
// ...

void readers(classroom const &lee) {
    // ...
}
SoapBox
  • 20,457
  • 3
  • 51
  • 87