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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T06:07:11+00:00 2026-06-04T06:07:11+00:00

I was trying to define a class like this: #include <library/foo.h> class my_class {

  • 0

I was trying to define a class like this:

#include <library/foo.h>

class my_class {
private:
  someone_elses foo;
public:
  myclass();
  ~myclass();
  //...
};

But the compiler failed: “field foo of type someone_elses has a private copy constructor”. Now I know that I can address this by:

class my_class {
private:
  someone_elses *foo;
//...
};

my_class::my_class() { foo = new someone_elses(); }
my_class::~my_class() { delete foo; }

My question is why does the compiler care whether an object’s field implements an accessible copy constructor?

  • 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-06-04T06:07:13+00:00Added an answer on June 4, 2026 at 6:07 am

    In the first version, when you are copying instances of my_class, and you must be somewhere that you’re not showing us, you are doing a deep copy of someone_elses.

    In the latter version, when you do the same thing, you are making a shallow copy of a pointer to an instance of someone_elses. Thus you don’t make a copy but instead have a pointer to the same instance in both copies of my_class.

    Since you are in fact deleting someone_elses in your destructor you are going to run into all kinds of problems. You need to be able to copy someone_elses but it seems you’re not allowed to for some reason that the author of that class thought was so important that they made it impossible.

    How to respond to this problem depends too much on the details you left out. It may be that you cannot allow copies of my_class and should be making its copy constructor private as well. This will of course break something somewhere else because that constructor is not created if it’s not used.

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

Sidebar

Related Questions

I am trying to create a CBuffer member in every CClient class like this
I'm trying to define base class, which contains typedef's only. template<typename T> class A
I'm stuck. I'm trying to dynamically define a class method and I can't wrap
Im trying to define a select tag in my view. My view looks like
I'm trying to define an action for a model Bar -- but I only
This is driving me crazy, I am trying to do something like: $this->data =
When writing a header library (like Boost ), can one define free-floating (non-method) functions
You may think this question is like this question asked on StackOverflow earlier. But
I'm trying to create a C++ dynamic library that won't have a class. I'd
I was trying some hashing algorithms with stdext::hash_value() on VS2010 and realized this: #include

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.