CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Oct 2001
    Location
    Chicago, USA
    Posts
    222

    Template, class - data member copy problem

    Hi! I have a template class say Store defined like :

    Code:
    template <class obj>
    
    class Store
    {
    private:
      obj* data;
    
    public:
      Store()
      {
         data = NULL;
      }
    
      Store(Store& s)
      {
         .... //now i dont know how to do deep copy of the data from s to my data here
              //becuase if i do memcpy and pass the size as sizeof(obj), then if i have a Store<char*> then it wouldnt seem to work
              //how do i solve this problem, or if there is better way of jusy copying then it would be cool too. please help
      }
    
    };
    Thanx in advance..

    Ankur

  2. #2
    Join Date
    May 2002
    Location
    Quebec City, Canada
    Posts
    374
    From the info you give me... here's how I would do it:

    I assume the operator = has been defined for the object obj.

    PHP Code:
    template <class obj>

    class 
    Store
    {
    private:
      
    objdata;

    public:
      
    Store()
      {
         
    data NULL;
      }

      
    Store(Stores)
      {
           
    // if you only want to copy the pointer
           
    data s.data;
          
          
    // if you want to copy the data pointed by the pointer
          
    (*data) = *(s.data);
      }

    }; 
    Martin Breton
    3D vision software developer and system integrator.

  3. #3
    Join Date
    Oct 2001
    Location
    Chicago, USA
    Posts
    222
    Thanx, actually this is what i had intially thought of..but i remember, i had posted a link list program doing exactly this like 4 months back or something, and there is one of the gurus here said that it was a wiered way of doing what i was trying to..but now i cant find his solution, do u think, any otherway would be better and also, now you are assuming by this that the class which would be passed tot he store have an overloaded = operator right?

    thanx again,

    Ankur

  4. #4
    Join Date
    Jul 2002
    Location
    St. Louis, MO
    Posts
    484
    I think the use of the template in this manner is a little confusing.
    PHP Code:
    template <class obj>
    class 
    Store
    {
    private:
      
    objdata;
    };

    //So if I declare this:
    Store<doublemyDouble;
    //I expect that my object will contain a double, when in
    //fact, it contains a pointer to a double.

    //And if I:
    Store<CMyObject*> myObj;
    //I expect to have stored a pointer to my object, when in
    //fact, I have CMyObject**. 
    Depending upon the design of the class, I could see where it would be misleading. It also introduces some unneeded complexity. Take for instance:
    PHP Code:
    template <class obj>
    class 
    Store
    {
    private:
      
    objdata;

    public:
        
    objValue()
        {
            
    //if we didn't store a pointer to the template object
            //we would not need the (*) here
            
    return *obj;
        }
    }; 
    I don't really see how anything is gained by using a pointer to the template object. It should be the programmers discretion as to whether or not to use a pointer or an actual object.

  5. #5
    Join Date
    Mar 2002
    Location
    California
    Posts
    1,582
    HeartBreakKid has a good point. Usually, the internal representation of class data does not matter to the user of that class. However with templates, it often does, especially when your dealing with containers. When it's a problem of data structures, which containers answer, the internal representation becomes important.

    It would be better to store it exactly as it's handed to you. If the use of Store wants to store a pointer, he/she can use "Store<MyClass*> classStore;"

    Jeff

  6. #6
    Join Date
    Oct 2001
    Location
    Chicago, USA
    Posts
    222
    Hi! I understand where you going from here, it makes sense..may be i will try that, thanx..

    Ankur

  7. #7
    Join Date
    Oct 2001
    Location
    Chicago, USA
    Posts
    222
    Ok Now in this, if i use it how you are saying i should..What do i assume in my code, do I assume that the obj the programmer would pass would have an overloaded = operator or do i do a memcpy with the sizeof(obj). Also if the person does:
    Store<myClass*>

    how would i do the copy now, cuz this would just copy the address and not the physical data, in this case do i assume its the users responsibility?

    Also when the user writes his own code like this:

    Store<myclass*> dptr;

    dptr.Add(new myclass("whatever")); //assuming Add is function.

    then wouldnt this lead to memory leak, cuz who would clean this allocated memory?

    Please help!

    thanx,

    Ankur
    Last edited by ankursaxena; September 30th, 2002 at 05:01 PM.

  8. #8
    Join Date
    Apr 1999
    Posts
    27,449
    Originally posted by ankursaxena
    Ok Now in this, if i use it how you are saying i should..What do i assume in my code, do I assume that the obj the programmer would pass would have an overloaded = operator or do i do a memcpy with the sizeof(obj). Also if the person does:
    Store<myClass*>

    how would i do the copy now, cuz this would just copy the address and not the physical data, in this case do i assume its the users responsibility?

    Also when the user writes his own code like this:

    Store<myclass*> dptr;

    dptr.Add(new myclass("whatever")); //assuming Add is function.

    then wouldnt this lead to memory leak, cuz who would clean this allocated memory?

    Please help!

    thanx,

    Ankur
    The responsibility should be with the user. A competent C++ programmer would and should know that a class that uses pointers to dynamic memory need correct and sensible copy constructors to be coded. If they want to store pointers, that's their business, not yours. If the program blows up, it's the user's fault. You can't baby sit the users -- they should at least know that pointers to dynamic memory is something that they should be responsible for.

    If you want an example, take the std::vector class. You can store pointers or objects, vector doesn't care. When you copy an element, you invoke the item's copy constructor, whatever that may be. If the object being stored uses pointers to dynamic memory, and proper copy constructors are not coded, all kinds of problems will occur. It is not the fault of vector that the program blows up when items are removed, moved, or copied.

    Regards,

    Paul McKenzie

  9. #9
    Join Date
    Oct 2001
    Location
    Chicago, USA
    Posts
    222
    Ok! I understand, yeah it makes sense..let him do the cleaning..

    Cool..

    Thanx

    Ankur

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured