c ++ object-oriented high-level programming studies four static, class templates, function templates

Static static: static data and static functions, only one in memory, not as the number of created objects increases
static data : for example, bank account class, the account name is ordinary data, there will be 100 100 objects name accounts, but the interest rate is the same, so the interest rate can be set to static
static function : static function and other functions compared to ordinary members, not this static function pointer, so it can not deal with the general class of data, can only handle static data

static data, to be defined on the outside class.
Static function call in two ways: (1) object by calling (2) by calling class name

class Account {
public:
static double m_rate;//声明
static void set_rate(const double& x) { m_rate = x; }
};
double Account::m_rate = 8.0;//定义
int main() {
Account::set_rate(5.0); //通过class name调用
Account a;
a.set_rate(7.0); //通过object调用
}

Examples of single-mode embodiment:

class A {
public:
static A& getInstance();
setup() { ... }
private:
A();
A(const A& rhs);
...
};
A& A::getInstance()
{
static A a;
return a;
}

Class template: template <typename T>
Here Insert Picture Description
function template: template <class T>
Here Insert Picture Description

Released four original articles · won praise 0 · Views 119

Guess you like

Origin blog.csdn.net/weixin_43116900/article/details/104732866