-3

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 ?

iouvxz
  • 89
  • 9
  • 27
  • Try to declare the static variable as extern in the header. In the cpp you can assign it a standard value and then also a value from elsewhere, for example a file. This is how i realise using a config file – Eskalior Nov 14 '15 at 14:35
  • I'm not sure to understand, as you do not give your code. You may give your static member a default value where you define it. Maybe are you doing so in MyClass.cpp (Typename MyClass::P = ;). See http://stackoverflow.com/questions/185844/initializing-private-static-members – SR_ Nov 14 '15 at 14:43

2 Answers2

1

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.

Community
  • 1
  • 1
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
1

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)

varian_97
  • 11
  • 2