Run Code
|
API
|
Code Wall
|
Users
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Blog
Breakfast Function
//g++ 7.4.0 //Accessing static member without an object #include <iostream> using namespace std; class Breakfast { public: Breakfast (int qty ):itsqty(qty){item++;} virtual ~Breakfast (){item--;} virtual int GetQty(){return itsqty;} virtual void SetQty(int qty){itsqty=qty;} static int item; private: int itsqty; }; int Breakfast::item=0; void TheFunction();//This function does not create a breakfast object nor does it take a breakfast object as parameter but it can access item member variable int main () { const int MaxItem=5; Breakfast * Break[MaxItem]; for(int i=0;i<MaxItem;i++) { Break[i]=new Breakfast (i); TheFunction(); } for(int i=0;i<MaxItem;i++) { delete Break[i]; TheFunction(); } return 0; } void TheFunction() { cout<<"There are "<<Breakfast::item<<" items left!\n"; }
run
|
edit
|
history
|
help
0
Please
log in
to post a comment.
introduction c-types
30 აპრილი
003#
const_cast
Template arguments pack
Q
Why C++ optimizer has problems with these temporary variables
spiral traversal of a matrix
infix to postfix v 6.0 - (exponent support + multiple digits)
role of destructor during DMA
Please log in to post a comment.