> -----Original Message-----
> From: [EMAIL PROTECTED] 
> [mailto:[EMAIL PROTECTED]
g] On Behalf Of [EMAIL PROTECTED]
> Sent: Thursday, July 27, 2006 5:51 PM
> Subject: How do you implement this Python idiom in C++
> 
> I am no C++ expert but i guess there might be some in the Python and
> C++ newsgroups. 

You could try sth. like this:

<cpp>
#include <iostream>


template<class T> class Counted {
    static int count;
public:
    Counted() { ++count; }
    Counted(const Counted<T>&) { ++count; }
    ~Counted() { --count; }
    template <class V> friend int getCount(V&);
};

template<class T> int Counted<T>::count = 0;

// Curious class definitions
class CountedClass : private Counted<CountedClass> {};
class CountedClass2 : private Counted<CountedClass2> {};
class CountedClass3 : public CountedClass, private Counted<CountedClass3> {};

template <class T>
int getCount(T &)
{
    return Counted<T>::count;
}


using namespace std;

int main() {
  CountedClass a;
  cout << getCount(a) << endl;    // 1
  CountedClass b;
  cout << getCount(b) << endl;    // 2
  CountedClass3 c;
  cout << getCount(c) << endl;    // 1
  cout << getCount(a) << endl;    // 3 and should be 2??
}
</cpp>

Your last assertion ('should be 2') is questionable, IMHO.  I think the above 
code has it right in this case rather than your python version (after all c isA 
CountedClass, isn't it?).


cheers,

aa


-- 
Andreas Ames | Programmer | Comergo GmbH |
Voice:  +49 69 7505 3213 | ames AT avaya DOT com
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to