吃什么代谢快有助于减肥| 喝酒会得什么病| 关心则乱是什么意思| 贬低是什么意思| 写字楼是干什么的| 一什么桃子| 半岛铁盒是什么| 献完血应该注意什么| 海柳什么颜色最贵的| aupres是什么牌子化妆品| 梦到考试是什么意思| 主人是什么意思| 复原是什么意思| 牛骨煲汤搭配什么最好| 牙龈疼吃什么药| 晕车吃什么| 甲亢多吃什么食物比较好| 喝葡萄糖有什么功效与作用| 吃蝎子有什么好处| 最贵的烟是什么牌子| 外围是什么| 看头部挂什么科| 河北有什么特产| 头晕吃什么可以缓解| 买手是什么职业| 蚂蚁最怕什么| 一黑一白是什么蛇| 皮肤黑穿什么颜色的衣服好看| 嘴里甜是什么原因| 生长因子是什么| 如愿什么意思| exo什么时候出道的| rt表示什么意思| 哪吒为什么叫哪吒| 伤官女是什么意思| bata鞋属于什么档次| 狗懒子是什么意思| 胎儿左心室强光点是什么意思| 女人喜欢什么类型男人| 七月份是什么季节| 眼开大财主是什么生肖| 缺铁吃什么| 双侧上颌窦炎是什么病| 怂人是什么意思| 睡觉嗓子干是什么原因| 胆结石挂什么科室| 怀孕初期头晕是什么原因| 坐骨神经痛吃什么药好| ctp是什么| 藿香正气水有什么功效| 梦到明星是什么意思| 水上漂是什么意思| 喝酒脸红是什么原因| 苹果有什么功效和营养| 梦见老公回来了是什么征兆| 96615是什么电话| 右手小拇指发麻是什么原因| 今晚吃什么家常菜| 血小板高是什么问题| guess是什么意思| 起湿疹是什么原因造成的| 喝什么| 扯证是什么意思| 尿血吃什么药| 什么是菱形| 虚火牙痛吃什么药效果最快| 须发早白吃什么中成药| 什么药可以缩阴紧致| 腰上有痣代表什么| 老年人出现幻觉是什么原因| gm是什么意思| 女生排卵期是什么时候| 医院信息科是做什么| 送什么礼物给女老师| 亢奋什么意思| 四个木字念什么| 血白细胞高是什么原因| 不安腿综合征吃什么药| 回南天什么意思| 一直想大便是什么原因| 什么叫同理心| 做梦梦到理发是什么征兆| 月经两个月没来是什么原因| 阴历六月是什么月| 腔梗吃什么药| 煞笔是什么意思| 降火吃什么药| 张学良为什么不回大陆| 吧可以组什么词| acs是什么意思| dbm是什么单位| 什么钙片补钙效果好| 夏天什么颜色最防晒| 自作多情是什么意思| 软组织损伤是什么意思| 肩周炎吃什么药好得快| 怀孕牙龈出血是什么原因| 甘胆酸偏高是什么原因| 李宁是什么牌子| 肝气郁结吃什么中药| 下颌骨紊乱挂什么科| 岩茶是什么茶类| 黑曜石属于五行属什么| 地区和市有什么区别| 四季豆是什么| 开放性骨折是什么意思| 插入阴道是什么感觉| 南浦是什么意思| 害怕是什么意思| bape是什么品牌| 久负盛名的负是什么意思| 中介什么意思| 做梦梦到自己怀孕了是什么意思| 王毅是什么级别| 热闹对什么| 什么叫血糖| 乌龟吃什么| 因缘际会是什么意思| 柏读什么| 脱头发严重是什么原因引起的| ufo是什么意思| 贫血吃什么药好| 深蹲有什么好处| 为什么会吐| 裸钻是什么| 办理出院手续都需要什么| 皮肤痒用什么药最好| 排湿气吃什么药效果好| 铁蛋白是什么意思| 蛇标志的车是什么牌子| 金蟾折桂什么意思| 心电图逆钟向转位什么意思| 什么茶助眠| ed什么意思| 打嗝不停吃什么药| 维生素b6有什么作用和功效| 龙是什么意思| 大姑姐最怕弟媳什么| 吃什么能降血压最有效| 中国的国酒是什么| 格列本脲和格列美脲有什么区别| 内膜厚是什么原因| 冬眠的动物有什么| 阴历九月是什么星座| 断背山讲的是什么故事| 用什么可以美白| 93年属鸡的是什么命| 什么流淌| 白炽灯属于什么光源| 520是什么日子| 维生素b6是治什么的| 什么是不动产权证| 女人漏尿是什么原因| 自缢什么意思| 肝不好挂什么科室| 油头粉面是什么意思| 袢是什么意思| 一厢情愿是什么意思| 人体缺钾会有什么症状| 榴莲有什么好处| jbp什么意思| 急诊是什么意思| ict是什么意思| 十月二十八是什么星座| 双脚发热是什么原因| 前列腺炎吃什么消炎药好| 子嗣是什么意思| 护肝喝什么茶| 护士最高职称是什么| 坐阵是什么意思| 失眠吃什么好| 带节奏是什么意思| 1020是什么星座| vcr是什么| 荒芜是什么意思| 人丹是什么| 枸杞和什么一起泡水喝最好| 痰带血丝是什么原因| 吃避孕药不能吃什么东西| 点睛之笔是什么意思| 吃桃子对身体有什么好处| 反流性食管炎有什么症状| 胃不好吃什么最养胃| 生殖疱疹用什么药效果好| 玻璃属于什么垃圾| 两肋胀满闷胀是什么病| 为什么家里有蟑螂| 笃行是什么意思| 身正不怕影子斜是什么意思| 溘然是什么意思| 户口所在地是什么意思| 叫床是什么| 吃什么消除肺部结节| 一月27日是什么星座| 手容易出汗是什么原因| 古埃及是什么人种| 试管婴儿是什么意思| 结节状高密度影是什么意思| 手机服务密码是什么| 毒血症是什么病| 吃什么开胃| 盘尼西林是什么药| 吃牛肉不能吃什么| 印度以什么人种为主| 为什么肚子上会长毛| 泌乳素偏高是什么原因| 日加军念什么| 什么是真菌| 肛门里面痒是什么原因| 什么筷子不发霉又健康| bell什么意思| 二级烧伤是什么程度| 子宫肌瘤有什么危害| 喉咙痒咳嗽吃什么药好| 胸前长痘痘是什么原因| 什么是指标到校| 柿子与什么食物相克| 跳楼是什么感觉| 小孩子坐飞机需要什么证件| 夏天空调开什么模式| 戒指中指代表什么意思| 商标r是什么意思| 医院打耳洞挂什么科| 腊肠炒什么菜好吃| 吃芹菜有什么好处| 什么叫三叉神经痛| 俊五行属什么| ra医学上是什么意思| 似水年华是什么意思| 高亢是什么意思| dtc什么意思| 婴儿半夜哭闹是什么原因| 牛什么饭| 什么是钓鱼执法| 积是什么意思| 过敏性皮炎吃什么药好| 打嗝用什么药| 什么叫元素| 什么的荷花| 风生水起是什么生肖| 高压高是什么原因| 陪衬是什么意思| 病毒性感冒吃什么药效果好| 阿华田是什么饮料| 年底是什么时候| 女性长期缺维d会带来什么病| 起床眼睛肿是什么原因| 入赘什么意思| 骨盆前倾有什么危害| 益生菌什么时间吃最好| 吃什么对眼睛近视好| 男人额头凹陷预示什么| nba什么时候开始| 晚字五行属什么| 什么的大自然| 咖啡过敏的症状是什么| life style是什么品牌| 高考什么时候结束| 饸烙面是什么面| 精神什么满| 胎儿头围偏大什么原因| 电焊打眼最有效最快的方法是什么| 为什么会长胎记| 三观是什么意思| 百度Jump to content

吉林:冰上也能赛龙舟【图】

From Wikipedia, the free encyclopedia
百度 ”十一届三中全会后,我们党恢复了解放思想、实事求是的思想路线,党和国家工作重心实现了战略转移,统一战线也实现了从服从和服务于阶级斗争的统一战线向服从和服务于社会主义现代化建设的统一战线的历史性转变,“台湾归回祖国提上具体日程”。

In the C++ programming language, a copy constructor is a special constructor for creating a new object as a copy of an existing object. Copy constructors are the standard way of copying objects in C++, as opposed to cloning, and have C++-specific nuances.

The first argument of such a constructor is a reference to an object of the same type as is being constructed (const or non-const), which might be followed by parameters of any type (all having default values).

Normally the compiler automatically creates a copy constructor for each class (known as an implicit copy constructor) but for special cases the programmer creates the copy constructor, known as a user-defined copy constructor. In such cases, the compiler does not create one. Hence, there is always one copy constructor that is either defined by the user or by the system.

A user-defined copy constructor is generally needed when an object owns pointers or non-shareable references, such as to a file, in which case a destructor and an assignment operator should also be written (see Rule of three).

Definition

[edit]

Copying of objects is achieved by the use of a copy constructor and an assignment operator. A copy constructor has as its first parameter a (possibly const or volatile) reference to its own class type. It can have more arguments, but the rest must have default values associated with them.[1] The following would be valid copy constructors for class X:

X(const X& copy_from_me);
X(X& copy_from_me);
X(volatile X& copy_from_me);
X(const volatile X& copy_from_me);
X(X& copy_from_me, int = 0);
X(const X& copy_from_me, double = 1.0, int = 42);
...

The first one should be used unless there is a good reason to use one of the others. One of the differences between the first and the second is that temporaries can be copied with the first. For example:

X a = X();     // valid given X(const X& copy_from_me) but not valid given X(X& copy_from_me)
               // because the second wants a non-const X&
               // to create a, the compiler first creates a temporary by invoking the default constructor
               // of X, then uses the copy constructor to initialize as a copy of that temporary. 
               // Temporary objects created during program execution are always of const type. So, const keyword is required.
               // For some compilers both versions actually work but this behaviour should not be relied 
               // upon because it's non-standard.

A similar difference applies when directly attempting to copy a const object:

const X a;
X b = a;       // valid given X(const X& copy_from_me) but not valid given X(X& copy_from_me)
               // because the second wants a non-const X&

The X& form of the copy constructor is used when it is necessary to modify the copied object. This is very rare but it can be seen used in the standard library's std::auto_ptr. A reference must be provided:

X a;
X b = a;       // valid if any of the copy constructors are defined
               // since a reference is being passed.

The following are invalid copy constructors because copy_from_me is not passed as reference (&) :

X(X copy_from_me);
X(const X copy_from_me);

because the call to those constructors would require a copy as well, which would result in an infinitely recursive call.

The following cases may result in a call to a copy constructor:

  1. When an object is returned by value
  2. When an object is passed (to a function) by value as an argument
  3. When an object is thrown
  4. When an object is caught by value
  5. When an object is placed in a brace-enclosed initializer list

These cases are collectively called copy-initialization and are equivalent to:[2] T x = a;

It is however, not guaranteed that a copy constructor will be called in these cases, because the C++ Standard allows the compiler to optimize the copy away in certain cases, one example being the return value optimization (sometimes referred to as RVO).

Operation

[edit]

An object can be assigned value using one of the two techniques:

  • Explicit assignment in an expression
  • Initialization

Explicit assignment in an expression

[edit]
Object a;
Object b;
a = b;       // translates as Object::operator=(const Object&), thus a.operator=(b) is called 
             // (invoke simple copy, not copy constructor!)

Initialization

[edit]

An object can be initialized by any one of the following ways.

a. Through declaration

Object b = a; // translates as Object::Object(const Object&) (invoke copy constructor)

b. Through function arguments

type function(Object a);

c. Through function return value

Object a = function();

The copy constructor is used only for initializations, and does not apply to assignments where the assignment operator is used instead.

The implicit copy constructor of a class calls base copy constructors and copies its members by means appropriate to their type. If it is a class type, the copy constructor is called. If it is a scalar type, the built-in assignment operator is used. Finally, if it is an array, each element is copied in the manner appropriate to its type.[3]

By using a user-defined copy constructor the programmer can define the behavior to be performed when an object is copied.

Examples

[edit]

These examples illustrate how copy constructors work and why they are sometimes required.

Implicit copy constructor

[edit]

Consider the following example:

import std;

struct Person {
    int age;

    explicit Person(int age): 
        age(age) {}
};

int main() {
    Person timmy(10);
    Person sally(15);

    Person timmy_clone = timmy;
    std::println("Timmy age: {}, Sally age: {}, Timmy clone age: {}", timmy.age, sally.age, timmy_clone.age);
    timmy.age = 23;
    std::println("Timmy age: {}, Sally age: {}, Timmy clone age: {}", timmy.age, sally.age, timmy_clone.age);
}

Output

10 15 10
23 15 10

As expected, timmy has been copied to the new object, timmy_clone. While timmy's age was changed, timmy_clone's age remained the same. This is because they are totally different objects.

The compiler has generated a copy constructor for us, and it could be written like this:

Person(const Person& other): 
    age{other.age} /* Calls the copy constructor of the age. */ {}

User-defined copy constructor

[edit]

Consider a very simple dynamic array class like the following:

import std;

class IntArray {
    std::size_t size;
    int* data;
public:
    explicit IntArray(int size): 
        size{size}, data{new int[size]} {}

    ~IntArray() {
        if (data) {
            delete[] data;
        }
    }

    [[nodiscard]]
    std::size_t getSize() const noexcept {
        return size;
    }

    int operator[](std::size_t index) const noexcept {
        return data[index];
    }
};

int main() {
    IntArray first(20);
    first[0] = 25;

    {
        IntArray copy = first;
        std::println("first[0] = {}, copy[0] = {}", first[0], copy[0]);
    }  // (1)

    first[0] = 10;  // (2)
}

Output

25 25
Segmentation fault

Since we did not specify a copy constructor, the compiler generated one for us. The generated constructor would look something like:

IntArray(const IntArray& other): 
    size{other.size}, data{other.data} {}

The problem with this constructor is that it performs a shallow copy of the data pointer. It only copies the address of the original data member; this means they both share a pointer to the same chunk of memory, which is not what we want. When the program reaches line (1), copy's destructor gets called (because objects on the stack are destroyed automatically when their scope ends). Array's destructor deletes the data array of the original, therefore, when it deleted copy's data, because they share the same pointer, it also deleted first's data. Line (2) now accesses invalid data and writes to it. This produces a segmentation fault.

If we write our own copy constructor that performs a deep copy then this problem goes away.

IntArray(const IntArray& other): 
    size{other.size}, data{new int[other.size]} {
    std::copy(other.data, other.data + other.size, data); 
}

Here, we are creating a new int array and copying the contents to it. Now, other's destructor deletes only its data, and not first's data. Line (2) will not produce a segmentation fault anymore.

Instead of doing a deep copy right away, there are some optimization strategies that can be used. These allow you to safely share the same data between several objects, thus saving space. The copy-on-write strategy makes a copy of the data only when it is written to. Reference counting keeps the count of how many objects are referencing the data, and will delete it only when this count reaches zero (e.g. boost::shared_ptr).

Bitwise copy constructor

[edit]

There is no such thing as "bitwise copy constructor" in C++. However, the default generated copy constructor copies by invoking copy constructors on members, and for a raw pointer member this will copy the raw pointer (i.e. not a deep copy).

Logical copy constructor

[edit]
It can be seen that in a logical copy constructor, a new dynamic member variable is created for the pointer along with copying the values.[4]

A logical copy constructor makes a true copy of the structure as well as its dynamic structures. Logical copy constructors come into the picture mainly when there are pointers or complex objects within the object being copied.

Explicit copy constructor

[edit]

An explicit copy constructor is one that is declared explicit by using the explicit keyword. For example:

explicit X(const X& copy_from_me);

It is used to prevent copying of objects at function calls or with the copy-initialization syntax.

See also

[edit]

References

[edit]
  1. ^ INCITS ISO IEC 14882-2003 12.8.2. [1] Archived 8 June 2007 at the Wayback Machine
  2. ^ ISO/IEC (2003). ISO/IEC 14882:2003(E): Programming Languages - C++ §8.5 Initializers [dcl.init] para. 12
  3. ^ INCITS ISO IEC 14882-2003 12.8.8. [2] Archived 8 June 2007 at the Wayback Machine
  4. ^ Computer Science A Structured Approach Using C++ by Behrouz A. Forouzan and Richard F. Gilberg, figure 10-9, page 507
甲状腺应该多吃什么 农历五月二十是什么星座 255是什么意思 道理是什么意思 内心孤独的人缺少什么
笋吃多了有什么危害 hd什么意思 小孩睡觉打呼噜是什么原因 ooh什么意思 孕妇适合吃什么鱼
节操什么意思 眩晕症什么症状 一戴套就软是什么原因 为什么睡觉流口水很臭 同房出血是什么原因造成的
血小板偏低有什么危害 营业执照什么时候年审 百年好合是什么生肖 绿茶有什么好处 1022是什么星座
偏头痛挂什么科hcv8jop6ns8r.cn 女龙配什么属相最好dayuxmw.com 瑜伽什么意思hcv9jop2ns2r.cn 血糖高适合吃什么蔬菜hcv7jop5ns4r.cn pml是什么意思hcv9jop4ns5r.cn
eyki是什么牌子的手表hcv8jop0ns8r.cn 引体向上练什么肌肉hcv8jop1ns6r.cn 指甲上有白点是什么原因hlguo.com 前庭功能检查是查什么hcv9jop7ns4r.cn 呼吸性碱中毒吃什么药hcv9jop0ns4r.cn
法器是什么意思jingluanji.com 龙井属于什么茶96micro.com 孩子说话晚是什么原因是什么情况hcv9jop0ns0r.cn 落地生根是什么生肖hcv8jop5ns4r.cn 什么鱼蛋白质含量高hcv9jop2ns6r.cn
炭疽是什么病clwhiglsz.com 脂肪肝浸润是什么意思hcv9jop5ns9r.cn 尿常规挂什么科hcv7jop5ns2r.cn 儒雅什么意思hcv9jop1ns4r.cn 975是什么意思hcv9jop7ns5r.cn
百度