You are heading the wrong way...
There are a number of ways to implement this but the easiest I can think
of is to use RTTI.
To get around with counting sub-objects you can rely on virtual
inheritance, which only happens at the top of the inheritance tree.
Here is a simple demo:
#include
#in
Pierre Barbier de Reuille wrote:
> [EMAIL PROTECTED] wrote:
> > Pierre Barbier de Reuille wrote:
> [...]
> >
> > I thank you for your response. The equivalent of your solution is
> > posted hereunder:
> > class cA(object):
> > count=0
> > def __init__(self):
> > sel
[EMAIL PROTECTED] wrote:
> Pierre Barbier de Reuille wrote:
[...]
>
> I thank you for your response. The equivalent of your solution is
> posted hereunder:
> class cA(object):
> count=0
> def __init__(self):
> self.__class__.count +=1
> @classmethod
>
Pierre Barbier de Reuille wrote:
> [EMAIL PROTECTED] 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 pos
[EMAIL PROTECTED] 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:
>
Noah Roberts wrote:
> What happens if you print Parent.getcount() now?
You still get 2 since there is no new instance of Parent that was
created.
Alain
--
http://mail.python.org/mailman/listinfo/python-list
[EMAIL PROTECTED] 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:
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
> -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
wrote in news:[EMAIL PROTECTED] in
comp.lang.python:
> #include
> using namespace std;
>
> // A base class that provides counting
> template class Counted {
> static int count;
> };
>
> template int Counted::count = 0;
>
> // Curious class definitions
> class CountedClass : public Counted
[EMAIL PROTECTED] wrote:
> I am no C++ expert but i guess there might be some in the Python and
> C++ newsgroups.
>
Provide compilable code that exibits your problem. The technique is
sound; you must be screwing up somehow.
#include
using namespace std;
template
class counted
{
static i
[EMAIL PROTECTED] wrote:
> You miss the point; i want to derive a class and inherit all properties
> without worrying about those implementation details. The Python code is
> much cleaner in that respect. My post is about whether it is possible
> to get such a clean interface in C++
I was simply
Jon Clements wrote:
> [EMAIL PROTECTED] wrote:
> > // Curious class definitions
> > class CountedClass : public Counted {};
> > class CountedClass2 : public Counted {};
> >
> > It apparently works but in fact it doesn't:
> > If you derive from such a class, you get the count of the parent class,
>
[EMAIL PROTECTED] wrote:
> Hello,
>
> I have the need to write the equivalent of Python class methods in C++.
>
> Chuck Allison proposes the following
> (http://www.artima.com/cppsource/simple.html):
> #include
> using namespace std;
>
> // A base class that provides counting
> template class C
[EMAIL PROTECTED] wrote:
> // Curious class definitions
> class CountedClass : public Counted {};
> class CountedClass2 : public Counted {};
>
> It apparently works but in fact it doesn't:
> If you derive from such a class, you get the count of the parent class,
>
> not of the derived class.
> cla
Hello,
I have the need to write the equivalent of Python class methods in C++.
Chuck Allison proposes the following
(http://www.artima.com/cppsource/simple.html):
#include
using namespace std;
// A base class that provides counting
template class Counted {
static int count;
public:
Counted(
Hello,
I have the need to write the equivalent of Python class methods in C++.
Chuck Allison proposes the following
(http://www.artima.com/cppsource/simple.html):
#include
using namespace std;
// A base class that provides counting
template class Counted {
static int count;
public:
Counted()
17 matches
Mail list logo