|
any suggestions? i have an integer set equal to the value of GetCurSel(). i need to use this value in multiple functions, which i have spaced out over different header files. is there a way to acces this one value from all my other files? I've searched the msdn library for info on #ifndef, but i took away the impression thats solely for creating namespaces, which i think might be a little too far out of what i need to do.
thanks,
keegan
*.*
|
|
|
|
|
You could use a global variable, but that's icky.
What you ought to do is declare that integer as a static member variable in a class, and include the header for that class in any CPP that needs it.
e.g. (note, this is just to show the idea how it's done, you should probably have accessor functions instead of making it a public member, but that's beside the point)
(MyClass.h)
class MyClass
{
public:
static int bob;
...
};
(MyClass.cpp)
int MyClass::bob = 0;
...
(SomeOther.cpp)
#include "MyClass.h"
...
void SomeFunc()
{
int number = MyClass::bob;
...
}
"When a man sits with a pretty girl for an hour, it seems like a minute. But let him sit on a hot stove for a minute and it's longer than any hour. That's relativity." - Albert Einstein
|
|
|
|
|
welp, i gave that a shot, but im getting unresolved external linker errors. i have mine set up exaclt like yours, the only difference is instead of SomeOther.cpp, i have SomeOther.h (figuratively speaking) i dont think that should make a difference...im including the header files in both places. when i say int MyClass::bob = 0; i get a linker error, and also when i try int number = MyClass::bob;.
any suggestions? i've double checked to ensure im including the header file with the declaration of my static int.
*.*
|
|
|
|
|
You do some thing like the following:
extern int g_iMyCurSel;
This can be placed in a header file or source file as long as it is defined in at least one source file. If you wish to place it in several different header files then I would recommend some thing like the following be placed in each one:
// Just in case
#ifndef __G_IMYCURSEL__
#define __G_IMYCURSEL__
extern int g_iMyCurSel;
#endif
Trust in the code Luke. Yea right!
|
|
|
|
|
this too gave me unresolved external symbols.
i put
extern int g_iMyCurSel;
into my header file,
//above the includes in my cpp file
#ifndef __G_IMYCURSEL__
//after the includes in my cpp file
#define __G_IMYCURSEL__
extern int g_iMyCurSel;
//somewhere in my cpp file...
g_iMyCurSel = 0;
//at the end of my cpp file
#endif
this gave me error, linker errors at that.
*.*
|
|
|
|
|
1) This is for use in your header files if and only if you wish to declare the same external variable in multiple header files. You would use it as it appears.
// Start of MyHeader.h
... // unknown code here
#ifndef __G_IMYCURSEL__
#define __G_IMYCURSEL__
extern int g_iMyCurSel;
#endif
... // unknown code here
// End of MyHeader.h
2) If you are going to declare it in 1 and only 1 header file then use:
// Start of MyHeader.h
... // unknown code here
extern int g_iMyCurSel;
... // unknown code here
// End of MyHeader.h
If you only want to declare it only in .cpp file but it needs to be accessed inline inside 1 or more header file then you must do the following before including those header files:
// Start of MyCPPFile.c
... // unknown code here
extern int g_iMyCurSel;
#include "MyHeaderFile.h" // header needing g_iMyCurSel
... // unknown code here
// End of MyCPPFile.c
Trust in the code Luke. Yea right!
|
|
|
|
|
1) Create a header file for the module (.cpp file) containing the global variable.
2) Declare the variable as follows in the header file for your module (.cpp file) as follows:
extern int g_iMyCurSel;
3) Include the header file for your module (.cpp file) before any other header files that needed to have access to your global variable.
Now any file that includes you modules (.cpp files) header file will have access to you global variable.
Trust in the code Luke. Yea right!
|
|
|
|
|
|
If there is a true need for it and you comment it then why not use a global?
There is a reason why global variables work.
Lots of people blindly assume that globals are "BAAAAAAAD!!!!" but if that variable is truly needed across multiple source files then just use a global and be done with it.
Buuuuuuuuuuuuuuuuuuuuuut - 99% of the time the reason you now "need" a global is that there is some structural faults with the design of your application.
I'm going to live forever or die trying!
|
|
|
|
|
Hi
How can I know that a set of VB function is part of COM object?
Ehsan Behboudi
|
|
|
|
|
i need to develop a basic firewall in vc++ which will restrict access to all ports except port 80. how should i go about it?
|
|
|
|
|
Why go thru the hassle? If closing off everything but port 80 is what you want, why not just use a commercially available product?
RageInTheMachine9532
|
|
|
|
|
Or, using W2K and XP, you can do port filtering right within the network driver setup.
onwards and upwards...
|
|
|
|
|
Hi I am working with several hardware, some of them support VC++ and others support VB.
Is there any way I could write a VC++ program and be able to call VB function in that as well?
Thanks
Ehsan Behboudi
|
|
|
|
|
If VB function is part of COM object yes, otherwise no.
|
|
|
|
|
How can I check that out?
I don't know much about COM object
The VB code is to control motion control/data aquisition hardware.
Ehsan Behboudi
|
|
|
|
|
hi,
You Can Use VB Functions In VC++ Program From VB Activex Dll. First Create VB Activex Dll as VB-GlobalMultiUse
Then Call VB Activex Dll From VC++
Leo12
|
|
|
|
|
how can I make a VB activeX DLL in .NET?
Ehsan Behboudi
|
|
|
|
|
Dear Friend,
You Can Go Through This Page
It Will Help How To Create an Activex Dll In Vb
and How To call From VC++
http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_11233681.html
|
|
|
|
|
Is there a function to get the HINSTANCE from a HWND???
I've got a COM DLL (which I use in VB) and I've made a function in it, but I need the HINSTANCE from the VB Form, I only know the HWND from the VB form and not the HINSTANCE so how can I get it??
|
|
|
|
|
Why do you need a HINSTANCE in VB?
As for you question you probably can use ::GetWindowThreadProcessId / ::EnumProcessModules and go from there
|
|
|
|
|
I'm making a game engine in C++ which you can use in VB and DirectInput needs an HINSTANCE to init so...
|
|
|
|
|
HINSTANCE hInst = GetWindowLong(hWnd,GWL_HINSTANCE);
Look up GetWindowLong() in the MSDN Library for usage.
Trust in the code Luke. Yea right!
|
|
|
|
|
I created a stuct in a class and I need a function that returns the pointer to that struct.
I need to be able to access the elements in the structure.
<br />
class MyClass{<br />
struct a {<br />
char arr[10];<br />
double dub[10];<br />
}<br />
<br />
<br />
function that returns the pointer to struct a<br />
void returnPointer(){<br />
return pointer?????;<br />
}<br />
}<br />
-------------------------<br />
<br />
(From another class)<br />
MyClass::returnPointer();<br />
a.arr[1];
Do I need to make a struct in the class that is calling the function in order to access the elements
by casting the pointer to the struct?
As you can see I am a bit lost on how to do this.
sj
|
|
|
|
|
if you asking about syntax
struct MyClass::a* returnPointer(){return pointer?????;}
if you asking what you should return - you need to declare private/protected member of that type and return pointer to it.
|
|
|
|