copy constructor, and destructor for n-ary tree

I have to write a copy constructor and a destructor for a n-ary tree.

Here is the .hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#ifndef INCLUDES_ASTree_HPP_
#define INCLUDES_ASTree_HPP_

#include <list>
#include <vector>
#include <iostream>
#include <iomanip>
#include <cassert>
#include <algorithm>

class      ASTree;
enum       nodes {category, token, whitespace};
const bool TAGS  = false;

bool                     isStopTag (std::string);
std::string              readUntil (std::istream&, char);
std::string              unEscape  (std::string);
std::vector<std::string> tokenize  (const std::string& s);


////////////////////////////////////////////////////////////////////////
// An ASTree is either a: 
//     -Syntactic category node
//     -Token node
//     -Whitespace node
//
// CLASS INV: if (nodeType == category) than (child != 0) && (text == "")
//            if ((nodeType == token) || (nodeType == whitespace)) then (child == 0) && (text != "")
//
class ASTree {
public:
                  ASTree    () {};
                  ASTree    (nodes t) : nodeType(t) {};
                  ASTree    (nodes t, const std::string&);
                  ~ASTree   ()                      {}; //NEED TO IMPLEMENT                        
                  ASTree    (const ASTree&);
    void          swap      (ASTree&);
    ASTree&       operator= (ASTree);
    ASTree*       copyASTree();
    ASTree*       getChild  (std::string);
    std::string   getName   () const;
    
    void          mainHeader(std::vector<std::string>&);
    void          fileHeader(std::vector<std::string>&);
    void          mainReport(std::vector<std::string>&);
    void          funcCount (const std::string&);
    void          lineCount (const std::string&);
    std::ostream& print     (std::ostream&, int) const;
    std::istream& read      (std::istream&);
    
private:
    nodes               nodeType;       //Category or Token node  
    std::string         tag,            //Category: the tag name and 
                        closeTag;       //Closing tag.
    std::list<ASTree*>  child;          //Category: A list of subtrees.
    std::string         text;           //Token/whitespace: the text.
};


Here is what I got so far on the two :

copy constructor :
1
2
3
4
5
6
7
8
9
ASTree::ASTree(const ASTree& actual) {

  nodeType = actual.nodeType ;
  tag = actual.tag ;
  closeTag = actual.closeTag ;
  text = actual.text ;

  child = std::list<ASTree*> (acutal.child)
}


destructor :
1
2
3
~ASTree(){
delete child ;      //taking use of the list destructor
}



I was just wondering if this all would work out. Using the lists copy constructor and destructor. Leaving no leaked memories and copying all the children.
Last edited on

1
2
3
4
 std::list<ASTree*>  child;          // Not a pointer...
~ASTree(){
    delete child ;      // ...so why delete (if it is not new'ed)?
}


Also, are you intending/required to delete all children when killing an ASTree object? Because that would affect the object created from copy constructor too - it's child points to the same addresses, so it would end up with list full of dead pointers.
Last edited on
Topic archived. No new replies allowed.