Copy Constructor

T

terminator

#include<iostream>
using namespace std;

class cpchk{
private:
char* name;
int num;
public:
// Methods for name
char* getname()const{
cout<<"Address:Name>>"<<&name<<endl;

****wait a minute****.try this:
cout<<"name string
adress>>"<<( (void*)name )<<endl;

remmember that 'name' is a pointer to 'char's who stores the address
of a 'char' ,but meanwhile it is a variable and every variable has an
address which is indepent from its value and is retrived via the unary
'&' operator.

and why do you prefer malloc/free over new/delete?
name=(char*)malloc((strlen(acname)+1)*sizeof(char));
try this:

name= new char[strlen(acname)+1];//much simpler
free(name);
instead do this:

delete name;//invokes destructor prior to remove from heap

regards,
FM
 
R

Roy

#include<iostream>
using namespace std;
class cpchk{
private:
char* name;
int num;
public:
// Methods for name
char* getname()const{
cout<<"Address:Name>>"<<&name<<endl;

****wait a minute****.try this:
cout<<"name string
adress>>"<<( (void*)name )<<endl;

remmember that 'name' is a pointer to 'char's who stores the address
of a 'char' ,but meanwhile it is a variable and every variable has an
address which is indepent from its value and is retrived via the unary
'&' operator.

and why do you prefer malloc/free over new/delete?
name=(char*)malloc((strlen(acname)+1)*sizeof(char));

try this:

name= new char[strlen(acname)+1];//much simpler
free(name);

instead do this:

delete name;//invokes destructor prior to remove from heap

regards,
FM

Thanks Guys shall remember the tips that i got in here :)
 
T

Thomas J. Gritzan

terminator said:
and why do you prefer malloc/free over new/delete?

Because then he won't mix up new and new[]. :)
name=(char*)malloc((strlen(acname)+1)*sizeof(char));
try this:

name= new char[strlen(acname)+1];//much simpler
free(name);
instead do this:

delete name;//invokes destructor prior to remove from heap

Must be:
delete[] name;

I strongly suggest std::string and std::vector<>.
 
T

terminator

terminator said:
and why do you prefer malloc/free over new/delete?

Because then he won't mix up new and new[]. :)
try this:
name= new char[strlen(acname)+1];//much simpler
free(name);
instead do this:
delete name;//invokes destructor prior to remove from heap

Must be:
delete[] name;

damn me.you did knock me down.
I strongly suggest std::string and std::vector<>.

the discussion is all about pointers.Your suggestion is safer, but if
one wants to learn ,he must take the risk .

regards,
FM.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,271
Latest member
BuyAtenaLabsCBD

Latest Threads

Top