Jump to content
  • Advertisement
Sign in to follow this  
Raeldor

Non-instanced Classes With Available Functions

This topic is 4874 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Sorry for the crappy title, but I don't know how else to phrase this... In c++ is it possible to have a class that is never instanced that has public functions that include all data and functionality internally and can be called from the class name alone? Ie... class myclass { int add(int a, int b); }; int myclass::add(int a, int b) { return a+b; } void main() { cout << myclass.add(1,2); };

Share this post


Link to post
Share on other sites
Advertisement
Yes using static members.
#include <iostream>

using namespace std;

class myclass
{
public:
static int add(int a, int b);
};

int myclass::add(int a, int b)
{
return a+b;
}

int main()
{
cout << myclass::add(1,2) << endl;
return 0;
}

Share this post


Link to post
Share on other sites
Cool, thanks guys. Strange that it would use '::' and not '.' to reference... any reason for that?

Share this post


Link to post
Share on other sites
Further, since the dot operator is used for instantiations of structures and classes, it would muddle things up and make it harder to read if that same operator was used for static methods and members.

-Auron

Share this post


Link to post
Share on other sites
Quote:
Original post by Raeldor
Sorry for the crappy title, but I don't know how else to phrase this...

In c++ is it possible to have a class that is never instanced that has public functions that include all data and functionality internally


..even FURTHER [smile]... an abstract base class is good for defining an interface for the derived classes; and it's good to put default implementations into the base class as well, if possible

Quote:
...and can be called from the class name alone?


That would be the (public) static member functions, which the others have described

Share this post


Link to post
Share on other sites
Quote:
Original post by Auron
Further, since the dot operator is used for instantiations of structures and classes, it would muddle things up and make it harder to read if that same operator was used for static methods and members.

-Auron


It's a language design decision, really. Java and Python both use '.' here, and I've never heard any Java or Python programmer complain about it.

Share this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
Quote:
Original post by Auron
Further, since the dot operator is used for instantiations of structures and classes, it would muddle things up and make it harder to read if that same operator was used for static methods and members.

-Auron


It's a language design decision, really. Java and Python both use '.' here, and I've never heard any Java or Python programmer complain about it.


As does C#, and I believe VB.NET (not sure though). I personally prefer the differentiation between instanced and static classes, but this is beside the point.

Anyway, if you need global access to a class, you can always look at the Singleton pattern. However, singletons have their own source of problems, some of which are shared with static methods, some of which aren't.

It's really a matter of style, in my opinion.

See this thread for some better info about singletons.

Share this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
Quote:
Original post by Auron
Further, since the dot operator is used for instantiations of structures and classes, it would muddle things up and make it harder to read if that same operator was used for static methods and members.

-Auron


It's a language design decision, really. Java and Python both use '.' here, and I've never heard any Java or Python programmer complain about it.


True, but C++ also has two different operators already for dealing with objects: the dot operator and the arrow operator (even though it's just a shortcut for (*ptr).whatever). I don't think either C# or Python uses anything more than the dot operator, and I know Java doesn't.

But, since it allows you to access namespaces and to escape any other named scoped (for example ::MessageBox(...)), it seems at least kind of consistent.

-Auron

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!