Use of Objects
1. static keyword
· For all objects of a particular type, it may sometimes be necessary to access a global variable.
For example, count the number of objects created for a certain type.
· If we use global variables to destroy data encapsulation, general user code can modify this
Global variables, then we can use static members of the class to solve this problem.
· Non-static data members exist in each object of the class type, and static data members are independent of that class.
Any object exists. It is an object associated with a class, not with a class object.
static members need to be initialized and defined outside the class definition:
Code example:
String.h:
#ifndef _STRING_H_
#define _STRING_H_
#include <iostream>
using namespace std;
class String
{
public:
static int MAX_LEN;
private:
};
#endif
String.cpp:
#include <iostream>
#include "String.h"
using namespace std;
int String::MAX_LEN = 1024;
Main.c:
#include <iostream>
#include "String.h"
using namespace std;
int main()
{
cout << String::MAX_LEN << endl;
return 0;
}
Operation results:
Static does not belong to a specific object, it can be shared as a member method.
In main.c, cout << sizeof (String) << endl; can be used to verify (self-verify)
Here, for example, a member method is used to initialize the static variable:
string.h:
#ifndef _STRING_H_
#define _STRING_H_
#include <iostream>
using namespace std;
class String
{
public:
//static int MAX_LEN;
String();
String(char * str);
~String();
String(const String& other);
String& operator=(const String& other);
void Display();
void setMAXLEN(int maxlen);
int getMAXLEN();
private:
static int MAX_LEN;
char *str_;
};
#endif
String.cpp
#include <iostream>
#include "String.h"
using namespace std;
int String::MAX_LEN = 1024;
void String::setMAXLEN(int maxlen)
{
MAX_LEN = maxlen;
}
int String::getMAXLEN()
{
return MAX_LEN;
}
String::String()
{
str_ = new char('\0');
cout << "default constructor String!" << endl;
}
String::String(char * str)
{
cout << "constructor String!" << endl;
int len = strlen(str) + 1;
str_ = new char(len);
memset(str_, 0, len);
strcpy(str_, str);
}
String :: ~String()
{
cout << "destory String!" << endl;
}
String::String(const String& other)
{
int len = strlen(other.str_) + 1;
str_ = new char(len);
memset(str_, 0, len);
strcpy(str_, other.str_);
}
String& String :: operator=(const String& other)
{
if (this == &other)
{
return *this;
}
int len = strlen(other.str_) + 1;
delete[] str_;
str_ = new char(len);
memset(str_, 0, len);
strcpy(str_, other.str_);
return *this;
}
void String::Display()
{
cout << str_ << endl;
}
main.c
#include <iostream>
#include "String.h"
using namespace std;
int main()
{
String s1;
s1.setMAXLEN(2048);
cout << s1.getMAXLEN() << endl;
return 0;
}
Operation results:
The advantages of static members:
· The name of a static member is in the scope of the class, so you can avoid other class members or global objects.
Name conflict
· Encapsulation can be implemented. static members can be private, while global objects cannot.
· Reading programs can easily see that a static member is associated with a class, and this visibility can clearly reflect the process.
The intention of the preacher.
Use of static cost members:
The integer static const member can be initialized in the class definition body, and the member can be defined outside the class.
static member functions:
· static member functions do not have this pointer
· Non-static member functions can access static members
· Static member functions cannot access non-static members
static summary:
1. Used to modify variables within functions, i.e. static variables within functions. The lifetime of this variable is longer than that of the function, which makes the function have a certain "state". Functions that use static variables are generally non-reentrant and thread-safe, such as strtok(3)
2. Used at the file level (outside the body of the function), modify a variable or function to indicate that the variable or function is only visible in this file, and other files can not see or access the variable or function.
2. const keyword
1.const member function:
· const member functions do not modify the state of objects
· The const member function can only access the value of the data member, but cannot modify it.
2.const object
If an object is specified as const, it tells the compiler not to modify it.
· Definition of const object:
const class name object table (parameter table)
· Const objects cannot call non-const member functions
· const modifies the pointer, the address of the pointer is immutable, and the value in the pointing space is variable!
3. mutable keyword
· Data members modified with mutable can be used even in const objects or const member functions
Amendment.
Four, expand
1. Computation of class/object size
· Class size calculation follows the structure alignment principle previously learned
· Class size is independent of data members and member functions
· Class size is independent of static data members
· Virtual functions are independent of class size
· The effect of virtual inheritance on class size
2. Object scope and lifetime
· Stack object
Implicit calls to constructors (calls are not displayed in the program)
· heap object
Implicit calls to constructors (calls are not displayed in the program)
· Global Call, Static Global Object
Global objects are constructed prior to main functions
Initialized global variables or static global objects are stored in the. data segment
Uninitialized global variables or static global objects are stored in. bss segment
· Static local object
Initialized static local variables are stored in. data segment
Uninitialized static local variables are stored in. bss segment