473,396 Members | 1,726 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Static Method/stateless/statefull/how to :-)

Now... I have a problem... It's an engineering problem.
I have a function, we will call it MyBigFunc. It's a function that can be
easily built as a static method, because it is the only function that the
"user" will use and it is stateless.

So I wrote something like:
class MyClass
{
public:
static int MyBigFunc() { return 0;}
};

Clearly there are some private/protected methods that are called by
MyBigFunc. There is a storage (a sub-class) that is istantiated by MyBigFunc
and is destroyed at the end of MyBigFunc. It's quite big but it's life is
short (the life of MyBigFunc) The private/protected methods receive a
pointer to this subclass.

....
private:
class MyClassSomeData
{
};
....

Then I decided to make it easily expandible, so I built some overridable
functions that are called by MyBigFunc at various times. Clearly to be
called by a static function they are static :-) The problem is that the
"user" functions probably will need some private data that THEY will produce
(so, for example, if a1, a2 and a3 are methods called by MyBigFunc, a1 could
open a file that will be used by a2 and a3. The file must be closed in the
end). There is even the possibility (it is probable) that a derived class
will be stateful. So now I don't know what to do.

Oh... yes... I was forgetting something: at this time the base class is a
template class and it calls the methods through "template-virtual" calls

So it's more like:

template <class T>
class MyClass
{
public:
static int MyBigFunc()
{
MyClassSomeData SomeData;
T::OneFunc(&SomeData);
return 0;
}
static int OneFunc(MyClassSomeData *pSomeData) { // donothing }
};


Oct 24 '05 #1
4 2738
MaxMax wrote:
Now... I have a problem... It's an engineering problem.
I have a function, we will call it MyBigFunc. It's a function that can be
easily built as a static method, because it is the only function that the
"user" will use and it is stateless.

So I wrote something like:
class MyClass
{
public:
static int MyBigFunc() { return 0;}
};

Clearly there are some private/protected methods that are called by
MyBigFunc. There is a storage (a sub-class) that is istantiated by MyBigFunc
and is destroyed at the end of MyBigFunc. It's quite big but it's life is
short (the life of MyBigFunc) The private/protected methods receive a
pointer to this subclass.

...
private:
class MyClassSomeData
{
};
...

Then I decided to make it easily expandible, so I built some overridable
functions that are called by MyBigFunc at various times. Clearly to be
called by a static function they are static :-) The problem is that the
"user" functions probably will need some private data that THEY will produce
(so, for example, if a1, a2 and a3 are methods called by MyBigFunc, a1 could
open a file that will be used by a2 and a3. The file must be closed in the
end). There is even the possibility (it is probable) that a derived class
will be stateful. So now I don't know what to do.

Oh... yes... I was forgetting something: at this time the base class is a
template class and it calls the methods through "template-virtual" calls

So it's more like:

template <class T>
class MyClass
{
public:
static int MyBigFunc()
{
MyClassSomeData SomeData;
T::OneFunc(&SomeData);
return 0;
}
static int OneFunc(MyClassSomeData *pSomeData) { // donothing }
};


I think your post got cut off before it got to the question. Assuming
that you are interested in alternative design ideas, I would recommend
looking into function objects instead of purely static functions:

struct MyBigFunctionObjectClass
{
int operator() const;
...
} BigFunction;

int main()
{
int x = BigFunction();
...

MyBigFunctionObjectClass can be subclassed, its function call operator
can be declared virtual - or be declared a member template - in short
there's quite a range of possible uses for function objects that one of
them might work well for your situation.

Greg

Oct 24 '05 #2
> I think your post got cut off before it got to the question. Assuming
that you are interested in alternative design ideas, I would recommend
looking into function objects instead of purely static functions: No no... the question was "how should I build the class"
struct MyBigFunctionObjectClass
{
int operator() const;
...
} BigFunction;


Mmmh... It seems a little strange... And I would need a .cpp file (because
if I include this in a .h file the linker will complain about multiple
BigFunction)... But then I'll need a .h file because I'm using templates
(and I'm not using Comeau)

--- bye
Oct 24 '05 #3
MaxMax wrote:
Now... I have a problem... It's an engineering problem.
Since this is not a precise question about a language feature, you
should have put more efforts in making the question simple. You
probably won't get many answers to this post.
I have a function, we will call it MyBigFunc. It's a function that can be
easily built as a static method, because it is the only function that the
"user" will use and it is stateless.
This is an invalid premise. You should use static member functions if
1) the class is stateless
2) creating several instances of the class is invalid semantically

The first point seems to be refuted below when you say "There is even
the possibility (it is probable) that a derived class will be
stateful". You never talked about the second point so I assume it is
not important.
So I wrote something like:
class MyClass
{
public:
static int MyBigFunc() { return 0;}
};

Clearly there are some private/protected methods that are called by
MyBigFunc.

There is a storage (a sub-class) that is istantiated by MyBigFunc
and is destroyed at the end of MyBigFunc. It's quite big but it's life is
short (the life of MyBigFunc) The private/protected methods receive a
pointer to this subclass.

...
private:
class MyClassSomeData
{
};
...

Then I decided to make it easily expandible, so I built some overridable
functions that are called by MyBigFunc at various times. Clearly to be
called by a static function they are static :-) The problem is that the
"user" functions probably will need some private data that THEY will produce
(so, for example, if a1, a2 and a3 are methods called by MyBigFunc, a1 could
open a file that will be used by a2 and a3. The file must be closed in the
end). There is even the possibility (it is probable) that a derived class
will be stateful. So now I don't know what to do.

Oh... yes... I was forgetting something: at this time the base class is a
template class and it calls the methods through "template-virtual" calls

So it's more like:

template <class T>
class MyClass
{
public:
static int MyBigFunc()
{
MyClassSomeData SomeData;
T::OneFunc(&SomeData);
return 0;
}
static int OneFunc(MyClassSomeData *pSomeData) { // donothing }
};


That's a weird design. You should use virtual functions instead.

class MyClass
{
public:
int MyBigFunc()
{
MyClassSomeData some_data;
one_fun(&some_data);
return 0;
}

virtual int one_fun(MyClassSomeData *sd)
{
// do nothing
}
};

class Derived : public MyClass
{
public:
virtual int one_fun(MyClassSomeData *sd)
{
// ..
}
};

You may make MyClass a singleton, though this is another matter
completly. If you don't really need MyClass to have only one instance,
you can forget static member functions for this design.
Jonathan

Oct 24 '05 #4
> This is an invalid premise. You should use static member functions if
1) the class is stateless The class IS stateless. A derived class could be stateless or could be
statefull. I'm building the class to be easily expandable. 2) creating several instances of the class is invalid semantically

This isn't my problem.

The main problem is that if I create a stateless class using static
functions it will be very difficult to derive it in a statefull class. I was
even thinking of using the preprocessor to make something like:

#define STATELESSCLASS static

so that the "user" can select if he want the class to be stateless or
statefull

and then
#ifdef STATELESSCLASS
class MyClassStateless
#else
class MyClassStatefull
#endif
STATELESSCLASS int MyBigFunction()
{
}

In this way the "user" can include twice the header, once with and once
without the STATELESSCLASS defined and have both, or he can include it once
and have only one of the two.

--- bye
Oct 25 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: baylor | last post by:
In C#, an interface cannot mark any method as static. i'm told the ILASM supports it but i've never tested that Two questions. First, why? OK, i've heard the reason about interfaces being...
16
by: Bruno Rodrigues | last post by:
Hi In a class with simple methods like: Products.Insert(string name, string desc) Products.Update(int id, string name, string desc) Products.Delete(int id) What is better? Static or common...
9
by: Clint | last post by:
Hey all - Excuse the cross-post ... I'm not sure what the appropriate newsgroup would be for this question. I have a question that I'm not quite sure how to ask. For all I know, I have the...
10
by: tatemononai | last post by:
The old ASP was stateless. Every single time you hit the server it was starting from scratch. Even though HTTP is still stateless, it is my understanding that ASP.Net solves this problem by...
5
by: joeblast | last post by:
I have a Web service that gets the financial periods and hold a reference to a disconnected dataset built at initialization. Web methods work on the dataset inside the web service. Everything is...
4
by: MuZZy | last post by:
Hi, I'm retouching some utility classes used everywhere across our app, and there are certain methods used everywhere and pretty frequently. I'm changing them from instance methods to static...
2
by: Steve | last post by:
C# I am new to C#, and new to the whole OO concepts, and so I am sure this is a very stupid question, but I'll ask anyway. When should my methods be static? I have several "Business Objects" in...
14
by: Shimon Sim | last post by:
I have a static variable defined something like this private static Dictionary<string, string> roles = new Dictionary<string,string>(); Can I safely assume that it will be live for the live of...
11
by: Raja Chandrasekaran | last post by:
Hai folks, I have a question to get exact answer from you people. My question is How Static class is differ from instance class and If you use static class in ASP.NET, ll it affect speed or...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.