Note that there are some explanatory texts on larger screens.

plurals
  1. POArray objects initialization whose class has some ctor / dtor
    text
    copied!<p>I would like to realize array objects initialization by using the initialization statement as follows.</p> <pre><code>TestClass array[5] = { TestClass("test1"), TestClass("test2"), TestClass("test3"), TestClass("test4"), TestClass("test5") }; </code></pre> <p>According to some authoritative book like ARM (annotated reference manual) for C++, it seems that it says that this is the way to initialize object array which has constructor / destructor. Following this, I've just created the following sample code and see what happens.</p> <pre><code>#include &lt;iostream&gt; #include &lt;sstream&gt; #include &lt;string&gt; class TestClass { public: TestClass(const char* name) : name_(name) { std::cout &lt;&lt; "Ctor(const char*) : " &lt;&lt; name_ &lt;&lt; std::endl; } ~TestClass() { std::cout &lt;&lt; "Dtor() : " &lt;&lt; name_ &lt;&lt; std::endl; } TestClass() : name_("") { } void print() { std::cout &lt;&lt; "obj:" &lt;&lt; name_ &lt;&lt; std::endl; } private: TestClass(const TestClass&amp; rhs); std::string name_; }; int main() { TestClass array[5] = { TestClass("test1"), TestClass("test2"), TestClass("test3"), TestClass("test4"), TestClass("test5") }; for (unsigned int i = 0; i &lt; sizeof(array)/sizeof(array[0]); ++i) { array[i].print(); } return EXIT_SUCCESS; } </code></pre> <p>As for the first trial to compile the above source code using GNU GCC (4.1.2), it failed by generating something like the following.</p> <pre><code>error: ‘TestClass::TestClass(const TestClass&amp;)’ is private </code></pre> <p>So I understood that this means that in order to allow object array initialization, it would require 'copy constructor'. Then I tried to compile the above code by introducing user-defined (public) copy constructor as follows.</p> <pre><code>TestClass::TestClass(const TestClass&amp; rhs) : name_(rhs.name_) { std::cout &lt;&lt; "Copy Ctor : " &lt;&lt; name_ &lt;&lt; std::endl; } </code></pre> <p>I could successfully compile the source code. However, when I execute the program which has been built the above, I got the following output.</p> <pre><code>Ctor(const char*) : test1 Ctor(const char*) : test2 Ctor(const char*) : test3 Ctor(const char*) : test4 Ctor(const char*) : test5 obj:test1 obj:test2 obj:test3 obj:test4 obj:test5 Dtor() : test5 Dtor() : test4 Dtor() : test3 Dtor() : test2 Dtor() : test1 </code></pre> <p>What I'm curious to know is the following,</p> <ol> <li><p>Why we cannot make the copy constructor declared as private?</p></li> <li><p>Why the user-defined copy constructor is not invoked (I expected that the output should have included "Copy Ctor : xxxx" somewhere. But I couldn't get that. So I understood the user-defined copy constructor has not been invoked.)</p></li> </ol> <p>Actually, I'm not really sure whether the above is specific to GNU GCC or this is C++ language specification... It would be appreciated if some of you could give me the correct pointer on the above.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload