Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5966941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:52:17+00:00 2026-05-22T19:52:17+00:00

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

  • 0

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 / destructor. 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T19:52:18+00:00Added an answer on May 22, 2026 at 7:52 pm

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to sort an array in ascending order using C/C++ . The
I would like to filter an array of items by using the map() function.
I would like to quickly send email from the command line. I realize there
Would like to get a list of advantages and disadvantages of using Stored Procedures.
I would like to have a reference for the pros and cons of using
Would like to create a strong password in C++. Any suggestions? I assume it
I would like to test a string containing a path to a file for
I would like to use a language that I am familiar with - Java,
I would like to have an iframe take as much vertical space as it
I would like to have a VM to look at how applications appear and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.