Sign in to follow this  
rldivide

static class ?

Recommended Posts

rldivide    132
Is there a way in C++ to declare static class without repeating X times "static" ? Currently I do something like this: class mystaticclass { static int var1; static int var2; static int var3; static void method1(int param1); static void method2(int param1); static void method3(int param1); } This is not very elegant, and as far as I know there is no such thing as "static class mystaticclass { }". Also, I can't use namespace as I derive this class from another class. Any other workaround ?

Share this post


Link to post
Share on other sites
the_edd    2109
Quote:
Original post by rldivide
class mystaticclass
{
static int var1;
static int var2;
static int var3;
static void method1(int param1);
static void method2(int param1);
static void method3(int param1);
}


Use a namespace.

Share this post


Link to post
Share on other sites
jpetrie    13153
C++ doesn't have such a concept.

You could make a static instance of mystaticclass within mystaticclass, and then access everything through that. But that ends up being far less elegant that just declaring all the methods static.

I'd use a namespace for this, honestly. What are you getting out of the inheritance? You may be able to accomplish that differently.

Share this post


Link to post
Share on other sites
rldivide    132
I can't use namespace as I derive my class from one of the Qt framework class.

The reason I need it static is because at some point in my program I have to use a function pointer, pointing to several methods in this class; and as far as I know there's no way to point directly to dynamic instances.

Share this post


Link to post
Share on other sites
Antheus    2409
Quote:
Original post by rldivide
The reason I need it static is because at some point in my program I have to use a function pointer, pointing to several methods in this class; and as far as I know there's no way to point directly to dynamic instances.


Sure there is. Exactly how depends on which function pointer wrapper you are using.

Share this post


Link to post
Share on other sites
jpetrie    13153
Quote:

I can't use namespace as I derive my class from one of the Qt framework class.

Yes, but why? If your class has nothing but static methods there's very little that actually gains you. It may be gaining you exactly nothing, in fact, depending on the actual classes involved.

Quote:

The reason I need it static is because at some point in my program I have to use a function pointer, pointing to several methods in this class; and as far as I know there's no way to point directly to dynamic instances.

This is doable. Explain what you're trying to do. In detail.

Share this post


Link to post
Share on other sites
rldivide    132
I have a central class (SpectrumGUI) which is both a graphic item (that's why it derives from a Qt framework class) and a hub to which are connected plugins (ToolPlugin class). It exists only once in the program.

The plugins (loaded dynamically at runtime) can at any time requires operations from SpectrumGUI, but they don't know about SpectrumGUI, they are just given methods such as get/set and I have to connect theses methods to the actual methods in SpectrumGUI.

class ToolPlugin
{
void (*get)(int);
void (*set)(int);
}

class SpectrumGUI
{
static void get(int a);
static void set(int a);
}

each time I load a new plugin:
NewPluginInstance->get=&SpectrumGUI::get;
NewPluginInstance->set=&SpectrumGUI::set;

------

Is there a better way to do this ?

Thanks

Share this post


Link to post
Share on other sites
the_edd    2109
Would creating a SpectrumStuff namespace not suffice? Spectrum could stash its shared stuff there.


namespace SpectrumStuff
{
void get(int);
void set(int);
}

// ...

NewPluginInstance->get=&SpectrumStuff::get;
NewPluginInstance->set=&SpectrumStuff::set;


As for whether or not there's a better way, boost::bind and boost::function often make a nice combination for this kind of thing.

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