On 3/19/07, John Gunnarsson <[EMAIL PROTECTED]> 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]

Reply via email to