When would you use a friend class/function instead of inheritance? [duplicate] - c++

This question already has answers here:
When should you use 'friend' in C++?
(31 answers)
What's the difference between friendship and inheritance?
(4 answers)
Closed 5 years ago.
I am not exactly sure what the use of a friend function or class is when you can easily just use inheritance (parent/child classes)? From my understanding, a friend function or class allows a non-member function to access the members of another class that it is declared a friend of. Similarly, a child class can access the members of a parent class. I am wondering, in which situation would you pick one over the other?

Because you may want to use some private functions/member of a class without being a child of it....
You should read more about inheritance. You can't consider inheritance as a solution to access "members of a class" as you mentioned.

Related

How to define methods in templated abstract base class that will be used by the derived class? [duplicate]

This question already has answers here:
Why do I get "unresolved external symbol" errors when using templates? [duplicate]
(3 answers)
Why can templates only be implemented in the header file?
(17 answers)
Closed 4 years ago.
I am trying to create templated abstract base class. This class will have couple of pure virtual functions. It will also have two methods that are defined in it.
Then I derive from this templated abstract base class (I derive by specializing the template) and call those two methods that were defined in the templated abstract base class from the overloaded functions.
When I do this and try to compile I get undefined reference to. I am using cmake for building. Compilation works when I remove those two methods that were defined in base class.

Could we declare a pointer of the class's type in the class declaration? [duplicate]

This question already has answers here:
What are the differences between struct and class in C++?
(30 answers)
Closed 8 years ago.
I know that is possible for a struct:
struct A{
A* p;
};
But could we do that to a class:
class B{
B*p;
}
I tried to google it, but end not found it. Thank you very much!
Yes. There is no difference between a class declared with struct and one declared with class other than the default access for members and bases.
Yes, a class can contain pointers to a class of the same type, and the symbol representing the class can appear in signatures of functions or in the bodies of functions defined in that class. Furthermore, it should be noted that, in C++, struct and class are identical with only one difference -- the default access level for a struct is "public", while the default access level for a class is "private" (of course, one can trivially write "public:" or "private:" at the beginning of either in order to change the access level; it is only convention that results in "class" being used for types that include more functionality, encapsulation and "struct" for pure data objects).

Nested Classes C++ [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicates:
nested classes C++
nested class access control in C++
I'm a little confused as to what access a nested class has to the members of an enclosing class. What is the relationship it shares with the outer class?
The nested class does not have any special access to the enclosing class that would not be available to any other class - it can see public members, etc. The nesting only provides a useful scoping mechanism for nested class, but doesn't change its behavior or capabilities.
This might help you : nested class access control in C++. It is not exacly what you asked, but gives some interesting informations.

what is difference between structure and class [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
What are the differences between struct and class in C++
consider that i need to answer this question for interview..
in structure, the member access specifier are default to public while on class is private.
This is a standard interview question.
The standard (and correct) answer is "A struct's members are public by default while a class' members are private by default."
For bonus points, the full answer is "A struct's members are public by default and inheritance is public by default whereas a class's members and inheritance are private by default."

C++'s default inheritance access specifier? [duplicate]

This question already has answers here:
Default class inheritance access
(4 answers)
Closed 3 years ago.
I have some legacy code that I have to wrap, and I have come across this declaration:
class Foo : Bar
{
// ...
};
This seems to compile under GCC. I know it's bad, but I can't change it. My question is, if no inheritance access specifier is present, how does the C++ compiler handle it?
For classes, the default is private.
For structs, the default is public.
BTW, it is not called access modifier. It is called access specifier
$11.2/2 - "In the absence of an
access-specifier for a base class,
public is assumed when the derived
class is defined with the class-key
struct and private is assumed when the
class is defined with the class-key
class."
In your context, 'Bar' is a private base class of 'Foo'
It's private.