Click to See Complete Forum and Search --> : Template, class - data member copy problem


ankursaxena
September 30th, 2002, 11:32 AM
Hi! I have a template class say Store defined like :


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

proxima centaur
September 30th, 2002, 12:20 PM
From the info you give me... here's how I would do it:

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


template <class obj>

class Store
{
private:
obj* data;

public:
Store()
{
data = NULL;
}

Store(Store& s)
{
// 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);
}

};

ankursaxena
September 30th, 2002, 12:53 PM
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

HeartBreakKid
September 30th, 2002, 01:31 PM
I think the use of the template in this manner is a little confusing.

template <class obj>
class Store
{
private:
obj* data;
};

//So if I declare this:
Store<double> myDouble;
//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:

template <class obj>
class Store
{
private:
obj* data;

public:
obj& Value()
{
//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.

jfaust
September 30th, 2002, 03:58 PM
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

ankursaxena
September 30th, 2002, 04:00 PM
Hi! I understand where you going from here, it makes sense..may be i will try that, thanx..

Ankur

ankursaxena
September 30th, 2002, 04:58 PM
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

Paul McKenzie
September 30th, 2002, 10:00 PM
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

ankursaxena
September 30th, 2002, 10:04 PM
Ok! I understand, yeah it makes sense..let him do the cleaning..

Cool..

Thanx

Ankur