Non-instanced Classes With Available Functions

Started by
8 comments, last by Auron 18 years, 10 months ago
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); };
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;}
look up static methods.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

Cool, thanks guys. Strange that it would use '::' and not '.' to reference... any reason for that?
Because it's a name scope.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

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
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
my_life:          nop          jmp my_life
[ Keep track of your TDD cycle using "The Death Star" ] [ Verge Video Editor Support Forums ] [ Principles of Verg-o-nomics ] [ "t00t-orials" ]
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.
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.
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

This topic is closed to new replies.

Advertisement