English 中文(简体)
数组对象初始化,其类具有某些ctor/dtor
原标题:Array objects initialization whose class has some ctor / dtor

我想通过使用如下初始化语句来实现数组对象的初始化。

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

根据一些权威的书,比如C++的ARM(带注释的参考手册),它似乎说这是初始化具有构造函数/析构函数的对象数组的方法。接下来,我刚刚创建了以下示例代码,看看会发生什么。

#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;
}

至于使用GNUGCC(4.1.2)编译上述源代码的第一次尝试,它因生成以下内容而失败。

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

所以我理解这意味着为了允许对象数组初始化,它需要复制构造函数。然后,我尝试通过引入用户定义的(公共)复制构造函数来编译上面的代码,如下所示。

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

我可以成功地编译源代码。然而,当我执行上面构建的程序时,我得到了以下输出。

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

我很想知道的是,

  1. 为什么我们不能将复制构造函数声明为私有?

  2. 为什么没有调用用户定义的复制构造函数(我本以为输出应该在某个地方包含“copy Ctor:xxxx”。但我没有得到。所以我知道用户定义的副本构造函数没有被调用。)

事实上,我真的不确定以上是GNUGCC特有的,还是这是C++语言规范。。。如果你们中的一些人能给我上面的正确指针,我将不胜感激。

最佳回答

无论编译器是否使用复制构造函数,它都必须是可访问的,即它不能是私有的。在这种情况下,编译器可以通过直接使用const-char*构造函数来避免使用复制构造函数,但它仍然需要一个可访问的复制构造函数。这是ARM中没有涵盖的事情,已经过时了。

问题回答

编译器会删除副本,但副本构造函数仍然必须是可访问的。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签