how make a class's for be satic too?

when we create a class, can i add it an option for be static too?
Last edited on
when we create a class
When you create an instance of class, you can make it static like any other variable.
1
2
3
class foo;
//...
static foo variable;


Class itself cannot be static. Why would you need it?
i have a form class been used on another class(like a timer). imagine that i only need just 1 form(the static form) for be used on timer. is what i mean
You mean you need same instance avaliable for all instances of other class?
Then:
1
2
3
4
5
class bar
{
    static foo var;
    //can be accessed from any bar instance
//... 


You need only one instance in the whole program with global access point?
Singleton.

If something else, elaborate your intentions more.
but when i do, for exemple, on constructor:
var.something()
i get 1 error:
"In function `ZN5TimerC1Ev':"(in these case is going for the constructor);
"306|undefined reference to `Timer::frmTimer'"
why?
Looks like you did not link file with class implementation. Without code I cannot say more
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
class Timer
{

private:
    static form frmTimer;//creates and show the form.. the cambalinho.h file is big.
    UINT_PTR timerid=0;
    unsigned int intInterval=0;
    void TimerProc(HWND hwnd,UINT uMsg,UINT idEvent,DWORD dwTime)
    {
         //timerprocedure();
         MessageBox(NULL,"hi", "hi", MB_OK);
    }

    static void CALLBACK _TimerProc(HWND hwnd,UINT uMsg,UINT idEvent,DWORD dwTime )
    {
        reinterpret_cast<Timer*>(idEvent)->TimerProc(hwnd,uMsg,idEvent,dwTime);
    }

public:

    std::function<void()> timerprocedure=NULL;

    Timer()
    {
        frmTimer.Visible=false;
    }

    property<unsigned int> Interval
    {
        Get(unsigned int)
        {
            return intInterval;
        },
        Set(unsigned int uintInterval)
        {
            intInterval = uintInterval;
        }
    };

    void Start()
    {
        if(timerid!=0)
            KillTimer(WindowMain,timerid);//WindowMain is the handle that recives the 1st form handle(i create a form before these class)
        timerid=SetTimer( WindowMain,reinterpret_cast<UINT_PTR>(this),(UINT)intInterval,&Timer::_TimerProc);
    }

    void Stop()
    {
        KillTimer(WindowMain,timerid);
        timerid=0;
    }

    ~Timer()
    {
        KillTimer(WindowMain,timerid);
    }
};
Last edited on
Thanks for this code ;)
anytime ;)
but don't forget to see how the property works ;)
or you need the code?
You have Timer own a form? Isn't is better to hold pointer/reference to the form in your timer?

In your code you did not initialise your static variable.

In implementation file add: Timer::frmTimer;. However I stringly suggest to rethink your architecture.
honestly i did a diferent thing for avoid the form... but what i'm doing wrong on Timer constructor?
(forget how the Timer works... because i use a diferent way for works... but i mean the static form variable.. why i can't change it's values on constructor?)
Because it is declared but is not defined. You are trying to access non-existing value.

It is like that:
1
2
3
4
5
6
7
8
9
extern int i;

void doStuff();

int main()
{
    doStuff(); // Error. Function is not defined
    i = 0; // Error. Variable is not defined
}
now i understand what you mean... but for an int, for exemple, i know do it. but for a class? how can i do it?
sorry... now i'm confused :(
that link is these page link...
and i can't see what you mean :(

for an int i do:

int classname::staticvariablename = value;

but for a class?
I will repeat that line: form Timer::frmTimer;, This will default-construct this object.
thanks for that.. works fine.
let me ask anotherthing out off topic: the form class have the Enabled property. property is my class for class for build properties and i use, too, macros for help me make the properties.
when i do 'frmTimer.' why the Enable isn't showed in intellegence list?
You need to consult your IDE authors for answer to this question.
i see thanks...
thanks for all
Topic archived. No new replies allowed.