Ok first of all i would just like to say that dynamic memory is evil!
This program that i am making wont work and I have written a simpler one to demonstrate my problem.
It looks like when I try to use the copy constructor in the manner:
object1 = object2;
I get errors concerning dynamic memory yet when i do this:
class object2 = object1;
it works just fine??
my code is below:
This program that i am making wont work and I have written a simpler one to demonstrate my problem.
It looks like when I try to use the copy constructor in the manner:
object1 = object2;
I get errors concerning dynamic memory yet when i do this:
class object2 = object1;
it works just fine??
my code is below:
Rich (BB code):
#include <iostream>
#include <cstring>
using namespace std;
class header
{
public:
header();
header (char *);
header (const header &);
~header();
char * reader();
void input(char *);
private:
char *ptr;
};
int main()
{
header obj1("hello");
cout << obj1.reader() << endl;
obj1.input("goodbye");
cout << obj1.reader() << endl;
header obj2;
obj2 = obj1;
return 0;
}
header::header()
{
ptr = new char[40];
strcpy(ptr,"empty");
}
header::header(char *input)
{
ptr = new char[40];
strcpy(ptr,"empty");
}
header::~header()
{
delete [] ptr;
}
header::header(const header &obj)
{
ptr = new char[40];
strcpy(ptr,obj.ptr);
}
char * header::reader()
{
return ptr;
}
void header::input(char * in)
{
strcpy(ptr,in);
}
Last edited: