DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 2 of 2
  1. #1
    Join Date
    Oct 2005
    Posts
    173

    how to handle pointer to parent in copy constructor

    Got a question on how to write copy constructors when pointers to parent object is involved.

    Code:
    class File
    {
    private:
    	vector<Data> file;
    public:
    	// constructor
    	const CombinedData * parentSet;
    	File(const string &  strin, const CombinedData * parent);
    	File(const IDTSegDefFile & old);
    	~File(void);
    };
    I have a File object that has a vector of Data obects. I need to keep track of which File each Data belongs to. So withing each Data, I've got a File pointer.

    Code:
    class Data
    {
    public:
    	// constructor
    	const File * parentFile;
    	Data(const string & strin, const File * parent);
    	Data(const Data & old);
    	~Data(void);
    };
    I populate the data vector by this in the File constructor and pass along the this pointer so each Data can point back to the parent File obj.

    Code:
    File::File(const string &  strin, const CombinedData * parent)
    {
        ...
        // reads data from file and creates a vector of Data obj
        file.push_back(Data(currentLineRead, this));
        ...
    };

    so I can see an issue when a copy constructor is used. When a copy is made, all the references point back to the original obj.

    Code:
    Data::Data(const Data & old):
    	lac       (old.lac),
    	lgc       (old.lgc),
    	lstc      (old.lstc),
    	name      (old.name),
    	path      (old.path),
    	sif       (old.sif),
    	sn        (old.sn),
    	ude       (old.ude),
    	parentFile(old.parentFile)
    {
    };
    let say I've got these lines of code:

    File A(.... , ....);
    File B(A);

    when the vector in B copies A, what File obj does each Data in B.file point to?
    B.file = A.file;

    I would think it's still A.....because the way my Data copy constructor above is written.

    What's the proper way to update the parent object? update parent pointer of each Data oject from the File copy constructor after the copy of vector is made?

    Code:
    File::File(const Data & old): file(old.file)
    {
        for (int i=0; i<file.size(); i++)
        {
            file[i].updateParent(this);
        }    
    };


    in a realted question, since these pointer do not piont to objects created by new, I don't need to have delete in the destructor right? but should I have a parentFile = 0;

    Thanks!
    Last edited by rssmps; 09-26-2006 at 12:21 PM.

  2. #2
    Join Date
    Jan 2005
    Location
    UK
    Posts
    604
    Yes,
    your copied object would point to the original parent. Your implementation of the copy constructor is actually what would have been created for you anyway. And that will cause you problems, when the copied/original Data-objects are deleted, because the same memory will be attempted to be deleted twice (or multiple times). *BAD*.
    I recommend you have a look at your design: I think the is the real problem here.
    Probably what you really need is something like:
    Code:
    class File
    {
    ...
    };
    class FileData
    {
    ...
    private:
        // composition: open the file,keep it open if you must and store the data here
        File theFile;
        vector<char> theData;
    };
    Or if the File class is a very special class that only ever will be used by the Data class you can nest it into the data class.

    Hope that helps,
    D

    With
    DKyb
    -------------------------------
    Life is a short warm moment -
    Death is the long cold rest.
    Pink Floyd
    -------------------------------

Similar Threads

  1. Replies: 1
    Last Post: 04-07-2006, 06:23 PM
  2. Replies: 2
    Last Post: 02-06-2006, 04:44 PM
  3. Replies: 3
    Last Post: 10-21-2005, 10:06 AM
  4. Copy Constructor
    By dubbu in forum C++
    Replies: 2
    Last Post: 04-04-2005, 04:31 PM
  5. Replies: 3
    Last Post: 08-07-2002, 03:37 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links