内联,联合,显式关键字 [英] inline, union,explicit keywords

查看:148
本文介绍了内联,联合,显式关键字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我仍然对许多C ++关键字感到困惑,例如inlineexplicitunion.
一个.对于inline,它与普通功能有什么区别?当我们必须使用它时?
b.对于union,是否必须将其用于声明结构?因此,如果正确,那么我们是否有三种方法来声明一个类(classstructunion)?
C.对于explicit.仍然不知道如何使用它
:confused::confused::confused:

我在许多源代码中找到了这些关键字,但没有找到任何线索.因此,非常感谢您的帮助.
非常感谢.

Hi everyone,

I''m still confused with many of C++ keywords, such inline, explicit and union.
a. For inline, what are the differences with ordinary function? And when we have to use it?
b. For union, is it have to be used for declaring a structure? So if it''s right, do we then have three ways to declare a class (class, struct, and union)?
c. For explicit. Still have no idea how to use it
:confused: :confused: :confused:

I found those keywords in many source codes, but I haven''t find any clue for them. So, I really appreciate your help.
Many thanks.

推荐答案

a)在C ++中调用函数时,编译器必须生成代码以处理参数,然后跳转到包含该函数的代码.被调用的函数必须建立一堆数据结构来处理诸如异常和从函数返回之类的事情.这些全部占用可执行文件中的空间和执行时间.如果一个函数很小,那么这种开销会使该函数中的任何实际代码都相形见war.一个不错的优化编译器将对函数的代码和执行大小进行分析,如果看起来代码更小和/或更快,它将在调用站点转储该函数的代码副本. br/>
inline可以回溯到优化器有点废话的年代.它所做的只是对编译器说:我认为这是一个短函数,可以与内联一起使用."在过去的日子里,大多数编译器都会回答是的,有人在乎!"和inline代码为您服务,而不必费心检查这是否是一个好主意.如今,大多数编译器通常会回答无论如何我都会这样做"或你是什么人?疯了?那太大了!"然后忽略关键字.结果是在现代编译器上inline不会对您的代码做太多工作-有点像register关键字.

因此,如果我是您,我将不再使用它.除非您正在编写模板类,否则更多的原因是大多数编译器不完全支持export关键字和另一个故事.

b)在C ++中,struct实际上与class相同.唯一的区别是,默认情况下,struct的所有成员均为public.以前的张贴者之一在谈论完整的垃圾-它们可以具有普通的成员函数,虚函数,构造函数和析构函数,就像classes一样.使用struct的地方实际上是一个样式问题-一些代码体使用class进行除数据聚合(它们使用struct用于)之外的所有内容,其他代码使用struct进行函子和接口类之类的操作.试用不同的样式,看看哪种样式适合您,如果您以谋生为编码,请确保至少考虑当地的编码样式,以免吓到本地人.

unionstruct一样,只是有一个重要区别*.每个数据成员都覆盖其他所有数据成员.当处理硬件时,这很好,但是当某些白痴用int覆盖std::string时,效果就不那么好了.唯一需要union的时间是当您有一组需要以多种方式解释的位并且不想用强制转换和按位操作乱码的时候.有时候,使用强制转换可以使代码更清晰,因此,如果您必须执行此类操作,请确保尝试各种方式,并查看哪种方式对观众来说最令人困惑和惊讶.

因此,有三种声明和定义类似类行为的方法,但是有区别.诚然,classstruct之间的差异非常小.

c)如果您的class具有单个参数构造函数,例如:

a) When you call a function in C++ the compiler has to generate code to process the parameters and then jump to the code containing the function. The called function has to set up a bunch of data structures to handle things like exceptions and returning from the function. These all take space in the executable file and time to execute. If a function is small this overhead can dwarf any code actually in the function. A decent optimising compiler will do an analysis of the code and execution size of a function and if it looks like the code will be smaller and/or faster it''ll dump a copy of the code for the function at the calling site.

inline is a hangover back to the days when optimisers were a bit crap. All it does is say to the compiler "I think this is a short function that could do with inlining." In them days of yore most compilers would reply "yay, someone cares!" and inline the code for you and not bother checking whether it was a good idea or not. These days most compilers will usually reply "I was going to do that anyway" or "what are you? Mad? That''s way too big!" and then ignore the keyword. The upshot is on modern compilers inline doesn''t do a lot for your code - a bit like the register keyword.

So if I were you I wouldn''t bother using it anymore. Unless you''re writing template classes but that''s more down to the fact that most compilers don''t support the export keyword and another story entirely.

b) In C++ struct is virtually the same as class. The only difference is that all members of a struct are public by default. One of the previous posters is talking complete rubbish - they can have ordinary member functions, virtual functions, constructors and destructors, just likes classes. Where you use struct is really a matter of style - some bodies of code use class for everything apart from aggregates of data (which they use struct for), others use struct for things like functors and interface classes. Experiment with different styles and see which one hits the spot for you and if you code for a living make sure you at least consider the local coding styles to avoid scaring the natives.

union is just like struct with one important difference*. Every data member overlays all the others. This is great when dealing with hardware but not so great when some idiot overlays a std::string with an int. The only time you ever need a union is when you have a set of bits that you need to interpret in more than one manner and don''t want to litter your code with casts and bitwise operations. Sometimes the code can be clearer with casts though so if you ever have to do this sort of thing make sure you try various ways and see which one looks the least confusing and surprising for the audience.

So there are three ways of declaring and defining things that behave like classes but there are differences. Admittedly the difference between class and struct are fairly minimal.

c) If you have a class with a single argument constructor, e.g:

class foo
{
    public:
        foo( int n );
};



以及接受foo或const &foo类型的参数的函数:



and a function that takes an argument of type foo or const &foo:

void bar( foo f )
{
}



那么奇怪的事情就会发生.其余代码中任何需要foo的地方,都可以提供int,代码可以正常工作,或者至少可以编译:



then weird things can happen. Anywhere the rest of your code wants a foo you can supply an int and the code will work, or at least compile:

bar( 87 );



无论代码中需要foo的什么地方,并且您提供int,编译器都会创建foo类的临时变量,并使用int对其进行初始化.然后,在该语句之后,它将出现在其中,这将销毁该临时文件(除非该临时文件在其在ho,ho,ho ...中创建的范围内绑定到const引用).

如果转换在任何时候都有意义(例如,字符串文字和std :: string之间的转换可以使代码更容易阅读),这可能会非常方便,但是它可能会引入细微的错误(等等,我怎么会这样?"正在将数字传递给该函数,而我得到的代码叫我不知道存在!").

对于那些到处都没有自动转换的情况,可以将单参数构造函数标记为explicit.它说:如果要使用此构造函数,则必须通过提及类的名称来明确地使用它."您必须将对bar的所有调用编写为类似以下内容的代码:



Wherever the code expects a foo and you supply an int the compiler will create a temporary of class foo and initialise it with the int. Then after the statement it appears in it will destroy the temporary (unless the temporary is bound to a const reference in the scope it''s created in, ho, ho, ho...).

This can be very handy if the conversion makes sense at all times (e.g. the conversion between a string literal and a std::string can make code far easier to read) but it can introduce subtle errors ("Hang on, how come I''m passing a number into that function and I''m getting code called I didn''t know existed!").

For those occasions when the automatic conversion everywhere doesn''t make a lot of sense you can mark single argument constructors as explicit. It says "If you want to use this constructor you have to do so explicitly by mentioning the name of the class." You have to code all your calls to bar as something like:

bar( foo( 87 ) );



无论如何,希望这对您有所帮助.忽略inline,不要为union烦恼-您可能不需要它,并使用explicit来关闭没有任何意义的自动转换.

干杯,



*并非严格如此-联合不能具有虚拟功能或静态成员数据.尽管它们可以包括构造函数和析构函数,但它们可以具有成员函数.



Anyway, hope this lot helps. Ignore inline, don''t sweat it about union - you probably won''t need it, and use explicit to turn off automatic conversions that don''t make a lot of sense.

Cheers,

Ash

*That''s not strictly true - unions can''t have virtual functions or static member data. They can have member functions though including constructors and destructors though.


此链接将为您提供帮助
C ++关键字 [
This link will help you
C++ keywords[^]


1)对仅在头文件中定义的函数使用内联几行.当您需要性能并且不希望很快更改定义(需要重新编译/重新链接)时很有用.
2)结构是C样式,没有通过构造函数初始化和使用析构函数进行清理的方式,这与C ++类一样.我很少使用联盟;它对于变量类型数组很有用.我有时会使用_variant_t而不是并集.
3)显式对于构造函数有助于防止自动强制转换
1) Use inline for functions defined in header files that are only a few lines. Useful when you need performance and do not expect to change the definition soon, requiring recompilation/relinking.
2) structs are C style without initializaton by constructor and cleanup with destructor that you would have with C++ classes. Union I rarely use; it is useful for variable type arrays. I sometimes use _variant_t instead of unions.
3) explicit is useful for constructors to help prevent automatic casts


这篇关于内联,联合,显式关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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