开发者

default d'tor, copy c'tor, operator=

let's assume I have some class A:

header

class A开发者_开发知识库{
int x;
int value() {return x};
};

main

A a;
cout << a.value();

my question is: will my compiler produce d'tor copy c'tor and operator= for me or not (cause it actually doesn't need it)

EDITED

does it write d'tor for me at all, cause it seems to be useless, may you give please example if I'm wrong


In principle, yes for the ctor and dtor, which are "used". No for the operator=: the default functions are only generated if used, which is quite important since for some classes the default operator= "wouldn't work", so it's not available.

In practice, the auto-generated ctor and dtor of this class do nothing. A compiler good enough to use for real work, will ensure that their theoretical existence doesn't result in any code being generated[*].

[*] I think. It wouldn't necessarily be all that bad to have a bunch of do-nothing-and-return functions in the binary. It would likely be quite bad if the compiler couldn't remove the calls to them, though - not for your program, but for bigger programs with millions of such objects...


Optimizers get rid of uneeded stuff for you.


if it can (e.g. the members do not prohibit it), it probably will generate them by default. it if doesn't need them, then it may dispose of them. otherwise, you may end up generating hundreds (or more) identical symbols (and they may be private).


You can easily inspect assembly code generated by your compiler - just be sure to turn the oiptimizations on. My guess is that none of these will be actually generated in your case.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜