N
Nagrik
Hello Group,
How do I initialize a char* data member, if the parameter is also the
same as
that of data member.
class A {
private:
int id;
double pay;
char* name;
public:
A(int id, double pay, char* name ) : id(id), pay(pay) // OK
// Can not initialize name since memory is
not allocated
{
this->name = new char[strlen(name) + 1]; // Can not use "this"
pointer
// because "this" pointer will only exist after the constructor is
called.
}
};
Thanks.
nagrik
How do I initialize a char* data member, if the parameter is also the
same as
that of data member.
class A {
private:
int id;
double pay;
char* name;
public:
A(int id, double pay, char* name ) : id(id), pay(pay) // OK
// Can not initialize name since memory is
not allocated
{
this->name = new char[strlen(name) + 1]; // Can not use "this"
pointer
// because "this" pointer will only exist after the constructor is
called.
}
};
Thanks.
nagrik