Putting all functions in classes
I have recently read several posts thats state good programmers put all functions except main() in classes. How would you do this in a way that would let you use the fuctions without making an instance of the class? The whole thing seems pretty pointless to me.....
www.trak.to/rdp
--------------------
You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming
You are unique. Just like everybody else.
"Mechanical engineers design weapons; civil engineers design targets."
"Sensitivity is adjustable, so you can set it to detect elephants and other small creatures." -- Product Description for a vibration sensor
You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming
You are unique. Just like everybody else.
"Mechanical engineers design weapons; civil engineers design targets."
"Sensitivity is adjustable, so you can set it to detect elephants and other small creatures." -- Product Description for a vibration sensor
I try and do pretty religious object orientated programming in C++. Putting you''re functions in classes 1)leads to better program organization. You have to worry less about breaking module x by changing variable b if module x''s functionality is wrapped in a class. Now its not always this cut and dry but, imho, it makes a big difference. Also 2) you have the benefits of inheritance and polymorphism, which when used well provided incredible power and extensibility. CTrap could extended to include CPitTrap or CDartTrap, reusing the code of the parent and provided the children a nice plug in.
You can use functions and member data without an instance of a class by using static (in C++). As long as function is static it can be called with no instance of the class like this:
CMyClass::MyFunction ();
That function can also access any static data you declare for that class.
You can use functions and member data without an instance of a class by using static (in C++). As long as function is static it can be called with no instance of the class like this:
CMyClass::MyFunction ();
That function can also access any static data you declare for that class.
You can use static member functions to call a function without making an instance of the class it''s in:
class doh
{
public:
static int doit(void);
};
int
doh::doit(void)
{
return 0;
}
int
main(void)
{
return (doh::doit());
}
This really (in my opinion) doesn''t make sense to do for very general utility functions. Personally, if it doesn''t naturally lend itself to being in a class, I leave it global. Its also my opinion that "good" programmers won''t be tied down by the word "all" so much that they create confusing code
Hope this helps you a little
-ns-
class doh
{
public:
static int doit(void);
};
int
doh::doit(void)
{
return 0;
}
int
main(void)
{
return (doh::doit());
}
This really (in my opinion) doesn''t make sense to do for very general utility functions. Personally, if it doesn''t naturally lend itself to being in a class, I leave it global. Its also my opinion that "good" programmers won''t be tied down by the word "all" so much that they create confusing code
Hope this helps you a little
-ns-
-ns-
I wont pretend I put every thing in class but sometimes it can help. I just wrote a small, fully static class to store configuration data from a ini file. To me having all the parameters wrapped in the class which then could be queried allowed for it to be neater, as well as also allowing for more durable error handling, etc. However, I do have to admit the very ini file name is a global so I do use them at times. I suppose it can be a bit of a gray area sometimes.
I wouldn''t say you have to always put functions in a class.
It depends on what you are trying to do.
Classes are generally supposed to abstract behavior and data relevant to that behavior.
In some instances you may not want a class.
Sometimes you may want only one instance of a class ( a Singleton design pattern )
If your goal is to provide logical grouping for functions which do not belong to a specific class, you can use a namespace.
Here''s an example ( not necessarily the best, but... ):
namespace Clock
{
long GetTime(void);
}
This means we want a free function GetTime() but we do not want to pollute the global namespace, so we make a namespace to put the function in.
calling the function, without utilizing ''using'' looks very similar to calling a static class method :
long theTime = Clock::GetTime();
It depends on what you are trying to do.
Classes are generally supposed to abstract behavior and data relevant to that behavior.
In some instances you may not want a class.
Sometimes you may want only one instance of a class ( a Singleton design pattern )
If your goal is to provide logical grouping for functions which do not belong to a specific class, you can use a namespace.
Here''s an example ( not necessarily the best, but... ):
namespace Clock
{
long GetTime(void);
}
This means we want a free function GetTime() but we do not want to pollute the global namespace, so we make a namespace to put the function in.
calling the function, without utilizing ''using'' looks very similar to calling a static class method :
long theTime = Clock::GetTime();
Wow, it's good to see the phrase "Design pattern" on a game programming board.
Good post, Steve.
Mason McCuskey
Spin Studios - home of Quaternion, 2000 GDC Indie Games Fest Finalist!
www.spin-studios.com
Edited by - mason on 1/10/00 3:25:46 PM
Good post, Steve.
Mason McCuskey
Spin Studios - home of Quaternion, 2000 GDC Indie Games Fest Finalist!
www.spin-studios.com
Edited by - mason on 1/10/00 3:25:46 PM
Founder, Cuttlefish Industries
The Cuttlefish Engine lets anyone develop great games for iPad, iPhone, Android, WP7, the web, and more!
The Cuttlefish Engine lets anyone develop great games for iPad, iPhone, Android, WP7, the web, and more!
The one thing nobody has mentioned yet is that typically, game programmers are not always looking for the most ''proper'' way to code something - they''re looking for best and fastest way to do it. If you have a good reason for not writing functions in a class, then don''t write them in a class... the last thing you should do is do it just because someone else thinks you should. Programming is a very individual thing, experimenting with your own style and writing programs in a way you understand and are comfortable with is one of the more important things in my opinion.
Regards
Starfall
Regards
Starfall
Hi,
I don''t think it''s absolutely necessary to put all your functions into classes. If you do it like Tim Sweeney, all classes connected to each other through inheritance and polymorphism and whatever (means : well-designed), then ok, but just for creating another class, it''s IMO extremely useless.
If you have well-designed code or want to write well-designed code, then just do it, but don''t just become class-happy and create class after class because they''re called ''classes''. You won''t get a better performance out of it and just for having all your functions in one complex ? No.
CU
Graphix Coding @
Skullpture Entertainment
I don''t think it''s absolutely necessary to put all your functions into classes. If you do it like Tim Sweeney, all classes connected to each other through inheritance and polymorphism and whatever (means : well-designed), then ok, but just for creating another class, it''s IMO extremely useless.
If you have well-designed code or want to write well-designed code, then just do it, but don''t just become class-happy and create class after class because they''re called ''classes''. You won''t get a better performance out of it and just for having all your functions in one complex ? No.
CU
Graphix Coding @
Skullpture Entertainment
Graphix Coding @Skullpture Entertainmenthttp://www.skullpture.de
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement