如何在C ++中实现这个Python习语 [英] How do you implement this Python idiom in C++

查看:52
本文介绍了如何在C ++中实现这个Python习语的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


我需要在C ++中编写相当于Python类的方法。


Chuck Allison提出以下

http://www.artima。 com / cppsource / simple.html):

#include< iostream>

使用命名空间std;


//提供计数的基类

模板< class Tclass计算{

静态int计数;

public:

Counted(){++ count; }

计算(const Counted< T>&){++ count; }

~Counted(){ - count; }

static int getCount(){return count; }


};


模板< class Tint计算< T> :: count = 0;


//好奇的类定义

class CountedClass:public Counted< CountedClass {};

class CountedClass2:public Counted< CountedClass2 {};


它显然有效,但事实上它并不是:

如果你从这样一个类派生,你得到了父类的数量,

不是派生类。

class CountedClass3:public CountedClass {};


int main(){

CountedClass a;

cout<< CountedClass :: getCount()<< ENDL; // 1

CountedClass b;

cout<< CountedClass :: getCount()<< ENDL; // 2

CountedClass3 c;

cout<< CountedClass3 :: getCount()<< ENDL; // 3并且应该是1

cout<< CountedClass :: getCount()<< ENDL; // 3应该是2


}


我不是C ++专家,但我想在Python中可能会有一些/>
C ++新闻组。


Alain

解决方案


al********@yahoo.fr 写道:


//好奇的类定义

class CountedClass:public Counted< CountedClass {};

class CountedClass2:public Counted< CountedClass2 {};


它显然有效,但事实上它并不是:

如果你从这样的一个类派生,你得到了父类的数量,


不是派生类。

class CountedClass3:public CountedClass {};



提示:其中'模板参数按前两个

语句消失...


Jon。


al ******** @ yahoo.fr 写道:


您好,


我需要在C ++中编写相当于Python类的方法。


Chuck Allison提出以下内容

http://www.artima.com/cppsource/simple.html):

#include< iostream>

使用命名空间std;


//提供计数的基类

template< class Tclass Counted {

static int count;

public:

Counted(){++ count; }

计算(const Counted< T>&){++ count; }

~Counted(){ - count; }

static int getCount(){return count; }


};


模板< class Tint计算< T> :: count = 0;


//好奇的类定义

class CountedClass:public Counted< CountedClass {};

class CountedClass2:public Counted< CountedClass2 {};


它显然有效,但事实上它并不是:

如果你从这样一个类派生,你得到了父类的数量,

不是派生类。

class CountedClass3:public CountedClass {};


int main(){

CountedClass a;

cout<< CountedClass :: getCount()<< ENDL; // 1

CountedClass b;

cout<< CountedClass :: getCount()<< ENDL; // 2

CountedClass3 c;

cout<< CountedClass3 :: getCount()<< ENDL; // 3并且应该是1

cout<< CountedClass :: getCount()<< ENDL; // 3应该是2


}


我不是C ++专家,但我想在Python中可能会有一些/>
C ++新闻组。


Alain



为什么不发布你想要的Python代码翻译 ?在这里,我不知道你想要实现什么!!


Pierre


< blockquote>

Jon Clements写道:

al * *******@yahoo.fr 写道:


//好奇的类定义

class CountedClass:public计算< CountedClass {};

class CountedClass2:public Counted< CountedClass2 {};


它显然有效,但事实上并非如此:

如果从这样的类派生,则得到父类的计数,


不是派生类。

class CountedClass3:public CountedClass {};



提示:模板参数在前两个

语句中去了......

Jon。



你错过了这一点;我想派生一个类并继承所有属性

而不必担心这些实现细节。在这方面,Python代码更加清晰。我的帖子是关于是否可以在C ++中获得这样一个干净的界面


Alain


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 <iostream>
using namespace std;

// A base class that provides counting
template<class Tclass Counted {
static int count;
public:
Counted() { ++count; }
Counted(const Counted<T>&) { ++count; }
~Counted() { --count; }
static int getCount() { return count; }

};

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

// Curious class definitions
class CountedClass : public Counted<CountedClass{};
class CountedClass2 : public Counted<CountedClass2{};

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.
class CountedClass3 : public CountedClass {};

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

}

I am no C++ expert but i guess there might be some in the Python and
C++ newsgroups.

Alain

解决方案


al********@yahoo.fr wrote:

// Curious class definitions
class CountedClass : public Counted<CountedClass{};
class CountedClass2 : public Counted<CountedClass2{};

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.
class CountedClass3 : public CountedClass {};

Hint: where''s the template parameter gone as per the previous two
statements...

Jon.


al********@yahoo.fr 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 <iostream>
using namespace std;

// A base class that provides counting
template<class Tclass Counted {
static int count;
public:
Counted() { ++count; }
Counted(const Counted<T>&) { ++count; }
~Counted() { --count; }
static int getCount() { return count; }

};

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

// Curious class definitions
class CountedClass : public Counted<CountedClass{};
class CountedClass2 : public Counted<CountedClass2{};

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.
class CountedClass3 : public CountedClass {};

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

}

I am no C++ expert but i guess there might be some in the Python and
C++ newsgroups.

Alain

Why don''t you post the Python code you want to "translate" ? Here, I
just don''t know what you want to achieve !!

Pierre



Jon Clements wrote:

al********@yahoo.fr wrote:

// Curious class definitions
class CountedClass : public Counted<CountedClass{};
class CountedClass2 : public Counted<CountedClass2{};

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.
class CountedClass3 : public CountedClass {};


Hint: where''s the template parameter gone as per the previous two
statements...

Jon.

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++

Alain


这篇关于如何在C ++中实现这个Python习语的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆