How do you implement this Python idiom in C++

Noah Roberts roberts.noah at gmail.com
Thu Jul 27 16:36:14 EDT 2006


alainpoint at yahoo.fr wrote:
> Rob Williscroft wrote:
>
> > If this is more than idle curiosity I strongly suggest you post
> > a version of the python code you need to translate to C++.
>
> For the moment this is just healthy curiosity but i will still post the
> code i would like to see translated:
>
> class Parent:
> 	count=0
> 	def __init__(self):
> 		self.__class__.count +=1
> 	@classmethod
> 	def getcount(cls):
> 		return cls.count
>
> class Child(Parent):
> 	count=0 # replace this line by a 'pass'  statement if you don't want
> to reinitialise the count
>
> a=Parent()
> b=Parent()
> print Parent.getcount()  # you get 2
> c=Child()
> d=Child()
> e=Child()
> print Child.getcount() # you get 3 (you could get 5 if you don't
> reinitialise the count)

What happens if you print Parent.getcount() now?




More information about the Python-list mailing list