S
sernamar
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(){return _value;};
std::string& GetUomName(){return _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.GetValue()<<"\t";
cout<<a1.GetUomName();
it doesn't work. If I put the method
std::string& GetUomName(){return _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.GetValue()<<"\t";
cout<<a1.GetUomName();
doesn't work because I have a problem with returning string&. I think
that I have a problem because in
std::string& GetUomName(){return _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 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(){return _value;};
std::string& GetUomName(){return _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.GetValue()<<"\t";
cout<<a1.GetUomName();
it doesn't work. If I put the method
std::string& GetUomName(){return _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.GetValue()<<"\t";
cout<<a1.GetUomName();
doesn't work because I have a problem with returning string&. I think
that I have a problem because in
std::string& GetUomName(){return _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!