D
dragoncoder
Hello experts,
I have the following code me.
=> cat mystring.h
#include<iostream>
using namespace std;
class mystring
{
char* p;
int capacity;
int len;
enum {DEF_SIZE = 100};
public:
// Constructors
mystring();
mystring(int size);
mystring(const char* str);
// Iniializer
void init(int size = DEF_SIZE);
//Destructor
~mystring();
//Copy Constructor
mystring(mystring&);
//Assignment Operator
mystring& operator = (mystring&);
//Display String
void display();
};
=> cat mystring.cpp
#include<iostream>
using namespace std;
#include "mystring.h"
//Constructors
mystring::mystring()
{
cout << "Default Constructor" << endl;
init();
len = 0;
}
mystring::mystring(int size)
{
cout << "Constructor having size" << endl;
init(size);
len = 0;
}
mystring::mystring(const char* str)
{
cout << "Constructor with string argument" << endl;
int length = strlen(str);
init(length + 1);
len = length + 1;
strcpy(p, str);
}
void mystring::init(int size)
{
p = new char[size];
capacity = size;
}
mystring::~mystring()
{
cout << "Destructor" << endl;
delete p;
p = NULL;
}
mystring::mystring(mystring& other)
{
cout << "Copy Constructor" << endl;
delete p;
p = new char[strlen(other.p) + 1];
strcpy(p,other.p);
}
mystring& mystring:perator=(mystring& other)
{
cout << "Assignment Operator" << endl;
if(this == &other)
return *this;
delete p;
p = new char[strlen(other.p)+1];
strcpy(p,other.p);
return *this;
}
void mystring::display()
{
cout << *p << endl;
cout << "Length of string is " << len << endl;
cout << "Capacity of the string is " << capacity << endl;
}
=> cat str.cpp
#include<iostream>
using namespace std;
#include "mystring.h"
int main()
{
mystring s = "Nitin";
return 0;
}
Compiling this gives error.
=> g++ mystring.cpp str.cpp
str.cpp: In function `int main()':
str.cpp:7: no matching function for call to
`mystring::mystring(mystring)'
mystring.h:24: candidates are: mystring::mystring(mystring&)
mystring.h:15: mystring::mystring(const char*)
mystring.h:14: mystring::mystring(int)
str.cpp:7: initializing temporary from result of
`mystring::mystring(const
char*)'
Changing the parameter of the copy constructor to "const mystring&"
solves the problem. Can someone please explain how it makes a
difference ?
Thanks in advance.
I have the following code me.
=> cat mystring.h
#include<iostream>
using namespace std;
class mystring
{
char* p;
int capacity;
int len;
enum {DEF_SIZE = 100};
public:
// Constructors
mystring();
mystring(int size);
mystring(const char* str);
// Iniializer
void init(int size = DEF_SIZE);
//Destructor
~mystring();
//Copy Constructor
mystring(mystring&);
//Assignment Operator
mystring& operator = (mystring&);
//Display String
void display();
};
=> cat mystring.cpp
#include<iostream>
using namespace std;
#include "mystring.h"
//Constructors
mystring::mystring()
{
cout << "Default Constructor" << endl;
init();
len = 0;
}
mystring::mystring(int size)
{
cout << "Constructor having size" << endl;
init(size);
len = 0;
}
mystring::mystring(const char* str)
{
cout << "Constructor with string argument" << endl;
int length = strlen(str);
init(length + 1);
len = length + 1;
strcpy(p, str);
}
void mystring::init(int size)
{
p = new char[size];
capacity = size;
}
mystring::~mystring()
{
cout << "Destructor" << endl;
delete p;
p = NULL;
}
mystring::mystring(mystring& other)
{
cout << "Copy Constructor" << endl;
delete p;
p = new char[strlen(other.p) + 1];
strcpy(p,other.p);
}
mystring& mystring:perator=(mystring& other)
{
cout << "Assignment Operator" << endl;
if(this == &other)
return *this;
delete p;
p = new char[strlen(other.p)+1];
strcpy(p,other.p);
return *this;
}
void mystring::display()
{
cout << *p << endl;
cout << "Length of string is " << len << endl;
cout << "Capacity of the string is " << capacity << endl;
}
=> cat str.cpp
#include<iostream>
using namespace std;
#include "mystring.h"
int main()
{
mystring s = "Nitin";
return 0;
}
Compiling this gives error.
=> g++ mystring.cpp str.cpp
str.cpp: In function `int main()':
str.cpp:7: no matching function for call to
`mystring::mystring(mystring)'
mystring.h:24: candidates are: mystring::mystring(mystring&)
mystring.h:15: mystring::mystring(const char*)
mystring.h:14: mystring::mystring(int)
str.cpp:7: initializing temporary from result of
`mystring::mystring(const
char*)'
Changing the parameter of the copy constructor to "const mystring&"
solves the problem. Can someone please explain how it makes a
difference ?
Thanks in advance.