c++ - How many object instances are being created here? -
there c++ code looked in tutorial video (line numbers added clarity):
1. config defaultconfig("/etc/foobar/config"); 2. config userconfig("~/.config/foobar/config"); 3. config conf; 4. /* else not involving `conf` */ 5. conf = defaultconfig.join(userconfig); my question is, how many instances of config class being created?
in case, join function returns new config instance, rather modifying existing one, @ least 3 objects being created.
my question third line (config conf;). c++ automatically create config instance on line (even though in other language, such java, have manually tell assign new config())? or c++ compiler smart enough realize conf object never accessed, instead gets replaced on line #5?
how memory allocated? enough 3 references , 4 config instances, or there more going on behind scenes?
c++ isn't java, there no references here, class objects. say, there 3+1 objects created code.
you can delay creation of third object not declaring until needed, removing line 3 , making line 5
config conf = defaultconfig.join(userconfig); that way there 3 objects created.
Comments
Post a Comment