In this example where is the C++ assignment operator used rather than the copy constructor? -


As part of a continuous process of trying to upgrade my C ++ skills, I am trying to break habits. My old school C programmer tilt is to write it;

  Zero function (widget & ref) {widget w; // Default constructor int i; For (i = 0; i <10; i ++) {w = ref; // Assignment operator dot dot stick which modifies}}  

It works well but I think the following is close to the best practice;

  Zero function (widget & ref) {for (int i = 0; i & lt; 10; i ++) {widget w = ref; // ?? // dot dot dot dot}}  

With at least my widget class, it works fine. But I do not fully understand why I have two principles;

1) The copy maker runs 10 times.
2) If the copy constructor runs once, the assignment operator runs 9 times.

Both of these problems seem a little bit artificial and wrong, especially 2). Is there a third possibility that I am missing?

Of course the copy creator runs 10 times! When you repeat the cycle (<;>>;) for , the inside the braces on the declared variables will be out of the scope if widget < If there is a destroyer in / code>, then it will be called 10 times (potential performance hits).


Comments

Popular posts from this blog

windows - Heroku throws SQLITE3 Read only exception -

lex - Building a lexical Analyzer in Java -

python - rename keys in a dictionary -