How do you implement this Python idiom in C++

alainpoint at yahoo.fr alainpoint at yahoo.fr
Thu Jul 27 16:13:13 EDT 2006


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)

This is as simple as it can get. I just derive from Parent and i get my
proper count (added to my parent's if i wish so).
I wish i could achieve such a code purity in C++.

Alain




More information about the Python-list mailing list