You're right in that they are different but in your version you can't assign your class to be any more useful then a char, where as the origional would have cast it to a char and anything else. Also your version won't know what to do if I pass it a file stream for example.
It would be better if you took YOUR version of the constructor and turned it into a specilized template.
Whether the first is correct depends on what it's supposed to be used for.
Currently it has little use as it just stores a pointer that cannot be accessed.
Edit: that answer would have applied to ptr = src.ptr;*ptr = *(src.ptr); is definitely wrong.
Also, there's an error in your second constructor: you have to use delete[] instead of delete.
Well, as it is your template *should* cause an immediate run-time Segfault: you never allocate the ptr!
I think the templated version should be written like this:
1 2 3 4 5 6 7 8 9 10 11 12 13
template <typename T>
class A {
public:
A(const A &src)
{ this->ptr = new T;
*this->ptr = *(src.ptr);
}
~A(){deletethis->ptr; this->ptr=0;}
//I would also add the following constructor
A(){this->ptr=0;}
private:
T *ptr;
};
What is the point of this template?
Here is a template I found useful with similar looks to yours.
Aren't we getting a little off topic with the memory leaks guys? I agree they are important but it's pretty clear, to me at least, that the OP only wanted to know about the proper installation of a template class in case it was a char.
EDIT: I doubt this is even a functional class they would be using in the real world. How could it be?
Well, I think that it is semantically incorrect inconvenient (bordering with incorrect) to use copy constructors other than, well, for constructing things :). [Edit: Computergeek01 corrected me on this one]
For copying stuff around I always use operator= (overloaded). And of course, I try to be careful with the extremely annoying semantics ambiguity:
1 2 3
Rational tmp=0; //calls the constructor Rational(int)
Rational tmp;
tempRat2=0; //calls the overloaded operator Rational::operator=(int)
@ Thanz: I'm right here on the form so feel free to comment, PM me or start a thread of your own. I understand the idea that the individual attention may lead to a quicker solution to your questions but I can't even pretend to be the most informed person on this site.
@ tition: But there is a huge difference between those two. A copy constructor would create an new instance of an object where as the operator would copy data to an already existing instance. I guess I don't get what you are trying to say here.