I have a static member variable ,I want to initialize it by passing it to a function which modifies its own parameter ,like:
Class MyClass
{
static RECT rcRect;//The RECT structure defines the coordinates of the upper-left and lower-right corners of a rectangle.
}
GetClientRect(GetDesktopWindow(),&rcRect);
//Windows API,
//GetDesktopWindow retrieves a handle to the desktop window ,
//GetClientRect gets the coordinates of the desktop's client area ,
//and passes it to rcRECT
The only way I can think of is to initialize it in the main function . My question is :Is this the only way to do it? How Can I initialize it in the .h file ?
Your function will only modify a copy of p
that goes out of scope as soon the function returns.
To modify any variable passed in used a reference parameter:
void func(Typename& p)
// ^
Static class members can be initialized in the class declaration using constexpr
:
class Foo {
static constexpr Typename func();
static constexpr Typename bar = func();
};
See here also please.
you can modify it in the .h file by :
#ifndef FILENAME_H
#define FILENAME_H
you can also include any header you needed here like stdio or stdlib if you want then write the procedure / function
void func(Typename& p);
After that you must create a .cpp file to implement this header (because in header file you only write the procedure / function not the implementation)
#include "filename.h"
void func(Typename& p)
{
//code here,
//modifies the value of p
};
Finally in your driver (main program you can use it by include the .h file)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.