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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:31:22+00:00 2026-05-26T13:31:22+00:00

(using Visual C++ 2010, compiling in debug with optimizations turned off ) I have

  • 0

(using Visual C++ 2010, compiling in debug with optimizations turned off)

I have the following very simple class:

class exampleClass
{
public:
    exampleClass()
    {
        cout << "in the default ctor" << endl;
    }
private:
    exampleClass (const exampleClass& e)
    {
        cout << "in the copy ctor" << endl;
    }
};

When I try to compile it with the following main:

#include <iostream>
using namespace std;

int main()
{
    exampleClass e1=exampleClass();
    return 0;
}

I get the compilation error:

'exampleClass::exampleClass' : cannot access private
                               member declared in class 'exampleClass'

When I remove the access modifier “private” from the copy ctor, the program compiles and prints only:

in the default ctor

Why is this happening? If the compiler will not invoke the copy ctor anyway, why is it bugging me?

Since some people missed the first line (at least before some edits) i will repeat it:

I compiled in debug with optimizations turned off.

  • 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-26T13:31:23+00:00Added an answer on May 26, 2026 at 1:31 pm

    This type of initialization is called copy-initialization. I believe the following clause from the C++11 standard applies here (paragraph 8.5.16, page 204):

    If the initialization is direct-initialization, or if it is
    copy-initialization where the cv-unqualified version of the source
    type is the same class as, or a derived class of, the class of the
    destination, constructors are considered. The applicable constructors
    are enumerated (13.3.1.3), and the best one is chosen through overload
    resolution (13.3). The constructor so selected is called to initialize
    the object, with the initializer expression or expression-list as its
    argument(s). If no constructor applies, or the overload resolution is
    ambiguous, the initialization is ill-formed.

    In this case, the best applicable constructor is the copy ctor, which is private, hence the error message.

    To further answer your question, when the copy ctor is private, your program is simply not allowed to pass the complier check because of the rules imposed by the standard. When you make the copy ctor public, the program becomes valid, but the call to the copy ctor is optimized away.

    EDIT:
    Okay, to elaborate on previous paragraph.You’re dealing here with the so-called copy elision. While the copy elision is possible in this case, the standard requires you to provide an accessible copy ctor for your class.

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

Sidebar

Related Questions

Using Visual Studio 2010, I have written a simple WCF service and some integration
For reference, I'm using Visual Studio 2010. I have a custom build step defined
I am using Visual Studio 2010 and suppose I have to write some program.
Compiling FluidSynth 1.1.1 from source. I have unresolved external symbol with glib, using Visual
Using Visual Studio 2010 C++. I have a project which uses MFC and I
I am using Visual Studio 2010 with IIS 5.1 but unable to debug my
I am using Visual Studio 2010 RC1. I define a resource Brush2 in app.xaml_:
I'm using Visual Studio 2010 RC. Would it be possible that after doing my
I'm using Visual C# 2010 Express (final, not beta or anything) and I'm having
I am using Visual Studio 2010 Ultimate Beta 2. What is the relevant namespace/classes

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.