Hi there. Pointers confused me till the first day I used them (1 year ago) and still can't understand them 100%.
My problem is: I've read from a book that when you have
a class with a char* member you must create your own copy constructor,allocate memory dynamically...
but this works fine
#include <iostream>
using namespace std;
class Name
{
private:
char* fname;
public :
Name(char* k="no name"){fname=k;}
~Name(){delete [] fname;}
void print(void) {cout<<fname<<endl;}
};
int main()
{
Name name("alex"),Obj(name);
name.print();
Obj.print();
Obj.~Name();
name.print();
getchar();
return 0;
}
unless I change constructor's body with
{fname=new char[strlen(k)+1];
strcpy(fname,k);}
What is finally going on? How confusing pointers could be?