problem with string&

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sernamar

    #1

    problem with string&

    Hi,
    I have the following base class

    class UnitOfMeasure
    {
    public:
    //...
    std::string& GetName() {return _uomName;};
    //...
    protected:
    std::string _uomName;
    //...
    };

    And the subclass

    class UomAngle : public UnitOfMeasure
    {
    public:
    UomAngle();

    UomAngle(const UnitOfMeasure &uom);

    virtual ~UomAngle();

    };

    When I create an UomAngle object (called radian) and use it, all works
    fine

    cout<<radian->GetName();

    Then, I have another base class

    class Measure
    {
    public:
    Measure();
    virtual ~Measure();

    double GetValue(){retu rn _value;};
    std::string& GetUomName(){re turn _uom->GetName();};

    protected:
    double _value;
    private:
    UnitOfMeasure *_uom;
    };

    And the subclass

    class Angle : public Measure
    {
    public:
    Angle(double value,const std::string& uomName);

    private:
    UomAngle *_uom;
    };

    And I have the following problems:

    1.- When I create an Angle and I want to get the uom name

    Angle a1(180,degree);
    cout<<a1.GetVal ue()<<"\t";
    cout<<a1.GetUom Name();

    it doesn't work. If I put the method

    std::string& GetUomName(){re turn _uom->GetName();};

    in the class Angle it works. But, why I have to do this? How can I do
    it work without putting GetUomName in each subclass of Measure?

    2.- The other problem is that

    Angle a1(180,degree);
    cout<<a1.GetVal ue()<<"\t";
    cout<<a1.GetUom Name();

    doesn't work because I have a problem with returning string&. I think
    that I have a problem because in

    std::string& GetUomName(){re turn _uom->GetName();};

    _uom->GetName() works like a local variable, and I'm returning a
    reference to this local variable.

    Any idea to solve those problems or more problems that I could have?
    Thanks!

  • amparikh@gmail.com

    #2
    Re: problem with string&amp;


    sernamar wrote:
    Hi,
    I have the following base class
    >
    class UnitOfMeasure
    {
    public:
    //...
    std::string& GetName() {return _uomName;};
    //...
    protected:
    std::string _uomName;
    //...
    };
    >
    And the subclass
    >
    class UomAngle : public UnitOfMeasure
    {
    public:
    UomAngle();
    >
    UomAngle(const UnitOfMeasure &uom);
    >
    virtual ~UomAngle();
    >
    };
    >
    When I create an UomAngle object (called radian) and use it, all works
    fine
    >
    cout<<radian->GetName();
    >
    Then, I have another base class
    >
    class Measure
    {
    public:
    Measure();
    virtual ~Measure();
    >
    double GetValue(){retu rn _value;};
    std::string& GetUomName(){re turn _uom->GetName();};
    >
    protected:
    double _value;
    private:
    UnitOfMeasure *_uom;
    };
    >
    And the subclass
    >
    class Angle : public Measure
    {
    public:
    Angle(double value,const std::string& uomName);
    >
    private:
    UomAngle *_uom;
    };
    >
    And I have the following problems:
    >
    1.- When I create an Angle and I want to get the uom name
    >
    Angle a1(180,degree);
    cout<<a1.GetVal ue()<<"\t";
    cout<<a1.GetUom Name();
    >
    it doesn't work. If I put the method
    >
    std::string& GetUomName(){re turn _uom->GetName();};
    >
    in the class Angle it works. But, why I have to do this? How can I do
    it work without putting GetUomName in each subclass of Measure?
    Probably because you are not initializing the resp instance member
    within Angle and Measure correctly ?
    Both seemed to have a pointer named _uom and maybe that is messing up
    stuff in your code(you have not posted all the code, so this is just a
    guess).
    >
    2.- The other problem is that
    >
    Angle a1(180,degree);
    cout<<a1.GetVal ue()<<"\t";
    cout<<a1.GetUom Name();
    >
    doesn't work because I have a problem with returning string&. I think
    that I have a problem because in
    >
    std::string& GetUomName(){re turn _uom->GetName();};
    >
    _uom->GetName() works like a local variable, and I'm returning a
    reference to this local variable.
    >
    Any idea to solve those problems or more problems that I could have?
    Thanks!

    Comment

    • Andre Kostur

      #3
      Re: problem with string&amp;

      "sernamar" <sernamar@gmail .comwrote in news:1163628427 .605768.296020
      @b28g2000cwb.go oglegroups.com:
      Hi,
      I have the following base class
      >
      class UnitOfMeasure
      {
      public:
      //...
      std::string& GetName() {return _uomName;};
      //...
      protected:
      std::string _uomName;
      //...
      };
      >
      And the subclass
      >
      class UomAngle : public UnitOfMeasure
      {
      public:
      UomAngle();
      >
      UomAngle(const UnitOfMeasure &uom);
      >
      virtual ~UomAngle();
      >
      };
      >
      When I create an UomAngle object (called radian) and use it, all works
      fine
      >
      cout<<radian->GetName();
      >
      Then, I have another base class
      >
      class Measure
      {
      public:
      Measure();
      virtual ~Measure();
      >
      double GetValue(){retu rn _value;};
      std::string& GetUomName(){re turn _uom->GetName();};
      >
      protected:
      double _value;
      private:
      UnitOfMeasure *_uom;
      };
      >
      And the subclass
      >
      class Angle : public Measure
      {
      public:
      Angle(double value,const std::string& uomName);
      >
      private:
      UomAngle *_uom;
      };
      >
      And I have the following problems:
      >
      1.- When I create an Angle and I want to get the uom name
      >
      Angle a1(180,degree);
      cout<<a1.GetVal ue()<<"\t";
      cout<<a1.GetUom Name();
      >
      it doesn't work. If I put the method
      >
      std::string& GetUomName(){re turn _uom->GetName();};
      >
      in the class Angle it works. But, why I have to do this? How can I do
      it work without putting GetUomName in each subclass of Measure?
      >
      2.- The other problem is that
      >
      Angle a1(180,degree);
      cout<<a1.GetVal ue()<<"\t";
      cout<<a1.GetUom Name();
      >
      doesn't work because I have a problem with returning string&. I think
      that I have a problem because in
      >
      std::string& GetUomName(){re turn _uom->GetName();};
      >
      _uom->GetName() works like a local variable, and I'm returning a
      reference to this local variable.
      >
      Any idea to solve those problems or more problems that I could have?
      Thanks!
      >
      >
      I don't see anywhere in there where _uom is ever pointing to anything
      useful.

      Comment

      • sernamar

        #4
        Re: problem with string&amp;

        Here is the full code:

        // UnitOfMeasure.h
        //
        #include <string>

        class UnitOfMeasure
        {
        public:
        UnitOfMeasure() {};

        UnitOfMeasure(s td::string &uomID,
        std::string &uomName,
        std::string &uomSymbol,
        int measureType,
        std::string &nameStandardUn it,
        double scaleToStandard Unit,
        double offsetToStandar dUnit,
        std::string &formula);

        virtual ~UnitOfMeasure( );

        std::string& GetID() {return _uomID;};
        std::string& GetName() {return _uomName;};
        std::string& GetStandardName () {return _nameStandardUn it;};

        std::string& GetSymbol() {return _uomSymbol;};

        double ConvertToStanda rd(double value);
        double ConvertFromStan dard(double value);

        protected:
        std::string _uomID;
        std::string _uomName;
        std::string _uomSymbol;
        int _measureType;
        std::string _nameStandardUn it;
        double _scaleToStandar dUnit;
        double _offsetToStanda rdUnit;
        std::string _formula;
        };

        // UnitOfMeasure.c pp
        //
        #include "UnitOfMeasure. h"

        using namespace std;

        UnitOfMeasure:: UnitOfMeasure (std::string &uomID,
        std::string &uomName,
        std::string &uomSymbol,
        int measureType,
        std::string &nameStandardUn it,
        double scaleToStandard Unit,
        double offsetToStandar dUnit,
        std::string &formula):
        _uomID(uomID),
        _uomName(uomNam e),
        _uomSymbol(uomS ymbol),
        _measureType(me asureType),
        _nameStandardUn it(nameStandard Unit),
        _scaleToStandar dUnit(scaleToSt andardUnit),
        _offsetToStanda rdUnit(offsetTo StandardUnit),
        _formula(formul a)
        {
        }

        UnitOfMeasure:: ~UnitOfMeasure( )
        {
        }

        double UnitOfMeasure:: ConvertToStanda rd(double value)
        {
        return value * _scaleToStandar dUnit + _offsetToStanda rdUnit;
        }

        double UnitOfMeasure:: ConvertFromStan dard(double value)
        {
        return (value - _offsetToStanda rdUnit) / _scaleToStandar dUnit;
        }

        // UomAngle.h
        //
        class UomAngle : public UnitOfMeasure
        {
        public:
        UomAngle();

        UomAngle(const UnitOfMeasure &uom);

        virtual ~UomAngle();

        };

        // UomAngle.cpp
        //
        #include "UomAngle.h "

        UomAngle::UomAn gle():UnitOfMea sure()
        {
        }

        UomAngle::UomAn gle(const UnitOfMeasure &uom):UnitOfMea sure(uom)
        {
        }

        UomAngle::~UomA ngle()
        {
        }

        // Measure.h
        //
        #include <string>
        #include "UnitOfMeasure. h"

        class Measure
        {
        public:
        Measure();

        virtual ~Measure();

        double GetValue(){retu rn _value;};
        std::string& GetUomName(){re turn _uom->GetName();};
        std::string& GetUomSymbol() {return _uom->GetSymbol(); };

        protected:
        double _value;
        private:
        UnitOfMeasure *_uom;
        };

        // Measure.cpp
        //
        #include "Measure.h"

        Measure::Measur e()
        {
        }

        Measure::~Measu re()
        {
        }

        // Angle.h
        //
        #include "Measure.h"
        #include "UomAngle.h "

        class Angle : public Measure
        {
        public:
        Angle();

        Angle(double value,const std::string& uomName);

        virtual ~Angle();

        std::string& GetUomName(){re turn _uom->GetName();};
        std::string& GetUomSymbol() {return _uom->GetSymbol(); };

        Angle ConvertToStanda rd();
        Angle ConvertFromStan dard(const std::string& uomName);
        Angle ConvertTo(const std::string& uomName);

        private:
        UomAngle *_uom;
        Angle(double value,UomAngle* uom);
        };

        // Angle.cpp
        //
        #include "Angle.h"
        #include "UomManager .h"

        using namespace std;

        Angle::Angle()
        {
        }

        Angle::Angle(do uble value,const string &uomName)
        {
        _value = value;
        UomManager *uomMng = UomManager::Get Instance();
        _uom = uomMng->GetUomAngle(uo mName);
        }

        Angle::Angle(do uble value,UomAngle* uom)
        {
        _value = value;
        _uom = uom;
        }

        Angle::~Angle()
        {
        }

        Angle Angle::ConvertT oStandard()
        {
        string stdname(_uom->GetStandardNam e());
        if (_uom->GetName() == stdname) return *this;
        else {
        UomManager *uomMng = UomManager::Get Instance();
        UomAngle *stduom = uomMng ->GetUomAngle(st dname);
        double newvalue = _uom->ConvertToStand ard(_value);
        return Angle(newvalue, stduom);
        }
        }

        Angle Angle::ConvertF romStandard(con st std::string& uomName)
        {
        if (_uom->GetName() == uomName) return *this;
        else {
        UomManager *uomMng = UomManager::Get Instance();
        UomAngle *uom = uomMng ->GetUomAngle(uo mName);
        double newvalue = uom->ConvertFromSta ndard(_value);
        return Angle(newvalue, uom);
        }
        }

        Angle Angle::ConvertT o(const std::string& uomName)
        {
        if (_uom->GetName() == uomName) return *this;
        else {
        UomManager *uomMng = UomManager::Get Instance();
        UomAngle *uom = uomMng ->GetUomAngle(uo mName);
        double stdvalue = _uom->ConvertToStand ard(_value);
        double newvalue = uom->ConvertFromSta ndard(stdvalue) ;
        return Angle(newvalue, uom);
        }
        }


        note: UomManager is a unit of measure manager that allows to get a
        pointer to a UomAngle with the method GetUomAngle(uom Name), where
        uomName is a string.

        //main.cpp
        //
        #include <cstdlib>
        #include <iostream>
        #include <iomanip>

        #include <uommanager.h >
        #include <angle.h>

        using namespace std;

        int main(int argc, char *argv[])
        {
        string degree("degree" );

        Angle a1(180,degree);
        cout<<a1.GetVal ue()<<"\t";
        cout<<a1.GetUom Symbol();
        cout<<" ("<<a1.GetUomNa me()<<")"<<endl <<endl;

        system("pause") ;
        return EXIT_SUCCESS;
        }

        That's all. If you need something more, just tell me.
        Thanks

        Comment

        Working...