开发者

Array objects initialization whose class has some ctor / dtor

I would like to realize array objects initialization by using the initialization statement as follows.

TestClass array[5] = {
    TestClass("test1"),
    TestClass("test2"),
    TestClass("test3"),
    TestClass("test4"),
    TestClass("test5")
};

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 / destructo开发者_如何学编程r. Following this, I've just created the following sample code and see what happens.

#include <iostream>
#include <sstream>
#include <string>

class TestClass
{
public:

    TestClass(const char* name) : name_(name)
    {
        std::cout << "Ctor(const char*) : " << name_ << std::endl;
    }

    ~TestClass()
    {
        std::cout << "Dtor() : " << name_ << std::endl;
    }

    TestClass() : name_("")
    {
    }

    void print()
    {
        std::cout << "obj:" << name_ << std::endl;
    }
private:
    TestClass(const TestClass& 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 < sizeof(array)/sizeof(array[0]); ++i) {
        array[i].print();
    }

    return EXIT_SUCCESS;
}

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.

error: ‘TestClass::TestClass(const TestClass&)’ is private

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.

TestClass::TestClass(const TestClass& rhs) : name_(rhs.name_)
{
    std::cout << "Copy Ctor : " << name_ << std::endl;
}

I could successfully compile the source code. However, when I execute the program which has been built the above, I got the following output.

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

What I'm curious to know is the following,

  1. Why we cannot make the copy constructor declared as private?

  2. 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.)

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.


The compiler elides the copy, but the copy-constructor still has to be accessible.


Whether of not the copy constructor is used by the compiler, it must be accessible - i.e. it must not be private. In this case, the compiler could avoid using the copy constructor, by using the const char * constructor directly, but it still needs an accessible copy ctor. This is the kind of thing not covered in the ARM, which is way out of date.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜