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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:07:25+00:00 2026-06-12T22:07:25+00:00

I have a problem. I need to clone objects class containing pointers. An example

  • 0

I have a problem. I need to clone objects class containing pointers. An example of the problem is in the following code:

#include "stdafx.h"
#include <iostream>
#include <string.h>
#include <vector>

class CPoint
{
protected:
    int m_x;
    int m_y;

    int *m_p;

public:
    CPoint();
    CPoint(int x, int y);
    ~CPoint();

    CPoint*         clone();
    static CPoint*  clone(CPoint& p);

    int getX();
    int getY();
    void setX(int x);
    void setY(int y);

    void toString();
};

int CPoint::getX()
{
    return m_x;
}

int CPoint::getY()
{
    return m_y;
}

void CPoint::setX( int x )
{
    m_x = x;
}

void CPoint::setY( int y )
{
    m_y = y;
}

void CPoint::toString()
{
    std::cout << "(" << m_x << ", " << m_y<< ", " << *m_p << ")" << std::endl;
}

CPoint::CPoint( int x, int y )
{
    m_x = x;
    m_y = y;    

    m_p = new int();
    *m_p = x + y;
}

CPoint::CPoint()
{
    m_p = new int();
    *m_p = 1000;
}

CPoint* CPoint::clone()
{
    CPoint *p = new CPoint();
    *p = *this;
    return p;
}

CPoint* CPoint::clone( CPoint& p )
{
    CPoint *q = new CPoint();
    *q = p;
    return q;
}

CPoint::~CPoint()
{
    if (m_p) {
        delete m_p;
        m_p = NULL;
    }
}

int _tmain(int argc, _TCHAR* argv[])
{
    CPoint *p1 = new CPoint(10, 20);
    CPoint *p2 = new CPoint(30, 40);

    p1->toString();
    p2->toString();

    CPoint *p3;
    p3 = CPoint::clone(*p1);

    p3->toString();

    CPoint *p4;
    p4 = p2->clone();

    p4->toString();

    p1->setX(50);
    p1->setY(60);
    p2->setX(80);
    p2->setY(90);

    p3->toString();
    p4->toString();

    delete p1;
    delete p2;
    delete p3;
    delete p4;

    int a;
    std::cin >> a;

    return 0;
}

The problem I have with the variable m_p. When clone objects p1 and p2 on p3 and p4, the memory addresses p1 and p3 are different but m_p address is the same. Obviously, when remove p1, p3 removal fails. With p2 and p4 is the same.

How I can clone a CPoint class object?

  • 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-12T22:07:25+00:00Added an answer on June 12, 2026 at 10:07 pm

    Assuming that m_p points to only one integer (and not a whole array), cloning can be done like this:

    CPoint* CPoint::clone()
    {
        CPoint* cloned = new CPoint(m_x, m_y);
        if (m_p)
        {
            cloned->m_p = new int;
            *cloned->m_p = *m_p;
        }
        return cloned;
    }
    

    Note that such a member pointer has the sole purpose of adding the additional possibility of having a NULL-value – which can have a separate meaning.

    Note also that the following has to be done to avoid memory leaks and heap corruption:

    • The copy constructor and assignment operator have to be “disabled” (declared private)
    • The destructor must delete m_p.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem where I need to split an input string into possible
I have a problem where I need to remove all code and triggers from
I have the following problem: I need to use XSLFO to generate a 2-column
I have a problem with Matlab - I need to do the following :
I have a problem to test my non activity-class which need the context of
I have a problem, I need to change body of method when this class
I have a problem trying to get a Distinct List of my Class Objects.
I have problem i need to convert from my Array structure to std::vector<int> ...
I have the next problem: I need to process only 1 request at a
OSX 10.7, XCode 4. I have a small problem: I need to push back

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.