Member having only referenced attributes

M

Massimo Burcheri

Hello.

This is my first post to this group. Excuse me if the question doesn't
conform to the regulations.

My class Gps has private attributes latitude, longitude.. and public get()
set() methods. To access groups of attributes it also has a private member
Position and public getPosition(). For having the data only once instead of
twice I'd like to have the members of Position just being references.
But writing to Gps members doesn't apply to Position members as you can see
here:

Gps* gpsP = new Gps();
gpsP->setLongitude(1);
cout << gpsP->getLongitude() << endl; //is 1
cout << gpsP->getPosition().getLongitude() << endl; //is 0 ??

I tried to show in a minimal case attached at the end of this post.

Best regards,
Massimo

--
// gps.h

class Position
{
private:
double latitude_, longitude_, altitude_;
public:
Position(const double& _latitude, const double& _longitude, const
double& _altitude) :
latitude_(_latitude), longitude_(_longitude),
altitude_(_altitude) {}
const double& getLatitude() const;
const double& getLongitude() const;
const double& getAltitude() const;
};

inline const double& Position::getLatitude() const { return(latitude_); }
inline const double& Position::getLongitude() const{ return(longitude_); }
inline const double& Position::getAltitude() const { return(altitude_); }

class Gpsd
{
private:
double latitude;
double longitude;
double altitude;
Position* position;
public:
Gps() : latitude(0),longitude(0),altitude(0) {}
~Gps();

double const& getLatitude() const;
double const& getLongitude() const;
double const& getAltitude() const;

void setLatitude(const double&);
void setLongitude(const double&);
void setAltitude(const double&);

Position const& getPosition() const;

inline double const& Gps::getLatitude() const { return(latitude); }
inline double const& Gps::getLongitude() const { return(longitude); }
inline double const& Gps::getAltitude() const { return(altitude); }

inline Position const & Gps::getPosition() const { return(*position); }

inline void Gps::setLatitude(const double& n) { latitude=n; }
inline void Gps::setLongitude(const double& n) { longitude=n; }
inline void Gps::setAltitude(const double& n) { altitude=n; }


// gps.cpp

Gps::Gps()
{
position = new Position ( getLatitude(), getLongitude(),
getAltitude() );
}
 
M

Massimo Burcheri

Massimo said:
But writing to Gps members doesn't apply to Position members

I solved it now, not sure if that's a good design:
// gps.h
class Position
{
private:
double latitude_, longitude_, altitude_;
// members have to be references too of course:
double &latitude_, &longitude_, &altitude_;
public:
Position(const double& _latitude, const double& _longitude, const
double& _altitude) :
latitude_(_latitude), longitude_(_longitude),
altitude_(_altitude) {}
// non const input parameters:
Position(double& _latitude, double& _longitude, double&
_altitude) : latitude_(_latitude), longitude_(_longitude),
altitude_(_altitude) {}
Gps::Gps()
{
position = new Position ( getLatitude(), getLongitude(),
getAltitude() );
}
// to keep const get functions I pass private members directly
position = new Position ( latitude, longitude, altitude );

Regards,
Massimo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top