typedef还是派生? [英] typedef or derive?

查看:67
本文介绍了typedef还是派生?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨伙计们,

我在一个项目中找到了这个代码:

Hi folks,

I found this code in a project:

class TestList:public std :: vector< Test> {/ *空!!! * /};


基本上这会产生相同的结果,例如

typedef std :: vector< Test> TestList; ?
class TestList : public std::vector<Test> { /*empty!!!*/ };
Basicly this ends up in the same result like
typedef std::vector<Test> TestList; ?




那么第一个版本是否有真正的好处,我不知道?

我认为第一个版本最终会变慢代码!?

一个好的编译器能否将第一个案例优化到第二个案例?


最后但并非最不重要的,看看这个:


somePtr t * = new somePtr();

....

删除t;

t = NULL;


我知道,NULL是windows风格,但这是必要的吗?


非常感谢!!


问候,

Sascha



So is there a real benefit of the first version, and I have no idea?
I think the first ends up in slower code!?
Does a good compiler optimize the first case into the second?

And last but not least, have a look at this:

somePtr t* = new somePtr( );
....
delete t;
t = NULL;

I know, NULL is windows style, but is that necessary?

Thanks a lot!!

Regards,
Sascha

推荐答案

eiji skrev:
eiji skrev:
嗨伙计们,

我在一个项目中找到了这个代码:
Hi folks,

I found this code in a project:
class TestList:public std :: vector< Test> {/ *空!!! * /};
class TestList : public std::vector<Test> { /*empty!!!*/ };




这会创建一个新类型,

基本上这会产生相同的结果,如



This creates a new type,
Basicly this ends up in the same result like

typedef std :: vector< Test> TestList; ?
typedef std::vector<Test> TestList; ?



这个引入了另一个名字。

第一个版本真的有什么好处,我不知道?


类型限制。

我认为第一个会以较慢的代码结束!?
一个好的编译器是否优化了第一个案例到第二个案例?

最后但并非最不重要的,看看这个:

somePtr t * = new somePtr();
...
删除t ;
t = NULL;

我知道,NULL是windows风格,但这是必要的吗?


and this introduces just another name.

So is there a real benefit of the first version, and I have no idea?
Type constraining.
I think the first ends up in slower code!?
Does a good compiler optimize the first case into the second?

And last but not least, have a look at this:

somePtr t* = new somePtr( );
...
delete t;
t = NULL;

I know, NULL is windows style, but is that necessary?




不,它'不是windows style,NULL是标准的一部分,并且

不,删除后没有必要为指针指定NULL。

-

TB @ SWEDEN



No, it''s not "windows style", NULL is a part of the Standard, and
no, it''s not necessary to assign NULL to the pointer after delete.

--
TB @ SWEDEN


eiji写道:
我发现这个代码是项目:

I found this code in a project:

class TestList:public std :: vector< Test> {/ *空!!! * /};

基本上这会导致相同的结果,如

class TestList : public std::vector<Test> { /*empty!!!*/ };

Basicly this ends up in the same result like

typedef std :: vector<试验> TestList; ?
typedef std::vector<Test> TestList; ?



第一个版本真的有什么好处,我不知道?


So is there a real benefit of the first version, and I have no idea?




是的。类型''TestList''和''std :: vector< Test>''在

的情况下是不同的。

我认为第一个以较慢的代码结束!?


No.

一个优秀的编译器是否优化了第一个案例到第二个案例?


No.

最后但并非最不重要的,看看这个:

somePtr t * = new somePtr() ;
...
删除t;
t = NULL;

我知道,NULL是windows风格,但这是必要的吗?



Yes. The types ''TestList'' and ''std::vector<Test>'' are different in that
case.
I think the first ends up in slower code!?
No.
Does a good compiler optimize the first case into the second?
No.
And last but not least, have a look at this:

somePtr t* = new somePtr( );
...
delete t;
t = NULL;

I know, NULL is windows style, but is that necessary?




它不是windows风格。 ''NULL''是在< cstdlib>中定义的宏。 (和

可能是其他标题)并且通常会扩展到0.至于必要性,

它取决于你。在删除

对象后,是否需要将't'设置为空指针?那么你是否写下来并不重要


t = 0;




t = NULL;


除了在后一种情况下你需要包含一个定义

''NULL''宏的标题。


V $ / $
-

请在邮寄回复时从我的地址删除资金



It''s not "windows style". ''NULL'' is a macro defined in <cstdlib> (and
possibly other headers) and usually expanding to 0. As for the necessity,
it''s up to you. Do you need ''t'' to be set to null pointer after deleting
the object? Then it does not matter whether you write

t = 0;

or
t = NULL;

except in the latter case you need to include a header that defines the
''NULL'' macro.

V
--
Please remove capital As from my address when replying by mail


eiji发布:
eiji posted:
大家好,

我在一个项目中找到了这个代码:
Hi folks,

I found this code in a project:
class TestList:public std :: vector<试验> {/ *空!!! * /};
基本上这会导致相同的结果,如
class TestList : public std::vector<Test> { /*empty!!!*/ };
Basicly this ends up in the same result like
typedef std :: vector< Test> TestList; ?
typedef std::vector<Test> TestList; ?



第一个版本真的有什么好处,我不知道?



So is there a real benefit of the first version, and I have no idea?




好​​处?取决于你的目标是什么。但是,肯定有一个

的差异:


模板< class T>

void SomeFunction(TestList const& amp; ); //在别处定义


int main()

{

std :: vector< Test>对象;


SomeFunction(对象); //< ----它会编译吗?

}

如果您使用typedef,它会_will_编译。


如果你继承了,那么_won''t_编译。


somePtr t * = new somePtr();
...
删除t;
t = NULL;



Benefit? Depends on what your aim is. However, there''s definitely a
difference though:

template<class T>
void SomeFunction(TestList const &); //Defined elsewhere

int main()
{
std::vector<Test> object;

SomeFunction(object); // <---- Will it compile?
}
It _will_ compile if you used "typedef".

It _won''t_ compile if you inherited.

somePtr t* = new somePtr( );
...
delete t;
t = NULL;



来自雄心勃勃的典型代码(也称为不过度胜任的
程序员)。我打赌同一个人使用i ++。当++ i

就足够了。


我不会写这样的代码,因为害怕被解释为精神上的

迟钝。在我删除

它指向的东西后,我没有足够的迟钝指针。如果某人愚蠢到可以做到这一点,那么Java

就在他们的路上。


您可以用微软编写的可耻代码填充仓库。 A

几个例子:


1)


#define UINT unsigned int

而不是:


typedef unsigned UINT;


2)


SomePOD对象;

ZeroMemory(& object,sizeof(object));


而不是:


SomePOD对象= {};


3)


列表是lloonngg ...

-Tomás


Typical code from an unambitious (otherwise known as "not overly competent"
programmer). I bet the same person uses "i++" all over the place when "++i"
would suffice.

I wouldn''t write such code for fear of being interpreted as mentally
retarded. I''m not retarded enough to mess with a pointer after I''ve deleted
what it was pointing to. If someone is stupid enough to do that, then Java
is right down their road.

You could fill a warehouse with the disgraceful code that Microsoft write. A
few examples:

1)

#define UINT unsigned int

rather than:

typedef unsigned UINT;

2)

SomePOD object;
ZeroMemory( &object, sizeof(object) );

rather than:

SomePOD object = {};

3)

The list is lloonngg...
-Tomás


这篇关于typedef还是派生?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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