Connecting Tech Pros Worldwide Help | Site Map

getset vs public-private

Kev
Guest
 
Posts: n/a
#1: Oct 10 '05
Yes this is a very old argument that has already been beaten to death ;o)

To allow one instance of a class to copy private/protected values from
another Im currently using some simple 'get' functions.

Is this pretty much the most common way to do it... other than making the
variables themselves public?
Sandeep
Guest
 
Posts: n/a
#2: Oct 10 '05

re: getset vs public-private


>>To allow one instance of a class to copy private/protected values from[color=blue][color=green]
>>another Im currently using some simple 'get' functions.[/color][/color]
[color=blue][color=green]
>>Is this pretty much the most common way to do it... other than making the
>>variables themselves public?[/color][/color]

By making a member "private" and providing get-set methods gives the
class maker more control over its members. Doing this, you can expose
the members the way you want to, the consumer class will not have
direct access to the members (and their addresses). This way you can
protect your member variables from illegal manipulation by other
classes.

Kev
Guest
 
Posts: n/a
#3: Oct 10 '05

re: getset vs public-private


"Sandeep" <sandeepsinghal@gmail.com> wrote in news:1128914551.250102.215620
@f14g2000cwb.googlegroups.com:
[color=blue]
> By making a member "private" and providing get-set methods gives the
> class maker more control over its members. Doing this, you can expose
> the members the way you want to, the consumer class will not have
> direct access to the members (and their addresses). This way you can
> protect your member variables from illegal manipulation by other
> classes.[/color]

thx a bunch
Julián Albo
Guest
 
Posts: n/a
#4: Oct 10 '05

re: getset vs public-private


Kev wrote:
[color=blue]
> To allow one instance of a class to copy private/protected values from
> another Im currently using some simple 'get' functions.
> Is this pretty much the most common way to do it... other than making the
> variables themselves public?[/color]

The better way is no "to copy private/protected values". You write a
function to ask the object about some value, without worrying if is a
private variable, calculated in some way, or readed from a database.

--
Salu2
Jim Langston
Guest
 
Posts: n/a
#5: Oct 11 '05

re: getset vs public-private


"Kev" <die_spammers_die@pht.zzz> wrote in message
news:Xns96EAC0D3088EDkevsmail@216.168.3.44...[color=blue]
> Yes this is a very old argument that has already been beaten to death ;o)
>
> To allow one instance of a class to copy private/protected values from
> another Im currently using some simple 'get' functions.
>
> Is this pretty much the most common way to do it... other than making the
> variables themselves public?[/color]

Consider:

class MyClass
{
public:
int i;
};

class MyClass2
{
private:
int i;
public:
void SetI{ int xi ) { i = xi; }
int GetI( ) { return i; };
};

Okay, you put these in production and they get used all over. Now it's
decided that you need to log to a file every time the variable i gets
changed.

Or you add a new variable that's derived from i and needs to change every
time i gets changed. Which do you think would be easier to implement?

With MyClass2 you modify the SetI, recompile and you're done.

With MyClass you have to search through all your source to find everywhere i
is used and figure out how to modify the code each time.


Kev
Guest
 
Posts: n/a
#6: Oct 11 '05

re: getset vs public-private


"Jim Langston" <tazmaster@rocketmail.com> wrote in
news:4NM2f.176$f83.160@fe06.lga:
[color=blue]
> With MyClass you have to search through all your source to find
> everywhere i is used and figure out how to modify the code each time.[/color]

True. And Ive also come across a couple others that use private variables
and would probably benefit from this approach. Thankfully they only needed
to be changed in a few places.
Closed Thread