Sunteți pe pagina 1din 4

1

C++ Addendum:
Static Members in C++

Static Members
Memory Allocators
Implementation of Singleton Classes
Comparison of Static and Friend
Functions

oop

Static Members
vs. Ordinary Members
Static Members

Ordinary Members

Data
Members

Exactly one
instantiation for
the entire class

One instantiation
for each object in
the class

Function
Members

One copy per


class; no object to
operate on

One copy per


class; has object
to operate on

Detecting Memory Leaks

After detecting a memory leak one needs to count how many


objects of a certain class are out there:
VWDWLFLQWQ
FODVV;^
SXEOLF

;  ^Q`
a; ^Q`
`
Works, but far from being modular. Solution: move the VWDWLF
definition into the class.
FODVV;^
There is only one instance
of Q, regardless of the
VWDWLFLQWQ
number of instances of the
SXEOLF
FODVV;.
;  ^Q`
a; ^Q`
`
LQW;Q 
The data member Q is shared by all objects of FODVV;.

oop

Static Member Functions

FODVV:LQGRZ^
VWDWLF:LQGRZ DOO
:LQGRZ Q[W
3RLQWZKHUH
3RLQWVL]H

SXEOLF
VWDWLF:LQGRZ KHDG YRLG ^UHWXUQDOO`
:LQGRZ QH[W YRLG ^UHWXUQ Q[W`
YRLGGLVSOD\ YRLG 
:LQGRZ  
Q[W DOO
^
DOO

:LQGRZ%
:LQGRZ$
DOO WKLV
`
`
:LQGRZ :LQGRZDOO  :LQGRZ 
Recall: statics (globals) are
automatically zero-initialized

oop

oop

Calling Static Member Functions

IRU
:LQGRZ S :LQGRZKHDG 
S  :LQGRZ 
S S!QH[W

S!GLVSOD\ 

It is also legal to call or access a static member


through an object:
:LQGRZZ
:LQGRZ S ZKHDG 

Implementing Pool Allocation



i-1



oop

Static

Static Member Functions and Operator


Overloading

1RGH 1RGH1HZ YRLG


^
LI SRRO  1RGH  ^
SRRO QHZ1RGH>&+81.@
IRU LQWL L&+81.L
SRRO>L@OHIW SRRO>L@
SRRO>&+81.@OHIW  1RGH 
`

SRRO

CHUNK-1

`
oop

BUG:
BUG:the
thechunks
chunkswill
will
never
neverbe
bedeleted
deleted

oop

In C++ one can overload almost all operators.


These include also:

1RGH UHV SRRO


SRRO SRRO!OHIW
UHWXUQUHV

Pool Allocation for Small Objects

FODVV1RGH^
HQXP^&+81. `
VWDWLF1RGH SRRO
VWDWLF1RGH 1HZ YRLG 
VWDWLFYRLG'HOHWH 1RGH Q ^
Q!OHIW SRRO
SRRO Q
`
SRRO
1RGH
1RGH
'DWDGDWD


.H\NH\
1RGH OHIW
1RGH ULJKW
1RGH 'DWDG.H\N/HIWO5LJKWU 
GDWD G NH\ N OHIW O ULJKW U ^`
IULHQGFODVV%LQDU\7UHH
`

Here is how to display all windows:

oop

QHZ
GHOHWH
QHZ>@
GHOHWH>@

The above are always (regardless of declaration)


static members!
Very natural: memory allocation and deallocation (just
as construction) is something that belongs to the class,
not the object.

Recall the pool allocation algorithm.


FODVV1RGH^
HQXP^&+81. `
VWDWLF1RGH SRRO
VWDWLF1RGH 1HZ YRLG 
VWDWLFYRLG'HOHWH 1RGH Q ^
Q!OHIW SRRO
SRRO Q
`
SRRO
1RGH


YRLG RSHUDWRUQHZ VL]HBWVL]H ^
UHWXUQ1HZ 
`
YRLGRSHUDWRUGHOHWH YRLG SWU ^
'HOHWH 1RGH SWU 
`
`

1RGHRSHUDWRUGHOHWH

1RGH

11

Recall, they are always static members

Single Objects Locally: individually by class

12

Implementation of Singleton Classes



File
6LQJOHWRQK
File6LQJOHWRQK
FODVV6LQJOHWRQ^
FODVV6LQJOHWRQ^
SXEOLF
SXEOLF
LQOLQHVWDWLF6LQJOHWRQ ,QVWDQFH 
LQOLQHVWDWLF6LQJOHWRQ ,QVWDQFH 
SURWHFWHG
SURWHFWHG
Recall that local VWDWLF
6LQJOHWRQ 
6LQJOHWRQ 
variables are initialized
`
`
when the function is first
called.

Single Objects Globally: overload the global operators


RSHUDWRUQHZ and RSHUDWRUGHOHWH.
Arrays Locally: only total size is supplied as argument


Ensure
Ensurethat
thatexactly
exactlyone
oneinstance
instanceisisdeclared,
declared,and
and

avoid
avoidthe
thedependency
dependencyof
ofinitialization
initializationorder.
order.
6LQJOHWRQ 6LQJOHWRQ,QVWDQFH
6LQJOHWRQ 6LQJOHWRQ,QVWDQFH
^^
VWDWLF6LQJOHWRQLQVWDQFH
VWDWLF6LQJOHWRQLQVWDQFH
UHWXUQ LQVWDQFH
Can you propose another idiom for
UHWXUQ LQVWDQFH
implementing singleton classes in
``
which the singleton object is

FODVV)RR^
SXEOLF
YRLG RSHUDWRUQHZ>@ VL]HBWE\WHV 
YRLGRSHUDWRUGHOHWH>@ YRLG EXIIVL]HBWE\WHV 
`

Arrays Globally: overload the global operators


RSHUDWRUQHZ>@ and RSHUDWRUGHOHWH>@.
Again, only the total size is supplied as argument.

Cast to 1RGH is necessary in 1RGHRSHUDWRU


GHOHWH since after destruction we no longer have objects
of a type 1RGH.
Both QHZ and GHOHWH operators do not take a WKLV
parameter.

oop

RSHUDWRUQHZ VL]HBW
RSHUDWRUGHOHWH YRLG

oop

is not used here, but its very important once


inheritance is introduced.
To eliminate compiler warning on unused arguments,
functions should be defined without argument names:
YRLG 1RGHRSHUDWRUQHZ VL]HBW ^UHWXUQ1HZ `

if necessary, may use the global operators:

The argument of type VL]HBW to


and

FODVV)RR^
SXEOLF
YRLG RSHUDWRUQHZ VL]HBWE\WHV 
YRLGRSHUDWRUGHOHWH YRLG EXIIVL]HBWE\WHV 
`

Another version of the operator delete:

1RGHRSHUDWRUQHZ

Summary: Overloading new and delete

10

Notes on Overloading New and Delete


RSHUDWRUGHOHWH YRLG SWUVL]HBWVL]H

oop

Overloading New and Delete

destructed when the program


exits?

oop

Summary: Static Member Functions

13

VWDWLF member functions are similar to IULHQG


functions:
No WKLV pointer
May access all members: static/non-static, function/data,
private/public/protected.

VWDWLF member functions are more powerful than


IULHQG functions:
Do not pollute the global name space.
Can be assigned visibility

VWDWLF member functions are less powerful than


IULHQG functions:
Are strongly associated with a particular class
Cannot be used for operator overloading

Calling static member function is by


Element of a class
The class itself

oop

S-ar putea să vă placă și