Download presentation
Presentation is loading. Please wait.
1
Static Members Sharing is caring…
2
Single Copy Some things only need/want one of: Constants NextID
3
Static Static : Shared Member for which very object of class shares value Student mascot: Storm student1 name: Bob mascot: student2 name: Jane mascot:
4
Static Changing static variable in one changes for all: Student
mascot: Storm student1 name: Bob mascot: student2 name: Jane mascot:
5
Static Changing static variable in one changes for all:
student1.setMascot("Bruin") Student mascot: Bruin student1 name: Bob mascot: student2 name: Jane mascot:
6
Special Static Rules Non-const & non-literal member variables not initialized in constructor or declaration: .h file: Get own initialization outside of class .cpp file:
7
Static Functions Static Functions May only access static variables
Every object guaranteed to give the same answer Student mascot: Bruin student1 name: Bob mascot: student2 name: Jane mascot:
8
Static Functions Static Functions
Every object guaranteed to give the same answer Allowed to call static functions directly on class: CLASSNAME::function()
9
Book Example : Counter Circle with static int : number of Circles
.cpp:
10
Book Example : Counter Constructors add to counter
11
Constant Constants are good things to share: In .cpp
12
Constant Inside class, access via variable name:
From outside the class, need fully qualified name:
13
Static Rules of Thumb OK to make static Think long and hard first…
Constants Think long and hard first… Something you think should only be one of Functions you want to access through class Factory Functions Function Libraries (java style)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.