• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 451
  • Last Modified:

Friend class? And Static Const?

I'm learning C++.  Can someone explain to me what the concept of a friend class is?  

Also, when I see code that says static const..what does this do?  Thanks.
  • 2
2 Solutions
evilrixSenior Software Engineer (Avast)Commented:
A friend class is a class that is allowed to access all the internals of the class it is friends with, this includes all the members that are declared private or protected. You can consider a friend class as an extension to a classes normal interface.

static const does different things depending on the context:

for a class data member it means the value of the member is immutable and only one instance of it exists that all class instances share.

for a class function it means only one instance of it exists that all class instances share.The const will refer to the return value, which be returned as const.

You can also make functions and data members static at file scope but this is deprecated in C++ and should be avoided in preference for unnamed namespace.
friend class
Shortly: data declared as private are visible from the class friends.

static const:

shaolinfunkAuthor Commented:
Ok, I got the friend class part.

But am a little confused on the static const.  I think my question needs to be parsed further into.

What does "static" mean/do (by itself)?

What does "const" mean/do (by itself)?

And what does "static const" mean/do (combined)?
const means that you are not going to change this variable.
static means that you can use it in the static class methods - the methods that you can call without creating a class instance.
For example
Class CTest:
  static const int x = 1;
    static int howMany() { return x; }

int main()
   int number = CTest::howMany();
   return 0;
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now