delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/02/27/05:57:13

From: T DOT Mcdonald AT uea DOT ac DOT uk (Terry McDonald)
Newsgroups: comp.os.msdos.djgpp
Subject: destructors for C++ classes.
Date: Sat, 27 Feb 1999 10:36:59 GMT
Organization: University of East Anglia, Norwich, Norfolk, NR47TJ, UK
Lines: 75
Message-ID: <36d7c7dc.49356683@news.uea.ac.uk>
NNTP-Posting-Host: d36.dialup.uea.ac.uk
X-Newsreader: Forte Free Agent 1.11/32.235
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

I have written the following class which works perfictly ;-)

#include<vector>
#include<string>

class Parse
{
private:
        vector<string> words;
public:
        Parse(string phrase);
        int numWords(void);
        string operator[](int elem);
        //~Parse();
};

I am using it in a program with a loop simular to this...

int main(void)
{
    string sentance;
    int num;

    for (int x = 0; x < 5; x++)
    {
        cin >> sentance;
        Parse xyzzy(sentance);
        for (int y = 0; y < xyzzy.numWords(); y++)
        {
                cout << xyzzy[y] << endl;
        }
        //~xyzzy;
    }

    return 0;
}

I want to destruct the xyzzy at the end of every x loop before it is
re-constructed to parse the next senatance entered.  Every time I try
to write a destructor I get errors.  I've tryed the following:

Parse::~Parse
{
	~words;
}

Parse::~Parse
{
	delete [] words;
}

Parse::~Parse
{
	delete words[];
}

Parse::~Parse
{
	delete [] words [];
}

Parse::~Parse
{
	for (int loop = 0; loop < words.size(); loop++)
	{
		delete words[loop];
	}
}

none of the above work.  If anyone knows the answer please help!

Thanx in advance.

Terry.

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019