You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++中Classname(const Classname&)构造重载及私有语句作用咨询

Hey there! Let's break down your code and answer your questions clearly.

私有段中两条语句的作用

Your class's private section contains two special member declarations, both designed to prevent copying of Classname objects:

  • Classname(const Classname&);
    This is a declaration of the copy constructor, placed in the private scope. By making it private (and not providing a public implementation), you're blocking external code from creating a new Classname object by copying an existing one. For example, these lines will throw a compile error:

    Classname obj1(...); // Assume this is initialized properly
    Classname obj2 = obj1; // Error: copy constructor is private
    Classname obj3(obj1);  // Error: same reason
    
  • void operator=(const Classname&);
    This is an overload of the assignment operator. Putting it in private also blocks external code from assigning one Classname object to another. A line like this will fail to compile:

    obj1 = obj2; // Error: assignment operator is private
    

The goal here is to enforce that instances of Classname can't be copied or assigned—this is common for classes that manage exclusive resources (like file handles, network connections) or singleton classes where only one instance should exist.

C++中Classname(const Classname&)构造函数重载的相关内容

Let's dive deeper into the copy constructor, which is a special constructor in C++:

What is it?

The copy constructor is used to initialize a new object as a copy of an existing object of the same class. Its standard signature is:

Classname(const Classname& other);

The const here is a best practice—it allows the constructor to accept const objects and temporary objects as sources for copying.

Default behavior

If you don't explicitly declare a copy constructor for your class, the compiler will automatically generate a default copy constructor. This default version performs a shallow copy: it copies each member variable of the source object directly to the new object.

This works fine for simple classes with no dynamic memory or resource handles, but it's dangerous for classes that manage heap-allocated memory (like std::string under the hood, or your own classes with pointers). A shallow copy would copy the pointer value, leading to two objects pointing to the same memory—when one is destroyed, it frees that memory, leaving the other object with a dangling pointer (causing undefined behavior when accessed).

When is it called?

The copy constructor is triggered in several scenarios:

  • When initializing a new object with an existing object: Classname obj2 = obj1;
  • When passing an object by value to a function: void myFunction(Classname obj); (the parameter obj is a copy of the argument)
  • When returning an object from a function (though modern compilers often optimize this away with Return Value Optimization (RVO))

Why privatize or delete it?

As shown in your code, making the copy constructor private is an older way to prevent copying. In C++11 and later, you can use the = delete syntax to explicitly disable it, which is more readable:

Classname(const Classname&) = delete;
void operator=(const Classname&) = delete;

Both approaches achieve the same goal: ensuring that Classname objects can't be copied, which is crucial for classes where copying doesn't make sense or would cause resource conflicts.

内容的提问来源于stack exchange,提问作者Rahul Naik

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 09:35:59