Presentation is loading. Please wait.

Presentation is loading. Please wait.

Static, const, volatile. Static – class variables static class variable is shared by all the class objects static class variable is shared by all the.

Similar presentations


Presentation on theme: "Static, const, volatile. Static – class variables static class variable is shared by all the class objects static class variable is shared by all the."— Presentation transcript:

1 static, const, volatile

2 Static – class variables static class variable is shared by all the class objects static class variable is shared by all the class objects it exists even if there is no (yet) any object of its class it exists even if there is no (yet) any object of its class we may access it using the scope operator we may access it using the scope operator

3 class ST { static int s; // declaration only static int s; // declaration only int i; int i;public: static int ps; // as above static int ps; // as above}; int ST::s=0, // definition ST::ps=0; ST::ps=0; declare within class declaration, define (without „static” keyword) outside the class declare within class declaration, define (without „static” keyword) outside the class define it once define it once libraries: place definitions in *.cpp, place the class declaration in *.h (there is no need or possibility of using extern ST::s) libraries: place definitions in *.cpp, place the class declaration in *.h (there is no need or possibility of using extern ST::s)

4 Static – methods may be invoked even if there is no any object (using the scope operator) may be invoked even if there is no any object (using the scope operator) may access static class variables and static methods only may access static class variables and static methods only there is no „this” in static method there is no „this” in static method

5 Static – methods class ST { static int s; static int s; int i; int i;public: static int ps; static int ps; ST(int i=0) ST(int i=0) :i(i) :i(i) { s++; s++; } static void count_us() { cout << s << "\n"; cout << s << "\n"; } void all() void all() // no static here !!! // no static here !!! { cout << s << " " << i cout << s << " " << i cout << " " << ps << "\n"; cout << " " << ps << "\n"; }};

6 void main() { int i; int i; // i=s; // there is neither ::s nor ::ps // i=ps; // i=ST::s; // private i=ST::ps; i=ST::ps; ST::count_us(); ST::count_us(); // ST::all(); // non-static method ST a; ST a; ST::count_us(); ST::count_us(); a.count_us(); a.count_us(); a.all(); a.all();}

7 const, volatile const const const int ci =7; volatile volatile volatile int vi=8; i++;i--;i=i+0;

8 const, volatile const and volatile objects const and volatile objects we may call only the const and/or volatile methods for const and/or volatile objects respectively we may call only the const and/or volatile methods for const and/or volatile objects respectively Warning: declaring const and/or volatile method usually causes overloading of this method: Warning: declaring const and/or volatile method usually causes overloading of this method: int f(); int f() const;

9 class CV { int i; int i;public: CV(int i=1) CV(int i=1) :i(i) :i(i) { }; }; void out() void out() { cout << i; cout << i; } void out() const volatile void out() const volatile { cout << i; cout << i; } void inc() void inc() { i++; i++; } void inc_c() const void inc_c() const { // i++; // i is const // i++; // i is const } void inc_v() volatile void inc_v() volatile { i++; i++; } void inc_cv() const volatile void inc_cv() const volatile { // i++; // i++; }};

10 void main() { CV cv; CV cv; const CV cvc; const CV cvc; volatile CV cvv; volatile CV cvv; const volatile CV cvcv; const volatile CV cvcv; cv.inc(); cv.inc(); cv.inc_c(); cv.inc_c(); cv.inc_v(); cv.inc_v(); cv.inc_cv(); cv.inc_cv(); // cvc.inc(); cvc.inc_c(); cvc.inc_c(); // cvc.inc_v(); cvc.inc_cv(); cvc.inc_cv(); // cvv.inc(); // cvv.inc_c(); cvv.inc_v(); cvv.inc_v(); cvv.inc_cv(); cvv.inc_cv(); // cvcv.inc(); // cvcv.inc_c(); // cvcv.inc_v(); cvcv.inc_cv(); cvcv.inc_cv(); cv.out(); //CV::out(); cv.out(); //CV::out(); cvc.out(); //CV::out() const volatile; cvc.out(); //CV::out() const volatile; cvv.out(); cvv.out(); cvcv.out(); cvcv.out();}

11 const, volatile const and/or volatile methods cannot be static. const and/or volatile methods cannot be static. neither constructor, nor destructor may be const and/or volatile neither constructor, nor destructor may be const and/or volatile declaring const and/or volatile methods cannot lead to ambiguity: declaring const and/or volatile methods cannot lead to ambiguity: int f() const; // int f() const volatile;

12 Example: const and static variables class info { static int cntr, // counter of objects static int cntr, // counter of objects serial; // static helper variable serial; // static helper variable const int nr; // object serial number const int nr; // object serial numberpublic: info(); info(); info(const info & i); info(const info & i); ~info(); ~info(); info & operator=(const info &p); info & operator=(const info &p);}; int info::cntr=0, // static info::serial=0; // static info::serial=0; // static

13 info::info() info::info() :nr(++serial) :nr(++serial) { cntr++; cntr++; cout << "\ndefault constructor of object nr" << nr; cout << "\ndefault constructor of object nr" << nr; cout << " there is " << cntr << " info objects"; cout << " there is " << cntr << " info objects"; cout.flush(); cout.flush(); } info::info(const info & i) info::info(const info & i) :nr(++serial) :nr(++serial) { cntr++; cntr++; cout << "\ncopy constructor of object nr" << nr; cout << "\ncopy constructor of object nr" << nr; cout << " using object " << i.nr; cout << " using object " << i.nr; cout << " there is " << cntr << " info objects"; cout << " there is " << cntr << " info objects"; cout.flush(); cout.flush(); }

14 info::~info() info::~info() { cntr--; cntr--; cout << "\ndestructor of object nr" << nr; cout << "\ndestructor of object nr" << nr; cout << " there will be " << cntr << " info objects"; cout << " there will be " << cntr << " info objects"; cout.flush(); cout.flush(); } info & info::operator=(const info &p) info & info::operator=(const info &p) { cout << "\nassignment to object nr” << nr; cout << "\nassignment to object nr” << nr; cout << " from object nr" << p.nr; cout << " from object nr" << p.nr; cout << " there is " << cntr << " info objects"; cout << " there is " << cntr << " info objects"; cout.flush(); cout.flush(); return *this; return *this; }


Download ppt "Static, const, volatile. Static – class variables static class variable is shared by all the class objects static class variable is shared by all the."

Similar presentations


Ads by Google