这是C ++的好风格吗? [英] Is this good style of C++?

查看:82
本文介绍了这是C ++的好风格吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我碰巧阅读了一些源代码,这些源代码具有与我以前的经验不同的风格




在这种风格中,所有函数都返回RETURN_TYPE_T,这是定义为

typedef unsigned int RETURN_TYPE_T;

且返回值只能是枚举值

enum

{

SUCCESS = 0,

FAILURE = 1,

ABORT = 2

};

如果一个函数应该返回一个对象,那么API就不会像

Foo getFoo();



Foo * getFoo( );

相反,它被声明为

RETURN_TYPE_T getFoo(Foo * foo);

调用者必须新建一个Foo或获取指针首先通过其他方式通过其他方式引用Foo对象,然后将指针作为参数调用此方法。


该函数将有价值的信息填充到指向的Foo对象中

参数。通过检查返回的

RETURN_TYPE_T值来判断调用的结果。


这种编码风格对我来说很奇怪,但我被告知它是一个

好​​的风格,因为它适用规则谁创造它,谁发布

it。因此,这种风格应该可以降低内存泄漏的风险。


我仍​​然不太相信。任何有很长时间的C ++ exprirence

的人可以给出一些评论吗?

谢谢&问候

Recently I happens to read some sourcecode which has different style
from my previous experience.

In this style, all functions return a RETURN_TYPE_T which is defined as
typedef unsigned int RETURN_TYPE_T;
and the return value can only be enum value
enum
{
SUCCESS = 0,
FAILURE = 1,
ABORT = 2
};
If a function should return a object, the API is not like
Foo getFoo();
or
Foo* getFoo();
Instead, it is declared as
RETURN_TYPE_T getFoo(Foo* foo);
The caller has to new a Foo or get pointer to Foo object through other
way first, and then call this method with the pointer as parameter.

The function fill valuable info into the Foo object pointed by the
paramter. The result of invocation is judged by inspecting the returned
RETURN_TYPE_T value.

This style of coding seems strange to me, but I am told that it is a
good style, because it applys the rule "Who creates it, who releases
it". As a result, this style are supposed to reduce risk of memory leak.

I am still not quite convinced. Anybody with long time C++ exprirence
can give some comments?
Thanks & Regards

推荐答案

Cheng Mo写道:
Cheng Mo wrote:
最近我碰巧读了一些源代码与我以前的经历有不同的风格。

在这种风格中,所有函数都返回一个RETURN_TYPE_T,定义为
typedef unsigned int RETURN_TYPE_T;
和返回值只能是枚举价值
enum
{SUCCESS = 0,
FAILURE = 1,
ABORT = 2
};
如果是函数应该返回一个对象,API不像是Foo getFoo();
或者说是Foo * getFoo();
而是声明为
RETURN_TYPE_T getFoo(Foo * foo);
调用者必须首先通过其他方式新建一个Foo或获取指向Foo对象的指针,然后以指针作为参数调用此方法。
参数指向的Foo对象中。通过检查返回的
RETURN_TYPE_T值来判断调用的结果。

这种编码风格对我来说很奇怪,但我被告知它是一种好的风格,因为它适用谁创造它,谁发布它这一规则。因此,这种风格应该可以降低内存泄漏的风险。

我仍然不太相信。任何有很长时间C ++ exprirence的人都可以给出一些评论吗?
Recently I happens to read some sourcecode which has different style
from my previous experience.

In this style, all functions return a RETURN_TYPE_T which is defined as
typedef unsigned int RETURN_TYPE_T;
and the return value can only be enum value
enum
{
SUCCESS = 0,
FAILURE = 1,
ABORT = 2
};
If a function should return a object, the API is not like
Foo getFoo();
or
Foo* getFoo();
Instead, it is declared as
RETURN_TYPE_T getFoo(Foo* foo);
The caller has to new a Foo or get pointer to Foo object through other
way first, and then call this method with the pointer as parameter.

The function fill valuable info into the Foo object pointed by the
paramter. The result of invocation is judged by inspecting the returned
RETURN_TYPE_T value.

This style of coding seems strange to me, but I am told that it is a
good style, because it applys the rule "Who creates it, who releases
it". As a result, this style are supposed to reduce risk of memory leak.

I am still not quite convinced. Anybody with long time C++ exprirence
can give some comments?




如果出于特殊原因在应用程序中使用这种样式(

是,它不是一种风格),那就没关系。

否则作为C ++编程的*通用风格*它看起来很原始

和古代。

如果你想处理错误你最好使用异常,而不是

返回错误代码检查这种风格暗示。

如果您想创建没有内存泄漏的防弹代码或*其他资源*泄漏,请使用资源获取正在初始化。 (RAII):
http://groups.google.com/groups?q=%2...ntua。 gr& rnum = 1


-

Ioannis Vranos

http://www23.brinkster.com/noicys


一切都很可怕。


Cheng Mo <莫****** @ nospam.nospam> skrev i en meddelelse

新闻:co ********** @ avnika.corp.mot.com ...
All in all its horrible.

"Cheng Mo" <mo******@nospam.nospam> skrev i en meddelelse
news:co**********@avnika.corp.mot.com...
最近我碰巧看了一些源代码与我以前的经历有不同的风格。

在这种风格中,所有函数都返回一个RETURN_TYPE_T,定义为
typedef unsigned int RETURN_TYPE_T;
和返回值只能是枚举值
枚举
{SUCCESS = 0,
FAILURE = 1,
ABORT = 2
};
不要将UPPERCASE_ONLY用于值。这些名称应仅保留给

macroes。如果一个函数应该返回一个对象,那么API就不像是Foo getFoo();

Foo * getFoo();
相反,它被声明为
RETURN_TYPE_T getFoo(Foo * foo);
调用者必须首先通过其他方式新建一个Foo或获取指向Foo对象的指针,然后使用指针作为参数调用此方法。
也很可怕。第一个签名应该是首选,除非在正常情况下调用可能会导致
失败,在这种情况下,智能指针(参见boost)将是适当的

函数将有价值的信息填充到
参数指向的Foo对象中。通过检查返回的
RETURN_TYPE_T值来判断调用的结果。
通常,使用异常更可靠。没有忘记

检查返回类型,你不必用

来检查代码。
这种风格编码对我来说似乎很奇怪,但我被告知它是一种很好的风格,因为它适用了谁创造它,谁发布它这一规则。作为结果,这种风格应该可以降低内存泄漏的风险。
这是一个很好的建议,但对C ++并不那么重要。改为使用RAII。
我仍然不太相信。任何长期使用C ++的人都可以给出一些评论吗?
谢谢&问候
Recently I happens to read some sourcecode which has different style from
my previous experience.

In this style, all functions return a RETURN_TYPE_T which is defined as
typedef unsigned int RETURN_TYPE_T;
and the return value can only be enum value
enum
{
SUCCESS = 0,
FAILURE = 1,
ABORT = 2
}; Do not use UPPERCASE_ONLY for values. These names should be reserved for
macroes only. If a function should return a object, the API is not like
Foo getFoo();
or
Foo* getFoo();
Instead, it is declared as
RETURN_TYPE_T getFoo(Foo* foo);
The caller has to new a Foo or get pointer to Foo object through other way
first, and then call this method with the pointer as parameter. Also horrible. The first signature should be preferred unless the call could
fail in a normal case in which case a smart pointer (see boost) would be
appropriate.
The function fill valuable info into the Foo object pointed by the
paramter. The result of invocation is judged by inspecting the returned
RETURN_TYPE_T value. In general, using exceptions is far more reliable. There''s no forgetting of
checking the return-type, and you would not have to obfuscate your code with
all that checking.
This style of coding seems strange to me, but I am told that it is a good
style, because it applys the rule "Who creates it, who releases it". As a
result, this style are supposed to reduce risk of memory leak. This is good advice, but not so relevant for C++. Use RAII instead.
I am still not quite convinced. Anybody with long time C++ exprirence can
give some comments?
Thanks & Regards




亲切的问候

Peter



Kind regards
Peter


Peter Koch Larsen写道:
Peter Koch Larsen wrote:
不要使用UPPERCASE_ONLY作为值。这些名称只能保留给
macroes。
Do not use UPPERCASE_ONLY for values. These names should be reserved for
macroes only.



实际上一个约定是用大写命名所有常量,不管是
宏,const对象或者是枚举的成员。


-

Ioannis Vranos

http://www23.brinkster.com/noicys


这篇关于这是C ++的好风格吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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