This article and everyone to share is mainly in C + + static data members of the relevant usage and source code examples, hoping to help you better learn C + +.
Static(statically stored) data member
StaticTest.cpp: defines the entry point of the console application.
1. Knowledge points
Static(statically stored) data member: created and initialized at compile time.
2. Code
#include "stdafx.h"
#include
using namespace Std;
Class computer
{
Private
float price;
Public
static float total_price;//static data member to compiler description: How to allocate memory for static data members
Computer (const float p)
{
Price = P;
Total_price + = p;
}
~computer ()
{
Total_price-= Price;
}
void print ()
{
cout<< " Total Price:" <<total_price<<endl;
}
};
float Computer::total_price = 0;// true memory allocation
int _tmain (int argc, _tchar* argv[])
{
Computer COMP1 (7000);
cout<< " buy computer 1 " <<endl;
Comp1.print ();
cout<<computer::total_price<<endl;;
Computer COMP2 (4999);//
cout<< " Total Price:" << " after purchasing computer 2 " <<endl;
Comp1.print ();
Computer Comp3 (2500);
cout<< " buy computer 3 " <<endl;
Comp1.print ();
push off the computer 2
Comp2.~computer ();
cout<< " back computer 2 " <<endl;
Comp1.print ();
return 0;
}
3. Running Results
original link:http://www.maiziedu.com/wiki/cplus/data/
Detailed description of static data members in C + +