| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Trouble with destructors in class
Ive been working on this code for quite a while and now I come for help with my difficulties .
This is part of the driver program to be used. Code:
void read_poly( Poly & p )
{
Term t;
char ch, var;
int coef, exp;
while ( ( ch = cin.get() ) && ( ch != '\n' ) )
{
if ( ch == '+' )
{
cin >> coef >> var >> exp;
t.set ( coef, var, exp );
p += t;
}
else if ( ch == '-' )
{
cin >> coef >> var >> exp;
t.set ( -1 * coef, var, exp );
p += t;
}
}
}
This is what I have been encountering problems with. Code:
// input: A Term t
// output: none
// return: returns a pointer to this
// notes: This function assigns the polynomial the term that was sent to it. However, the term that is written to the polynomial overwrites the old term that was stored in the poly(this).
Poly Poly::operator+= (const Term &t)
{
if(this->used < this->size)
{
*this[used].terms = t; //assigns this the value of t's items
// ((doesnt work as expected))
this->used++; //increases the amount of terms used
}
else
{
grow(*this); //grow the array to a larger size
*this->terms = t; //same as above
this->used++;
}
return *this;
}
///// This is my grow() function that should(but doesnt necessarily) increase the size of the polynomial by 5 in order to hold additional terms, replace the polynomial with a new poly of size+5, keeping the old terms. This function also should delete [] the old array of polynomials in order to prevent memory leakage. ///// Code:
// input: The Polynomial P gets passed to this function
// output: none
// return: a new array with a new size to allow more elements to be put in
// notes: This function takes an array and expands it by 5 and
// deletes the old array
Poly *grow(Poly &p)
{
int newsize = p.size + 5; //declaring new size of old size + 5
Poly *narr = new Poly[newsize];//allocates new memory location of newsize
for(int i = 0;i < p.size;++i)//loop to assigne values
narr[i] = p;
cerr << "eh?";
delete [] &p; //delete old array -- does not work - causes seg fault
narr->size = newsize;
return narr;
}
and for my class definitions : Code:
/////////////////////////////
///Polynomial class definition
//////////////////////////////
class Poly
{
public:
Poly(void); //constructor
~Poly(void); //destructor
Poly(const Poly &p);
friend class Term;
void copy(const Poly &p); //copy values
friend Poly *grow(Poly &p);
Poly operator+= (const Term &t);
Poly operator+ (const Poly &p)const;
Poly operator- (const Poly &p)const;
Poly operator* (const Poly &p)const;
Poly operator= (const Poly &p);
friend ostream& operator<< (ostream &streamout, const Poly &p);
private:
Term * terms; //Term values
int used; //terms in use
int size; //size of poly array
};
Code:
///////////////////////
/////Term class definition
//////////////////////
class Term
{
friend class Poly;
public:
Term(void);
int getcoef(void)const;
int getexp(void)const;
char getvar(void)const;
void set(int = 0, char = 'x', int = 0);
friend ostream& operator<< (ostream &streamout, const Term &t);
private:
int coef;
char var;
int exp;
};
any help as to how to prevent the old terms from being deleted, delete the old array properly in grow function, etc would be greatly appreciated |
|
#2
|
|||
|
|||
|
Dyn Mem -vs - Vector
This problem of memory allocation and growth appears more readily managed with a vector. you can add terms without concern for lost goodies and you don't have to mangage the memory leak. Otherwise your work looks good to this point.
|
|
#3
|
|||
|
|||
|
i don't know if you can really delete[] addresses-of-references...what if you passed a pointer-to-Poly instead?
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Trouble with destructors in class |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|