Prevent friend function from accessing private member of a class - c++

How can we prevent friend function from accessing private member of a class. Can we do this at all?
This question was asked in an interview and he was confident that it can be done, he gave hint about functor / function object. So far I can't think of anything. I am excited about the answer, if any.

How can we prevent friend function from accessing private member of a class. Can we do this at all?
No, you cannot.
As soon something is declared as friend of your class the doors to access any private members are opened.

The idea of Encapsulation is to bundle data and methods (that work on the data) together and restrict access of private data members outside the class. In C++, a friend function or friend class can also access private data members.

Related

Inheritance/Polymorphism - Am I forced to use "protected" variables?

I am currently working with Big C++ 2nd Edition, using Code::Blocks 17.12, on the chapter for inheritance
The book introduces the protected variable type for cases in which you want to allow a derived class to access said variable.
The book also forewarns that protected elements suffer from some of the pitfalls of a public variable: in the worst cases derived class members can corrupt the base class data
The book demos the use of a protected member in a clock program in a dedicated section in which they introduce this, but in the final code they ultimately went with setting the variable to private and then using some get_ helper functions to access this private data. This combo of private and using helper functions always returned errors in my IDE and I wasn't able to pull that off
Take this simple example I created for P8.1, a employee record for a programmer, with an employee base class and programmer derived class. I created the following ctor, with the variables name and sal set to protected status in the base class
Programmer::Programmer(string p_name, double p_sal)
:Employee(get_name(), get_sal())
{
name=p_name;
sal=p_sal;
}
With this code, the program works perfectly.
Based on the textbook, if the variables name and sal were set to private status in the base, then I should be able to execute the code also, granted that I am using a get_ helper function I created to accessed the data.
Can anyone explain what the issue is here? Should I be ok with using protected or is there truly a way to keep all of my variables private for classes?
I also found this on https://learn.microsoft.com/en-us/cpp/cpp/protected-cpp?view=vs-2019
Protected members that are also declared as static are accessible to any friend or member function of a derived class. Protected members that are not declared as static are accessible to friends and member functions in a derived class only through a pointer to, reference to, or object of the derived class.
I have not covered static so far, So I ultimately tried a bunch of different combinations with pointers and references, none of which worked either.
I am trying to understand when to use protected vs when to use private basically and the book isn't being clear on that. Any ideas?
For some reason I thought when calling the base class constructor you needed to provide helper functions to access the private data.
Since the base ctor is already public, and by feeding it the parameters for the derived class ctor it will construct the derived class obj properly, allowing get_ func's to read its private variables
Programmer::Programmer(string p_name, double p_sal)
:Employee(p_name, p_sal)
{}
I wasn't understanding this in the book, thank you all for helping clarify

Which access specifiers will not affect a friend function?

Which access specifiers will not affect the friend function?
private and protected members of a class cannot be accessed from outside, private and protected member can be accessed anywhere or both
None of the access specifies affect a friend.
When you declare a function/method or class as a friend you are making it part of the public interface to your class (thus implying a tight cupping).
A friend is actually able to see all the members of your class (you could think of it as a member function but without the implied this parameter). As such it is actually tightly coupled to your class. Any change in the internal representation of your class is going to be reflect in changes to the implementation of all public interface members.

Using a class within it's own declaration

ok, so my problem is this. I need to copy a custom made list and the function has to be a private member of my list-class. looks like this atm:
private:
struct List_Node* head_;
List* copy(List* list);
looks like crap i know, but i have been told to do it that way.
getting the compilation error:
error: `List* List::copy(List*)' is private
is there some way to go around this problem or am i understanding my directions wrong?
You need to call the function from within another member function which is public.
You cannot call private member functions from outside the class. The error suggests you are doing that.
If you are calling the List::copy from within member functions of List (as the title indicates), you should have no problem.
If you want to call it from outside the class, you will not get access to the private member functions (e.g. copy) unless you declare the caller function a friend of the class
Try to use copy-constructor or operator = overloading.
I think you are told to do so because the users of this class are not supposed to copy the list directly. The list node is also declared as a private structure, which is inaccessible to outside functions. The construction and destruction of the list node should be handled by member methods of this class.
May be because of these reasons, the copy method should be protected, in this case, declared as private.
You can call the private copy method in any member methods of this class, including public methods.
Although it is unlikely to happen, you can simply declare a public member method only calling this private method, which expose the private method to outside functions. However, it will be non-sense to declare the copy method private in the first place.

In C++, does adding a friend to a class change its memory layout?

Also, does it matter where in the class you declare the friend ?
Does it matter if you add a friend class or a friend function ?
No it doesn't. It's a purely compile-time thing: similar to access modifiers themselves.
Despite the fact that you write the declaration inside the class, you don't really add a friend to a class. You'd basically declare something else as a friend of the class and simply allow it to access the class's private members, as if they were public.
As mentioned already, it is purely a compile-time mechanism.

Why defining private members below public members in C++?

In C++ sometimes in class definition public members are declared at first and privates later. But the variables or data members are normally private and used by the public methods. So, in this case variables are used but not even declared yet. Thus the code become difficult to understand. But I found renowned programmers, sites or books to declare the private members later. Does anybody knows what is the reason?
I do things that way round since users of my class don't care about the private members, they're interested in the public API (i.e. how to use my class).
Also, in header files I'm generally just declaring member functions, rather than defining them, so I'm not accessing any private members anyway.
We read text from top to bottom, so the most relevant information should be at the top. In a class definition, that's the public interface.
Private members and implementation should be hidden from the header file. Putting the private member definitions at the bottom is a quick way to do so. Perhaps it is better to use the Pimpl idiom and hide the private part of your class in an internal struct.
Normally private members don't matter. If I'm looking at a class to determine how to use it somewhere else in code I don't care about it's internals so put private members at the bottom since i don't need to know about them. If I"m modifying a class then I'll take the time to find the private members and know they'll be at the bottom instead of having to skim the entire class.
We are like opposites: My Question
My reasoning is that when you are becoming familiar with a class it is more beneficial to first get introduced to the public interface then go deeper into the private members when you need to. If you start by looking at the private members, you have no context how they are used.
Agreed. Private members should be declared at bottom. The only good reason to declare private members first that i found, is when a function needs to get or return a custom data type, like: vector . The compiler will ask you about what kind of data is that.
But even, in that case i preffer to do:
{
private: /// advicing ofc theres more private below!
earlier declare just of the type
public:
private:
};