|
Then you should rename your files to use .c extensions. If you use a compiler that supports C and C++, it will use C++ when detecting a .cpp extension and C compilation is not specified explicitely.
But the answer about the global variable is still the same.
|
|
|
|
|
Sakhalean wrote: How to do this?
You cant, period. You only have one variable called x. Chanhe its value and it changes, regrdless of where it is used.
==============================
Nothing to say.
|
|
|
|
|
Your question shows that you don't understand scope.
When you declare a variable using extern , it has global scope. Global scope means that all parts of your application will access the same variable when using that name, and there's only one variable and one value.
When you declare a variable in your .c file, but not elsewhere, this variable has local scope. Local scope means you can use it in this compilation unit only, and only from the point where you declare it, not before. It also means that when you use the same name in another .c file you get a compiler error, because the variable is not known there. You can declare another variable by the same name in another .c file. If you do that, that variable will be independent of the first one - you will have two different variables storing two different values.
If you want a variable that does not affect other parts of your application when you change it, you need a local variable
On a sidenote, it is a good practice to avoid variables with global scope. Prefer local variables over global ones wherever you can. Instead of using global variables, pass every value that a function requires as a fucntion argument, even if the only thing the function does with it is pass it on to another function.
|
|
|
|
|
Well, to be precise, declaring it outside of any code gives the variable global scope, using 'extern' just allows that symbol to be imported into the other source code file at compile time.
Stefan_Lang wrote: On a sidenote
Verging on dogma. A solution demands certain things, and with C, you use alot of globals. OK, you can get into a mess, and if that is the case the coder whould wonder about his ability, rather than what features of the language to use.
==============================
Nothing to say.
|
|
|
|
|
1. Yes and no. Syntacticaly you are correct. At least if you only have one .c file. But if you have more than one .c file, each file defines a local scope within the program. It's a wider scope than a function block or struct, but not as wide as a variable declared with 'extern' in a header.
2. Maybe. I am aware that many C program(mer)s like to use a lot of globals, and some mechanisms that make it easier to avoid globals in C++ are not available in C. Still, the problems caused by too many global variables is exactly the same in C as it is in C++. It's just that the effort to avoid them is on a different scale.
I've also noticed that long-time C programmers turning to C++ have a nasty habit to keep using lots of globals if there's no one around to keep them in check ('them' referring to both the globals and the programmers). Therefore I like to give out this advice to C and C++ programmers alike.
|
|
|
|
|
This is what gets me about C++ dogma. I have seen worse C++ code than C code. It is capale of being more obtuse, messy, confusing and dangerous.
Bad code is bad code, and dogma doesnt solve this, that why I write good code and leave the dogma at the church.
==============================
Nothing to say.
|
|
|
|
|
I assume what you're trying to do is to create a global variable in Main.cpp and make it read only in other .cpp files?
If so, here is what you can do.
Create the global variable as static in Main.cpp - static int x = 0;
Now create a function in Main.cpp that returns this value - int GetX() { return x; }
Now you can call this function from other .cpp files to get the value of x and also x can only be modified from Main.cpp.
Here you do not need to use the extern keyword at all.
Also to call GetX in other .cpp files, you will need to declare it there - int GetX();
|
|
|
|
|
...or, if you really want to mess with peoples' heads:
Leave main.cpp as is.
In the other files, declare extern const int x;
The compiler then won't let you modify x from these other files, but anyone looking at main.cpp could not tell why.
If you do this, you would have a readymade entry for the Coding Horrors forum!
Software rusts. Simon Stephenson, ca 1994.
|
|
|
|
|
I was going to answer that
I know I am coward since the day I know that fortune favors the brave
|
|
|
|
|
Hi,
In my application whenever alarms occur, we are playing sound.(.wav file)
For that
i include this set of code in one thread function.
void tRecvActiveData()
{
while(1)
{
if(bAlarmHHBeep)
PlaySound("IDR_LOWALM", hInst, SND_RESOURCE | SND_ASYNC)
}
}
when the application is running,this sound is playing continoulsy but not fully completed.
Only its playing a single beep(starting sound only).
It does not get completed fully.
That thread function is used for continous checking of alarm.
Pls kindly help me out..
|
|
|
|
|
add the SND_LOOP and SND_NOSTOP flags.
Independent ACN Business Owner- Check out the possibilities for your future!
- Financial independance
- Full time or Part time
- In more than 20 countries through North America, Europe, Asia and the Pacific
- Featuring the ACN IRIS 5000 video phone. See the person you are talking to.
Within you lies the power for good - Use it!
|
|
|
|
|
Thanks for your valuable support.It is working fine.
|
|
|
|
|
Hi,
I have 2 custom drawn oval buttons that initialized inside onInitDialog. The problem is that those buttons are trying to save the background of their position (to create their transparent mask) before the actual background is drawn to this dialog.
I draw the dialog's background inside the onPaint() instead of the onEraseBkgd() to avoid flickering, but onPaint() is called after the onInitDialog is over.
Any recommendations ?
Regards,
sdancer75
|
|
|
|
|
You can always use a static variable to save anything you need from the first OnPaint() call...
static bool FirstPaint = true;
if(FirstPaint){
FirstPaint = false;
}
|
|
|
|
|
Thank you Albert for your answer, but onPaint() is comming after the custom draw of the button.
So the buttons are initialized inside the onInitDialog do everything they need and after that is called the onPaint() method.
What I need is to call onPaint() before any button initialization inside the onInitDialog.
Regards,
sdancer75
|
|
|
|
|
sdancer75 wrote: onPaint() is comming after the custom draw of the button
It shouldn't... any custom drawing should take place within OnPaint(). That is the proper place to do it, hence the name. If you're doing it elsewhere, well, don't.
|
|
|
|
|
Thank you Albet,
The custom draw is done inside the onDraw of the button class, so this is preceding by default the OnPaint() of the parent window.
Anyway the solution I found is to delay the onDraw using a flag variable inside the OnEraseBkgnd() of the button class, that always executed after the OnPaint() of the parent class.
Best Regards,
sdancer75
|
|
|
|
|
Need help creating a scrollview like MSWord or Adobe Reader. What I mean to do is - create a view which displays like a centered A4 piece of paper. (Also like Print Preview).
Can you please provide a link to a sample application that does this?
Thanks
bart
|
|
|
|
|
|
Hi all. Which is the best way to disable OnFileNew, OnFileOpen and OnFileSave(As) in a SDI application ? Beside, I wish to dispaly in status bar a custom message ( "You can't open a document" or something ... )
I found a little solution to disable menu item (CCmdUI* ... ) but I want to set a message in status bar before ....
|
|
|
|
|
Just add handlers for ID_FILE_NEW , ID_FILE_OPEN , ... to your class, and update the status bar from within your functions. And, of course, don't call the corresponding CWinApp handlers OnFile... from your own handlers.
|
|
|
|
|
CStatusBar[^] has an explanation of the sequence to follow.
Unrequited desire is character building. OriginalGriff
I'm sitting here giving you a standing ovation - Len Goodman
|
|
|
|
|
Hi Developers,
I am developing a MDI based application. I want to show a transparent image. I have used Microsoft Activx Picture Control 2.0. But it's not showing the image transparently, it's coming with a gray background. In MFC picture controls is not supporting PNG image.
Can anyone help me to do it.
Thanks In Advance.
Amrit Agrawal, Mumbai.
|
|
|
|
|
Try it[^] in your own control's rendering...
They sought it with thimbles, they sought it with care;
They pursued it with forks and hope;
They threatened its life with a railway-share;
They charmed it with smiles and soap.
|
|
|
|
|
Friends, I use Visual Studio (2008) and created a project (MFC Doc-View application). I have to use some third party libraries (which has DLLs etc). How to use 'using' to make the program alright? I request a reliable reply.
If 'using' cannot be made use of in MFC, may I know how to invoke the DLLs.
My e-mail is rajeswarit@rediffmail.com
With many thanks,
Rajeswari
|
|
|
|