[CPP] static const and friend

This pointer

  • For functions to actually do something to an object, they need to know which object to affect, and the implicit extra parameter saying which object to act on is this.
  • enter image description here

static

We have talked about static variables(local and global) before, now we are going to talk about static members and methods.
  • Same as static in Java, static members are shared between all objects of that class
  • static member functions do not have a this pointer
  • both static and non-static member data and functions are class members, they all have access to private members

Declaration and definition of the static member function

Declaration: usually in .h file,
e.g., static void foo();
Definition: usually in .cpp file, no static keyword in cpp file
e.g., void MyClass::foo(){xxx;}

const

  • const means the variables cannot be changed
  • #define could have same effect, but using text replacement in preprocessor
  • const is nicer for declaring constants

pointers and const

Normal const keyword is easy to understand, but pointer to const or const pointer is hard to identify.
When const is to the left of the *, it means the pointer points to a const data!
When const is to the right of the *, it means the pointer itself cannot be changed!

how to remember it?

Read backwards with * meaning ‘pointer to’
enter image description here

const references/ pointers to objects

  • const references make the thing referred to const
  • You cannot make a reference refer to something else anyway, so const always means the thing referred to
  • const references are useful for parameters
  • Passing by value(not reference) means the original variable cannot be accidentally modified(maybe safer)
  • Passing a reference means that no copy is made(maybe quicker, copying objects can be slow)
  • Using a const reference means no copy needs to be made, but the original can still no be changed, like a copy but faster.
Q: If you have a const reference (or pointer)to an object, then which methods can you call using the reference (or pointer)?
A: Only methods which guarantee not to change the object (i.e. accessors)
enter image description here

mutable

enter image description here
Here, i have a const member function of DemoClass, and the i is in this function, which means i can not be change in this function, so it is not allowed
  • The compiler will not allow you to alter any member data from a member function declared as const
  • If you need to alter a specific variable within a const member function, you can declare that variable mutable
enter image description here
But if you declare this class as const, it means you cannot call non-const function like this,
enter image description here

const member data

const member data must be initialized in the initialization list for the constructor
Cannot just be set in constructor body, since construction has occurred by then
enter image description here

Friends

  • Members can be private or public
  • usually only class members can access private data
  • classes can grant access to their private member data and functions to their friends
  • The friends of a class are treated as class members for access purposes – although they are not members

Friend function

enter image description here
The function FriendFunc() is defined outside the Friendly class, and it can use friend keyword to declare the function and use the private member variables of Friendly Class inside the friend function

Friend class

enter image description here

评论

此博客中的热门博文

[MLE] Linear Classification

[AIM] MetaHeuristics

[CS231] Neural Networks