使用指针复制构造函数 [英] Copy constructor with pointers

查看:121
本文介绍了使用指针复制构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近发现,当我有一个类中的指针,我需要指定一个复制构造函数。

I have recently discovered that when I have pointers within a class, I need to specify a Copy constructor.

为了学习,我做了以下简单的代码。它编译,但在执行复制构造函数时给我运行时错误。

To learn that, I have made the following simple code. It compiles, but gives me runtime error when performing the copy constructor.

我试图只复制来自复制对象的指针的值,但避免分配相同地址。

I am trying to copy just the value from the pointer of the copied object, but avoiding assigning the same address.

那么,这里有什么问题?

So, what's wrong here?

    #include <stdio.h>
    #include <stdlib.h>
    #include <float.h>
    #include <math.h>
    #include <iostream>


    using namespace std;


    class TRY{
        public:
        TRY();
    ~TRY();
        TRY(TRY const &);

        int *pointer;

        void setPointer(int);
    };


    void TRY::setPointer(int a){
        *pointer = a;

        return;
    }


    TRY::TRY(){}


    TRY::~TRY(){}


    TRY::TRY(TRY const & copyTRY){
        int a = *copyTRY.pointer;
        *pointer = a;
    }



    int main(){

        TRY a;
        a.setPointer(5);

        TRY b = a;

        b.setPointer(8);

        cout << "Address of object a = " << &a << endl;
        cout << "Address of object b = " << &b << endl;

        cout << "Address of a.pointer = " << a.pointer << endl;
        cout << "Address of b.pointer = " << b.pointer << endl;

        cout << "Value in a.pointer = " << *a.pointer << endl;
        cout << "Value in b.pointer = " << *b.pointer << endl;

        return 0;
    }

我将使用这个概念给其他有很多指针的类,其中我需要将所有的值从对象复制到另一个。此代码最初需要复制,所以我想保留复制的可能性(我不会隐藏复制构造函数为私有)。

I'll be using this concept for other classes with lots of pointers in it, where I need to copy all values from on object to the other. Copying is initially necessary for this code, so I would like to keep the copying possibility (I won't be hiding the copy constructor as private).

此外,真正的我需要实现的类有10个指针,它可能随时间而变化。是不是有一个更聪明的方式有一个深层复制构造函数在C + + ...

Besides, the real class I need to implement has like 10 pointers, and it might be changing with time. Isn't there a somewhat smarter way to have a deep copy constructor in C++?...

推荐答案

c $ c> int * pointer 你刚刚定义了一个指针,但没有分配任何内存。首先你应该通过分配一些内存来指向一个正确的内存位置: int * pointer = new int 。然后在复制构造函数中,你必须为复制的对象分配内存。此外,不要忘记在析构函数中使用delete来释放内存。

With the statement int* pointer you have just defined a pointer but has not allocated any memory. First you should make it point to a proper memory location by allocating some memory like this: int* pointer = new int. Then in the copy constructor again you have to allocate the memory for the copied object. Also, don't forget to release the memory using delete in the destructor.

我希望这个例子有帮助:

I hope this example helps:

class B
{

public:
    B();
    B(const B& b);
    ~B();
    void setVal(int val);

private:
    int* m_p;
};

B::B() 
{
    //Allocate the memory to hold an int
    m_p = new int;

    *m_p = 0;
}

B::B(const B& b)
{
    //Allocate the memory first
    m_p = new int;

    //Then copy the value from the passed object
    *m_p = *b.m_p;
}

B::~B()
{

    //Release the memory allocated
    delete m_p;
    m_p = NULL;
}

void B::setVal(int val)
{
    *m_p = val;
}

这篇关于使用指针复制构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆