Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VC++6 and accessors

Status
Not open for further replies.

jsteel

Programmer
Feb 11, 2005
17
0
0
US
Ive been creating a game and my best reference uses all public members in every class. I followed this example until now, when I can no longer go against my education. I want to add in accessor methods. But this seems like a very daunting task do do for every member.

Can you only use properties in VC.net?

And should I actually be changing all these members to a private scope?

I am also going to encapsulate all my globals in singleton classes. Is there a better way to approach this as well?

Thanks
 
>when I can no longer go against my education.

Good

>I want to add in accessor methods

Even though I am totally into OO would as such always encourage use of access methods/private members I'd ask you to consider if its worth changing it, esp. if its a lot of work.

OO and encapsulation is more that just access methods.

Ie just going from
Code:
public:
  int mBalance;
to
Code:
public:
  void setBalance(int i) { mBalance =i;}
  int getBalance() const { return mBalance; }
private:
  int mBalance;
Does not give a lot, virtually nothing infact - you can still do whatever with mBalance.

Proper encapsulation means to design the interface to help/force you use it the right way.

Example:
Code:
public:
  int getBalance() const { return mBalance; }
  void deposit(int amount) { mBalance +=amount; }
  void withdraw(int amout) 
  { 
    if (amount>mBalance) throw "Not enough cash."
    mBalance-=amount;
  }
  // Delibarately no setBalance.

>And should I actually be changing all these members to a private scope?

Otherwise there would be no sense in having access methods.

>I am also going to encapsulate all my globals in singleton classes. Is there a better way to approach this as well?

My prefered approach is to not have globals at all. Instantiate the class and pass references to those who neeed it. By making copyconstructor & assignemnt operator private (and not implemented, only declared) you prevent unintentional copy of the single instance you pass about reference to.




/Per
[sub]
www.perfnurt.se[/sub]
 
Another reason for using Get & Set methods is to use locking for multithreaded apps whenever you try to get or set the values.
 
as perfnurt said, do it if you have time! it is, of course, better to use getters/setters so that people looking at your code don't have a gagging reaction! :p

if you think about it, if you use public getters and setters, it is pretty much saying that the variable is public.. just that you have to call these methods instead of assignment operators..

on the other hand, if it is perfectly safe to access variables directly, then you can avoid the overhead of a function call... saying this goes against my education (!!) but it is true! but only if there is clearly no "danger" in doing so...
 
ps. i would use getters adn setters for sure if it was a big class! but for a little class, where you can easily visualize stuff in your head i would say not using them is ok. just to be on the safe side!
 
Thans a lot guys. Youve all helped me a lot. Its good to hear some opinions from people that dont live by the book, or are obsessed with speed and trying to cut corners. I guess you could say the realists.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top