STL problem...

This is a discussion on STL problem... within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm from Taiwan and pretty new to using STL. I would describe the problems I encountered as possible as ...

  1. #1
    David
    Join Date
    May 2004
    Posts
    2

    STL problem...

    Hi, I'm from Taiwan and pretty new to using STL. I would describe the problems I encountered as possible as I could, and hope you guys to give me some advise.

    Here, I have three classes. One is the base class of the other two. The following is something about the classses.


    Code:
    class Layer
    {
    public:
        Layer(const string vstrName, const double vdHeight);
    
        const string GetName() const;
        const double GetHeight() const;
        Layer& operator=(const Layer& rlayer);
        const bool operator<(const Layer& rlayer) const;
        const bool operator==(const Layer& rlayer) const;
        const bool operator!=(const Layer& rlayer) const;
    
    protected:
        string _strName;    // name of layer
        double _dHeight;    // height of layer
    };
    
    class ConLayer: public Layer
    {
    public:
        ConLayer(const string vstrName, const double vdHeight, const doubl
    
        const double GetVolt() const;
        ConLayer& operator=(const ConLayer& rconlayer);
    
    private:
        double _dVolt;      // volt of conductor layer
    };
    
    class InsuLayer: public Layer
    {
    public:
        InsuLayer(const string vstrName, const double vdHeight, const doub
    
        const double GetDiel() const;
        InsuLayer& operator=(const InsuLayer& rinsulayer);
    
    private:
        double _dDiel;      // dielectic constant of insulator layer
    };
    Code:
    ///////////////////////////////////////////
    // Class Layer member function declariation
    
    Layer::Layer(const string vstrName, const double vdHeight):
    _strName(vstrName), _dHeight(vdHeight)
    { }
    
    const string Layer::GetName() const
    {
        return _strName;
    }
    
    const double Layer::GetHeight() const
    {
        return _dHeight;
    }
    
    Layer& Layer::operator=(const Layer& rlayer)
    {
        _strName = rlayer._strName;
        _dHeight = rlayer._dHeight;
    
        return *this;
    }
    
    const bool Layer::operator<(const Layer& rlayer) const
    {
        return this->_strName < rlayer._strName;
    }
    
    const bool Layer::operator==(const Layer& rlayer) const
    {
        return this->_strName == rlayer._strName;
    }
    
    const bool Layer::operator!=(const Layer& rlayer) const
    {
        return this->_strName != rlayer._strName;
    }
    
    // Class Layer member function declariation
    ///////////////////////////////////////////
    
    //////////////////////////////////////////////
    // Class ConLayer member function declariation
    
    ConLayer::ConLayer(const string vstrName, const double vdHeight, const double
    Layer(vstrName, vdHeight), _dVolt(vdVolt)
    { }
    
    const double ConLayer::GetVolt() const
    {
        return _dVolt;
    }
    
    ConLayer& ConLayer::operator=(const ConLayer& rconlayer)
    {
        Layer::operator=(rconlayer);
        _dVolt = rconlayer._dVolt;
        return *this;
    }
    
    // Class ConLayer member function declariation
    //////////////////////////////////////////////
    
    ///////////////////////////////////////////////
    // Class InsuLayer member function declariation
    
    InsuLayer::InsuLayer(const string vstrName, const double vdHeight, const doubl
    Layer(vstrName, vdHeight), _dDiel(vdDiel)
    { }
    
    const double InsuLayer::GetDiel() const
    {
        return _dDiel;
    }
    
    InsuLayer& InsuLayer::operator=(const InsuLayer& rinsulayer)
    {
        Layer::operator=(rinsulayer);
        _dDiel = rinsulayer._dDiel;
        return *this;
    }
    
    // Class InsuLayer member function declariation
    ///////////////////////////////////////////////
    Then, I use set container to do something I want. Then comes the following.

    Code:
    string __strLayerName;  // name for conductor or insulator layer
    set<ConLayer> __setConLayer;
    set<InsuLayer> __setInsuLayer;
    
    /////////////////////////////////////////////////
    // brief:       save the layer name for later use
    // return:      none
    
    void set_layer_name(const char* pcharName)
    {
        __strLayerName = pcharName;
    }
    
    ///////////////////////////////////////////////////////////////////
    // brief:       create a conductor layer based on given information
    // return:      none
    
    void set_con_layer(const double vdHeight, const double vdVolt)
    {
        __setConLayer.insert( ConLayer(__strLayerName, vdHeight, vdVolt) );
    }
    
    ///////////////////////////////////////////////////////////////////
    // brief:       create a insulator layer based on given information
    // return:      none
    
    void set_insu_layer(const double vdHeight, const double vdDiel)
    {
         __setInsuLayer.insert( InsuLayer(__strLayerName, vdHeight, vdDiel) );
    }
    I don't have any error or warning in compiling and linking. But I don't get what I expected when ddd (debug tool) is employed. That is, after insert operation, instead of a duplicate object of ConLayer/InsuLayer, I got a object in set with initial values which are set in constructor.

    I have done much efforts to find what's wrong and got nothing. Could someone give some suggestion? Or some information like website could give me some hints?

    Thanks in advance.

    David

  2. #2
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    Code:
    ConLayer::ConLayer(const string vstrName, const double vdHeight, const double
    Layer(vstrName, vdHeight), _dVolt(vdVolt)
    { }
    
    InsuLayer::InsuLayer(const string vstrName, const double vdHeight, const doubl
    Layer(vstrName, vdHeight), _dDiel(vdDiel)
    { }
    I don't think you can call a constructor from a parent class if you use public inheritance because the constructor is not inherited

  3. #3
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Quote Originally Posted by skorman00
    I don't think you can call a constructor from a parent class if you use public inheritance because the constructor is not inherited
    Yes you can.
    A subclass can access every non-private member of the base class, regardless of the type of inheritance.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  4. #4
    David
    Join Date
    May 2004
    Posts
    2
    Another question. It seemed insert never invokes overloaded operator = which I defined in classes. Is it correct?

    Thanks in advance.

    David

  5. #5
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    One solution is a copy constructor.

    Kuphryn

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    Yes, all of these STL containers set, multiset, vector, list, map, multimap and many more use a copy constructor to copy the object being pushed/inserted into the container. If you don't have one defined, the default copy constructor will be used instead to copy elements which may not behave in the manner you expect. So, make a copy constructor for all of your classes, that should help out.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  2. searching problem
    By DaMenge in forum C Programming
    Replies: 9
    Last Post: 09-12-2005, 02:04 AM
  3. STL vector <T> problem
    By correlcj in forum C++ Programming
    Replies: 11
    Last Post: 11-06-2002, 07:18 PM
  4. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 09:37 AM
  5. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 06:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21