Reputation: 149
I need to create 2 classes, and second class must have pointer to first object as its member. My program works, but there are memory leaks, and I can't fix them. I know, that the problem is in constructor or destructor, but I don't know how to fix it. Here is my code:
main.cpp
#include "classes.h"
int main() {
COne firstObj;
CTwo secondObj;
firstObj.setN(10);
firstObj.setS("candies");
firstObj.print();
secondObj.setS("cookies");
secondObj.setP(&firstObj);
cout<<endl;
secondObj.print();
return 0;
}
classes.h
#ifndef CLASSES_H
#define CLASSES_H
#include <iostream>
#include <string>
using namespace std;
class COne {
private:
int n;
string sOne;
public:
COne(int n2create = 0, const string& sOne2create = "");
COne(const COne& obj);
~COne();
COne& operator=(const COne& obj);
void setN(int n2set);
void setS(const string &sOne2set);
int getN() const;
string getS() const;
void print() const;
};
class CTwo {
private:
string sTwo;
COne* p;
public:
CTwo(string sTwo2create = "", COne& p2create = *(new COne()));
CTwo(const CTwo& obj);
~CTwo();
CTwo& operator=(const CTwo& obj);
void setS(string sTwo2set);
void setP(COne* p2set);
string getS() const;
COne* getP() const;
void print() const;
};
#endif
classes.cpp
#include "classes.h"
COne::COne(int n2create, const string& sOne2create) :n(n2create), sOne(sOne2create) {}
COne::COne(const COne& obj) :n(obj.n), sOne(obj.sOne) {}
COne::~COne() {}
COne& COne::operator=(const COne& obj){
n = obj.n;
sOne = obj.sOne;
return *this;
}
void COne::setN(int n2set) {n = n2set;}
void COne::setS(const string& sOne2set) {sOne = sOne2set;}
int COne::getN() const {return n;}
string COne::getS() const {return sOne;}
void COne::print() const {
cout<<"COne object:"<<endl;
cout<<"n = "<<n<<endl;
cout<<"sOne = \""<<sOne<<"\""<<endl;
}
CTwo::CTwo(string sTwo2create, COne& p2create) :sTwo(sTwo2create), p(new COne(p2create)) {}
CTwo::CTwo(const CTwo& obj) :sTwo(obj.sTwo), p(new COne (*obj.p)){}
CTwo::~CTwo() {delete p;}
CTwo& CTwo::operator=(const CTwo& obj){
sTwo = obj.sTwo;
p = obj.p;
return *this;
}
void CTwo::setS(string sTwo2set) {sTwo = sTwo2set;}
void CTwo::setP(COne* p2set) {p = p2set;}
string CTwo::getS() const {return sTwo;}
COne* CTwo::getP() const {return p;}
void CTwo::print() const {
cout<<"CTwo object:"<<endl;
cout<<"sTwo = \""<<sTwo<<"\""<<endl;
cout<<"p: n = "<<p->getN()<<", sOne = \""<<p->getS()<<"\""<<endl;
}
Upvotes: 0
Views: 1219
Reputation: 76298
For one, in:
COne& p2create = *(new COne())
You have a memory leak if the argument is default constructed. This is because p2create
is a reference to a dynamically allocated object and you then copy it into another dynamically allocated object only to store the latter and never free the former.
Specifically in:
CTwo::CTwo(string sTwo2create, COne& p2create) :sTwo(sTwo2create), p(new COne(p2create)) {}
Your code contains other errors of course. To solve them all, just rewrite your classes to:
struct COne {
int n;
std::string s;
void print() const {
std::cout << "COne object:\n"
<< "n = " << n << '\n'
<< "s = \"" << s << "\"\n";
}
};
struct CTwo {
std::string sTwo;
COne p;
void print() const {
std::cout << "CTwo object:\n"
<< "sTwo = \"" << sTwo << "\"\n"
<< "p: n = "<< p.n << ", s = \"" << p.s <<"\"\n";
}
};
which are functionally equivalent, and then use them as:
int main() {
COne firstObj {10, "candies"};
firstObj.print();
std::cout << '\n';
CTwo secondObj {"cookies", firstObj};
secondObj.print();
return 0;
}
Upvotes: 1
Reputation: 2096
void CTwo::setP(COne* p2set) {p = p2set;}
Saves the new pointer, overwriting the previous value of p without deleting any previously allocated memory assigned to p.
Upvotes: 0