Sign in to follow this  
Sagar_Indurkhya

SOLVED - C++ Static Classes like Java?

Recommended Posts

I didn't use to use static classes in C++ before, but this year after AP Java, I really like static classes for holding things like experimental constants, or perhaps functions that everyone has to use. How can I create a static class in C++, where I can store lots of constants, intialize them once, and then use them throughout my program. Thanks in advance. - Sagar [Edited by - Sagar_Indurkhya on May 28, 2005 10:09:15 AM]

Share this post


Link to post
Share on other sites
You can store static variables in any class, they'll be initialized once, and shared with all instances of this class. You can even have static methods, as long as these methods doesn't use non-static members.

But, in my opinion, if it's only constants to play with, why don't you simply declare them outside of a class?

Hope this helps

Eric

Share this post


Link to post
Share on other sites
Quote:
Original post by xEricx
You can store static variables in any class, they'll be initialized once, and shared with all instances of this class. You can even have static methods, as long as these methods doesn't use non-static members.

But, in my opinion, if it's only constants to play with, why don't you simply declare them outside of a class?

Hope this helps

Eric


Thanks. Yeah, I found it on google, and it turns out I can do just what I wanted.
There are a lot of experimental values I have to manipulate in the experiment, and I think it would be easier to have all of these in one class.

Share this post


Link to post
Share on other sites
You can create a class containing static members & methods, ie:

class StaticContainer {
private:
static const int MyConstStatic;
static int MyNonConstStatic;
static int foo() { return 3; }
};

// intitialize the static variables
const int StaticContainer::MyConstStatic=1;
int StaticContainer::MyNonConstStatic=2;

//... usage
int blah = StaticContainer::MyConstStatic;
int ret = StatiContainer::foo();



Now, this is kidna messy, isn't it? C++ does not allow you to initialize your consts or statics at the time of declaration, so you must do that someplace else.
This method works, though you might want to consider using a struct instead of a class, as everything's public by default, but that depends on what you need to do with it.

You may also declare a static instance of the type at the declaration:
static struct A_DEF {
public:
const int MyConst;
int foo();

// we can now initialize our methods here:
A_DEF():MyConst(1) {}
} A;

// ... elsewhere
int blah = A.MyConst;
int ret = A.foo();


Note you can also declare your class instance as const, but only if the methods inside are also const:
const static struct A_DEF {
const int a;
int Add(int nArg1, int nArg2) const { return nArg1+nArg2; }

A_DEF():a(1){}

} A;



However, since all you're doing is creating scoped methods and variables, I would suggest using a simple namespace:

namespace myconstants {
const int MyConst = 1;
int MyNonConst = 2;
int foo() { return 3; }
}

//.. usage
int blah = myconstants::MyConst;
int ret = myconstatns::foo();

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this