Re: Copy Constructor
On Jul 30, 8:25 am, Roy <royash...@gmail.com> wrote:
On Jul 30, 9:59 am, "int2...@gmail.com" <int2...@gmail.com> wrote:
On Jul 29, 9:38 pm, Roy <royash...@gmail.com> wrote:
On Jul 30, 9:24 am, Ian Collins <ian-n...@hotmail.com> wrote:
Snipped previous iterations.
heres what i get :-(
I have understood The NULL thing , i have modified the code to
#include<iostream>
using namespace std;
class cpchk{
private:
char* name;
int num;
public:
// Methods for name
char* getname()const{
cout<<"Address:Name>>"<<&name<<endl;
Here you are not printing the content of the pointer, but the address
of the pointer. This will not change during the live of the project.
Change this too (for example):
cout<<"Address:Name>>"<< hex << (long)name<<endl;
Better:
std::cout << "Address::Name>> :"
<< static_cast< void* >( name )
<< std::endl ;
Don't forget to include <iomanip> for "hex".
return name;
}
void setname(const char* acname){
if(name==NULL)
name=(char*)malloc((strlen(acname)+1)*sizeo=
f(char));
In C++, you'd generally use "new" here, not "malloc".
In C++, you'd generally use std::string, and be done with it:-).
But yes, the new operator should definitly be preferred to
malloc. (One might also add that sizeof(char) is guaranteed to
be one.)
else{
free(name);
name=NULL;
name=(char*)malloc((strlen(acname)+1)*sizeo=
f(char));
}
The if/else logic here can be simplified and redundant code can be
removed.
The if/else logic can be completely removed. There's no need
for the if, nor setting name to NULL:
delete [] name ;
name = new char[ strlen( acname ) + 1 ] ;
(With std::string, of course, the entire function becomes
simply:
name = acname ;
..)
You think about how - ask if you need to.
strcpy(name,acname);
}
// Methods for num
int getnum()const{
cout<<"Address:Num>>"<<&num<<endl;
return num;
}
void setnum(const int inum){
num=inum;
}
// Constructors
cpchk(){
cout<<"Constructor called"<<endl;
name=NULL;
num=0;
Lookup "initialization lists" in this newsgroup.
Or any good C++ book.
}
cpchk(char* acname,int inum){
name=(char*)malloc((strlen(acname)+1)*sizeof(char));
strcpy(name,acname);
}
inum is not used in this function.
// Destructor
~cpchk(){
cout<<"Address:Object>>"<<this<<endl;
cout<<"Destructor called"<<endl;
cout<<"Address:Name>>"<<&name<<endl;
Same problem here. You're displaying the address of the pointer, not
it's contents (what it is pointing too).
cout<<"Address:Num>>"<<&num<<endl;
if(name!=NULL)
free(name);
You can use "delete []" here if you use "new" to allocate the string.
name=NULL;
}
};
Most obviously, there's no assignment operator.
I would strongly recommend that the poster acquire a copy of
Scott Meyer's "Effective C++", and read it carefully.
2.
I have read that even in C++ we use malloc and free for primitive data
types . Is it wrong ?I guess i should search ?
I never use malloc and free in C++, except when implementing a
global operator new and operator delete replacement. Malloc and
free allocate raw memory, and are not type safe.
3.
Yes i know about the Initialization lists , but am unsure if i can
use them to bypass new/malloc ?
They don't bypass anything. For pointers, there's absolutely no
difference between:
MyClass::MyClass()
: somePointer( new Whatever[ howmany ] )
{
}
and:
MyClass::MyClass()
{
somePointer = new Whatever[ howmany ] ;
}
Except that in the first case, there is no way that later
modification can accidentally access an uninitialized pointer.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34