C and C++ static class and this pointer details and example code

  • 2020-05-12 02:56:09
  • OfStack

C/C++ static class and this pointer details

1. Static class

The static members of C++ can be accessed not only through objects, but also directly through the class name.


class CBook{
public:
  static double price;// You need to initialize from outside the class 
}

int main(void){

  CBook book;
  book.price;// Access by object 
  CBook::price// Access by class name 

  return 0;
}

Static member variable

Corresponding static members have the following points to note:

(1) a static data member can be the type of the current class, while other data members can only be Pointers or application types of the current class.


class CBook{
public:
  static double price;
  CBook book;// Invalid definition. Objects belonging to a class are not allowed to be defined in this class 
  static CBook m_book;// correct 
  CBook *book;// correct 
};

(2) static data members can be used as default parameters of other member functions (different data types cannot be used as parameters).


class CBook{
public:
  int pages;
  static double price;

  // define 1 Function, with static data as the default parameter 
  void outPutInfo(int data = price){
    // implementation 
  }

  // Error definition. Normal data cannot be used as the default parameter 
  void outPutPage(int data = pages){
    // implementation 
  }
};

Static functions


static void outPut();

(1) the static member function of class can only access static data members, and cannot access normal data members (because there is no this pointer).

(2) the static member function cannot be defined as const member function (that is, the const keyword cannot be added to the end of the static member function)


static void outPut() const;// Error definition 

(3) when defining a static member function, if the implementation of the function is outside the class, the static keyword cannot be identified in the implementation part of the function.


// Wrong definition 
static void CBook::outPutInfo(){
  // implementation 
}

As for the static class, we summarize 1 below:

(1) the static member function of class is an object belonging to the whole class rather than the class, so it has no this pointer, which results in that it can only access the static data and static member functions of class.

(2) static member functions cannot be defined as virtual functions.

2, this

C++


class CBook{

public:
  int pages;
  void outputPages(){
    cout<<pages<<endl;
  }
};

int main(){
  CBook book1,book2;
  book1.pages = 10;
  book2.pages = 20;
  book1.outputPages();
  book2.outputPages();
  return 0;
}

Both book1 and book2 have their own data members, pages, and when outputPages is called, they both output their own member data. How can they be distinguished? The answer is the this pointer.

The member function (non-static member function) of each class implies an this pointer to the object being called, whose type is the pointer type of the current class.

So similar to the outputPages() method above, the compiler interprets it as:


// A function call 
book1.outputPages(&book1);

// function 
void outputPages(CBook *this){// The implicit this Pointer to the 
  cout<<this->pages<<endl;// use this A pointer accesses a data member 
}

Java


/**
 *  This example is for illustration purposes this the 3 Kind of usage! 
 */
package test;
public class ThisTest {
  private int i=0;

  ThisTest(int i){
    //(1)this.i Represents a member variable i . i Said parameters 
    this.i=i+1;
  }

  ThisTest(String s){
    System.out.println("String constructor: "+s);
  }

  ThisTest(int i,String s){
    //(2)this Call the first 2 A constructor 
    this(s);

    this.i=i++;
  }
  public ThisTest increment(){
    this.i++;
    //(3) Returns the current object 
    return this;
  }
  public static void main(String[] args){
    ThisTest tt0=new ThisTest(10);
    ThisTest tt1=new ThisTest("ok");
    ThisTest tt2=new ThisTest(20,"ok again!");

    System.out.println(tt0.increment().increment().increment().i);
  }
}

In addition, this cannot be used in the static method, which is why it is impossible to call a member variable in a static function.

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: