L20_static - Today Today • static • what it does EECS...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Today Today • static • what it does EECS 183 University of Michigan static ML Dorf Problem Problem • Count how many times a specific function is called void whatsIt() { static int num = 0; num++; } EECS 183 University of Michigan static ML Dorf static static variable • changes the lifetime of the variable • created the 1st time it is encountered • exists until the end of the program • run static.cpp EECS 183 University of Michigan static ML Dorf static static function • static int myFunc(); • int myFunc(); • a 'static' function has a name visible in that translation unit only • A normal function has a name visible across the entire program EECS 183 University of Michigan static ML Dorf Example: Example: • If there are 2 source files with the following functions: static int foo() { return 0; } int bar() { return 0; } • both files are the same • bar: would give – function already defined • foo: would be fine EECS 183 University of Michigan static ML Dorf Classes: Classes: static member functions • have class scope • they do NOT have access to the 'this' pointer of the class • when a member is declared as 'static' – it has only one data for the entire class even though there are many objects created for the class EECS 183 University of Michigan static ML Dorf Example: Example: class example { private: static int sum; // keeps count of how many instances there are int x; public: example() { sum = sum + 1; x = 0; } }; EECS 183 University of Michigan static ML Dorf Class: Class: static member function • can access ONLY – static member data – static member functions and – data and functions outside the class • can be called, even when a class is not instantiated • A non-static member function can be called ONLY after instantiating the the class as an object • cannot have access to the 'this' pointer of the class • NOT USED FREQUENTLY EECS 183 University of Michigan static ML Dorf ...
View Full Document

Page1 / 8

L20_static - Today Today • static • what it does EECS...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online