Thomas,
Thank you for you very extensive answer, I agree with you're view about
getters/setters
in data storage classes, that is if they are only there "because they
should" and not providing any extra value, why having them "just because"?
The classes I'm designing at the momet are purely data access objects used
between the db backend layer and the application logic layer.
The feeling I had was that it could be okay to have public fields in
dto's/dao's, and I wanted it to be confirmed by
some one more experienced in c++, which i got. Thank you again.
Summit,
I see what you mean but what if the Address class have a large number of
fields, in order to change just one, i first have to use the getter (ie.
getAddress) and the change the field, and then I have to
invoke the setter to replace the content of the entire class (ie.
setAddress). This I would like to avoid, it doesnt seem to provide any
value, and with the ability to replace the entire content of the class,
am I really protecting the data? By having fields public I could do:
Order.Address.City = "New York";
One possible solution wich are in between the two described above would be
to actually implement accessors/mutators for all field which are not
objects, and for objects provide a function which returned a pointer to the
internal object instead:
class Address
{
public:
void setZipCode(string const &zipCode);
string getZipCode();
void setCity(string const &city);
string getCity();
/* more accessors and mutators.......*/
private:
/* fields for the above a/m */
};
class Order
{
public:
void setOrderId(int &orderId);
int getOrderId(void);
Address *getShipToAddress() { return &m_shipTo; };
private:
int m_orderId;
Addess m_shipTo;
};
I have seen implementations like this in taglib, and this would enable to
to do:
Order.getShipToAddress()->setCity("New York"); without having any public
fields.
Am I completly going pete tong here?
Thank you all for your input so far.
//John
On 3/21/07, Sumit Chawla <[EMAIL PROTECTED]> wrote:
>
> On 3/19/07, John Gunnarsson < [EMAIL
> PROTECTED]<mailing.list.recipient%40gmail.com>>
> wrote:
> >
> > Hi,
> >
> > I'm looking for you input on whats the best practice for public fields
> > in a class.
> > In other high level languages (such as C#) it usually exist a property
> > get/set mechanism. I have seen implementatons using get/set methods
> > for reteriving and setting private values in a c++ class, which i find
> > totally okay for primitives as int, strings etc.
> >
> > One thing that I don't know how to handle is where a class have one or
> > several class instances inside itself (as private fields) but should
> > be accessable from public.
> >
> > Ofcource you could have the get/set methods but then you have to get
> > and set the complete internal state of the class, where it's very
> > probably that you only would like to change one single field.
> >
> > What is the best practice to solve this? Is it okay to have publci
> > fields in a c++ class without get/set methods?
> >
> > I could of cource implement a get function that returnes a pointer to
> > the class, and via that pointer I could change the internal state on
> > the second class. If my explanation is messy consider this code (semi
> > pseduo)
> >
> > class Address
> > {
> > public:
> > string getCity();
> > string setCity(string const &city);
> >
> > int getId();
> > void setId(int const &id);
> > private:
> > string name;
> > int id;
> >
> > };
> >
> >
> > class Order
> > {
> > public:
> > /*
> > what to do here? implement get/set functions for the fields below,
> > or it it okay to declare the payer and shipto members here?
> > */
> >
> > private:
> > Address payer;
> > Address shipto;
> > };
> >
> > //John
> >
> >
> Hi John
>
> I would recommend to keep the fields to be kept as private. You can
> provide
> following type of methods in your class Order:
>
> SetPayerAddress(Address addr);
> SetShippingAddress(Address shipto);
> PrintBillingInfo()
> {
> //Process the Private fields using the Methods specified by Address Class
> }
>
> --
> Regards
> Sumit Chawla ("eNetra : Hail the Invention")
>
> "Computers are useless. They can only give you answers.":Pablo Picasso
>
> [Non-text portions of this message have been removed]
>
>
>
[Non-text portions of this message have been removed]