static class ?

Started by
7 comments, last by no such user 14 years, 1 month ago
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 ?
Advertisement
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.
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.
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.
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.
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.
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

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.
Why not use an interface rather than manually setting function pointers?

This topic is closed to new replies.

Advertisement