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(&Som eData);
return 0;
}
static int OneFunc(MyClass SomeData *pSomeData) { // donothing }
}; 4 2755
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(&Som eData); return 0; } static int OneFunc(MyClass SomeData *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 MyBigFunctionOb jectClass
{
int operator() const;
...
} BigFunction;
int main()
{
int x = BigFunction();
...
MyBigFunctionOb jectClass 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
> 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 MyBigFunctionOb jectClass { 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
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(&Som eData); return 0; } static int OneFunc(MyClass SomeData *pSomeData) { // donothing } };
That's a weird design. You should use virtual functions instead.
class MyClass
{
public:
int MyBigFunc()
{
MyClassSomeData some_data;
one_fun(&some_d ata);
return 0;
}
virtual int one_fun(MyClass SomeData *sd)
{
// do nothing
}
};
class Derived : public MyClass
{
public:
virtual int one_fun(MyClass SomeData *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
> 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 MyClassStateles s
#else
class MyClassStateful l
#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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
allocated on the stack but that's not a real reason. Which is OK because this
isn't a real question, it's just a complaint dressed up to look like a reason
Second question. If a language doesn't support a fairly obvious feature, one
has to wonder if...
|
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 methods? This objects do not need
instances.
|
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 verbaige completely wrong, but here goes nothing ...
I'm currently using the MS Data Access Block for a desktop application
I'm writing. Recently, I had to add a call to a web service, which in
|
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 hiding a unique code
inside the form of the client, associating each client with their
little block in memory on the server. When you post something it
automatically fills your text boxes back up, etc.
This is very convenient, but I'm having some...
|
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 fine so far.
My problem is that when I call a web method to modify the dataset inside the
Web service it is never updated and I get no errors.
currentPeriods method will always give me 1 even if I call ChangeDataset and
call currentPeriods...
| |
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 ones, so to use them you
don't need to create an instance of that utility class.
So my question is - is there any difference/danger of using a static
method vs instance method.
|
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 my app, which basically hold key
functionality. For example I have an Asset class. In there I have some
methods for getting various attributes of an asset. For example I have this:
public class Asset
{
|
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 application?
Meaning that any thread/page of the application will have access to the data
in this variable.
|
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 performance of site...?
Because I am using static class in my Database layer. But one of my
friend told me that "static class ll take more space than instance
class and it ll affect the performance. But if you use Instance class
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |