按值或引用传递枚举?
我的一般规则是按基本类型的值传递,并按对象的引用传递(如果需要,显然是 const 的)。但是,我不确定对枚举类型采取什么路线。我认为按值传递是首选,因为它们看起来很小,但我想听听其他人的想法。
My general rule is to pass by value for primitive types and pass by reference for objects (obviously const'd if need be). However, I'm not sure what route to take with enumerated types. I'd assume that pass by value is preferred since they are seemingly small, but I'd like to hear others thoughts.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
没有其他想法。枚举只是化装舞会(或西装,如果您愿意的话)中的积分值。它没有内部结构,只要有机会就会在寄存器中移动。如果您要按值传递 int,那么也可以这样传递枚举。
No other thoughts. An enum is just an integral value in a fancy dress (or suit, if you prefer). It has no internal structure and will travel in a register given a chance. If you'd pass an int by value, pass an enum that way, too.
枚举有一个底层表示类型,即整数类型。没有更多的理由(也没有更少的理由)通过引用传递它们,就像有理由通过引用传递整数类型一样。
Enum have a underlying representation type which is an integer types. There are no more reasons (and no less) to pass them by reference that there are reasons to pass integer types by reference.