[Tree-hh] Help needed with a copy constructor.

Stefan Koshiw stefankoshiw306 at gmail.com
Thu Dec 3 18:46:15 GMT 2009


Hi

i'm trying build a class representing a Genome that contains a tree as a
data member, i'm declaring the tree in the header file and instantiating it
in the constructors.
My problem lies in how i could build a copy constructor for this. i've
racked my brains fiddled with a multitude of different pointer constructions
but to no avail. i would greatly appreciate a bit of help here.

I can't figure out how i would copy the tree data pointed to by the tree
root pointer.

Thank you

Stefan


Genome.h
-------------------------
class Genome
{
private:
    int m_Identity, m_Generation;
    tree<string> *tr;  //Tree root
    tree<string>::iterator top, loc, LSystem;
    string m_Phenotype;
    double m_Fitness;

public:

    //Copy Constructor
    Genome(const Genome &p_Genome);

    //Assignment Constructor
    Genome &operator= (const Genome &p_Genome);

    tree<string> *GetTreeRoot (void) const;


... the rest includes other constructors, the destructor, accessor and
mutator definitions.


Genome.cpp
------------------------------------------------

Genome::Genome(const Genome &p_Genome)
{
    tr = new tree<string> ;

    tr = p_Genome.GetTreeRoot();
    m_Identity = p_Genome.GetIdentity();
    ...
}

Genome &Genome::operator=(const Genome &p_Genome)
{
    if(&p_Genome != this)
    {
        delete tr;
        tr = new tree<string>;
        tr = p_Genome.GetTreeRoot();
        ...
    }
    return *this;
}

tree<string>* Genome::GetTreeRoot() const
{
    return tr;
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.hepforge.org/lists-archive/tree-hh/attachments/20091203/9cee3b1b/attachment.htm 


More information about the Tree-hh mailing list