[C++] Classes

Classes vs. Functions & Globals

Page 1 of 1

2 Replies - 653 Views - Last Post: 26 January 2009 - 12:46 PM

#1 Hyper  Icon User is offline

  • Banned

Reputation: 108
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08

[C++] Classes

Posted 26 January 2009 - 06:45 AM

Hello, I've always avoided using classes due to a lack of "reusability." I've never personally felt the need to duplicate something more than once in something I've wrote, or to carry that onto another project. Recently I've taken it upon myself to try and learn more about the OO paradigm, more C++ than C I suppose is how you could put it.

In procedural programming, you have to basically either use static variables inside of functions, or global variables to get the job done the first time around (easily done as well), passing 20 values that you're going to use in every function is rather useless, when they're obviously better off being global. At anyrate.

My question was: Is it "ok" by any means, to use a class instead of using global variables and 3-4 functions (that're all associated with that specific data)? A plus side to this is organization (which is mostly what I love about it), and easy modification of a modular program.

Thanks for any answers!

This post has been edited by Hyper: 26 January 2009 - 06:46 AM


Is This A Good Question/Topic? 0
  • +

Replies To: [C++] Classes

#2 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5882
  • View blog
  • Posts: 12,761
  • Joined: 16-October 07

Re: [C++] Classes

Posted 26 January 2009 - 08:21 AM

View PostHyper, on 26 Jan, 2009 - 07:45 AM, said:

I've always avoided using classes due to a lack of "reusability."


Keep in mind, "reusability" is considered one of the major benefits of OO design.

View PostHyper, on 26 Jan, 2009 - 07:45 AM, said:

My question was: Is it "ok" by any means, to use a class...

Yes.

Maintaining a global state in any program is always an issue. For an OO design, I'd recommend using a "singleton" pattern. Load it up with all the junk that needs to be shared. You often see such classes called things like "Helper" or "AppState", depending one the intent.
Was This Post Helpful? 0
  • +
  • -

#3 Hyper  Icon User is offline

  • Banned

Reputation: 108
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08

Re: [C++] Classes

Posted 26 January 2009 - 12:46 PM

That was my exact intent. Thank you very much, baavagi for the confirmation.

I'm attempting to create something reusable. Pretty sure I'm done with it, I'm editing it into a game as we speak. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1