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 7195643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:37:09+00:00 2026-05-28T20:37:09+00:00

I would like to create class which can’t be copied, so I put copying

  • 0

I would like to create class which can’t be copied, so I put copying constructor into the private section:

class NotCopyable
{
public:
    NotCopyable(const double& attr1, const double& attr2) : _attr1(attr1), _attr2(attr2) {}
    ~NotCopyable(void) {}

private:
    NotCopyable& operator=(const NotCopyable&);
    NotCopyable(const NotCopyable&);
    double _attr1;
    double _attr2;
};

Everything is OK except when I would like to assign the array:

NotCopyable arr[] =
{
    NotCopyable(1, 0),
    NotCopyable(2, 3)
};

The compiler says that she can’t access copying constructor as it is in private section.
When I put it in public section:

class NotCopyable
{
public:
    NotCopyable(const double& attr1, const double& attr2) : _attr1(attr1), _attr2(attr2) {}
    ~NotCopyable(void) {}
    NotCopyable(const NotCopyable&)
    {
        std::cout << "COPYING" << std:: endl;
    }
private:
    NotCopyable& operator=(const NotCopyable&);

    double _attr1;
    double _attr2;
};

Program compiles without errors, but copying constructor isn’t called. So the question is: how do I forbid copying but still have possibility to assign arrays?

  • 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-28T20:37:11+00:00Added an answer on May 28, 2026 at 8:37 pm

    Your code arr [] = { NotCopyable(1,2) }; does request the copy constructor, at least formally. Practically the copy is usually elided, but that falls under the “as-if” rule, and the copy constructor still has to be accessible, even though ultimately it isn’t used. (In GCC you can say -fno-elide-constructors to actually invoke the copy constructor.)

    You can’t solve this in C++03, where brace-initialization always necessitates a formal copy. In C++11 you can use brace initialization to direct-initialize array members, though:

    NotCopyable arr[] { {1, 0}, {2, 3} };
    

    This works even in the absence of an accessible copy constructor.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to create my own Time class which can accept the hour
I would like to create an abstract class with an abstract method which can
I would like to create a class whose methods can be called from multiple
I would like to create a function which can take two arguments object and
I would like to create one class which holds some key values of the
I would like to create a helper class which is similar to the UrlHelper
Here's the problem. I would like to create a class which will contain configuration
I would like to create a class MyView, which extends LinearLayout. MyView has a
I would like to create a Visual Studio addin which can identify if the
I would like to create a class that creates and manages log files. I

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.