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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T16:40:04+00:00 2026-06-02T16:40:04+00:00

Suppose I have a class where the copy constructor is private and not implemented

  • 0

Suppose I have a class where the copy constructor is private and not implemented (to make the object non-copyable)

class NonCopyable {
// whatever
 private:
    NonCopyable( const NonCopyable&);
    void operator=(const NonCopyable&);
 };

Now in some member function of the same class I write code that returns an object of that class:

NonCopyable NonCopyable::Something()
{
    return NonCopyable();
}

which is a case when RVO could kick in.

RVO still requires that a copy constructor is accessible. Since the possible call to the copy constructor is done from within the same class member function the copy constructor is accessible. So technically RVO is possible despite the fact that the intent was to prohibit using the copy constructor.

Is RVO allowed in such cases?

  • 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-02T16:40:06+00:00Added an answer on June 2, 2026 at 4:40 pm

    Your example is quite interesting.

    This is the typical C++03 declaration.

    class NC {
    public:
        NC NC::Something() {
            return NC();
        }
    
    private:
        NC(NC const&);
        NC& operator=(NC const&);
    };
    

    Here, as noted, RVO may kick in even though we semantically wanted to avoid copying.

    In C++03, the solution is to delegate:

    class NC: boost::noncopyable {
    public:
        NC NC::Something() { // Error: no copy constructor
            return NC();
        }
    };
    

    In C++11, we have the alternative of using the delete keyword:

    class NC {
    public:
        NC NC::Something() { // Error: deleted copy constructor
            return NC();
        }
    
    private:
        NC(NC const&) = delete;
        NC& operator=(NC const&) = delete;
    };
    

    But sometimes, we want to prevent copy, but would like to allow Builder (as in the Pattern).

    In this case, your example works as long as RVO kicks in, which is a bit annoying as it is, in essence, non-standard. A definition of the copy constructor should be provided but you wish it not to be used.

    In C++11, this usecase is supported by deleting copy operations and defining move operations (even privately).

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

Sidebar

Related Questions

Suppose I have this class: class Int{ private: int x; public: Int(int i) :
Suppose I have a class: class ClassX{ private: int* p; int i; .... }
Suppose I have a class module clsMyClass with an object as a member variable.
Suppose I have a class public class MyClass { private Set<String> set = new
Suppose you have a class NonCopyable class NonCopyable { public: NonCopyable(int n){} ~NonCopyable(){} [...]
I have a simple class Name: class Name { private: char nameStr[30]; public: Name(const
Suppose you have a class Dog , that has public class Dog { private
Suppose you have a class with a private pointer to an array. How can
Suppose I have class Foo(db.Model): bar = db.ReferenceProperty(Bar) foo = Foo.all().get() Is there a
Suppose I have: class Foo { ... }; class Bar : public Foo {

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.