|
Member 754960 wrote: AFAIK, the only reason for this style is a compiler limitation. The language allows a class constant definition but the MSVC compiler (for one) doesn't handle it.
Actually you can use a class const, i.e. a const static member.
Member 754960 wrote: I seem to recall that using enums for constants is somewhat frowned upon
Why?
Member 754960 wrote: common practice though.
Of course (however enums have limitation, for instance they cannot be used for floating pointer consts).
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
[my articles]
|
|
|
|
|
I just tested this with the MSVS Team Server (MSVC 9) and, yes, you can define a class constant,
class A
{
static const int MAXBUFFERLEN = 1024;
char szBuffer[MAXBUFFERLEN];
public:
A() {}
~A() {}
};
which is a Good Thing seeing as I already claimed it. Which is "better" than declaring it then defining it in the *.cpp file.
Why are the enum less desireable? I would have to seek it out again but what springs to mind is type. Enumerations are types and should be treated as such and not as substitutes for something else. It may not be so but I would expect the compiler to complain if I substituted a enumerated value for an integer,
enum WeekDays
{
Monday = 0,
Tuesday = 1,
Wednesday = 2,
Thursday = 3,
Friday = 4,
Saturday = 5,
Sunday = 6,
NumberofWeekDays = 7
};
int Workdays[5];
void f()
{
int i = Workdays[Monday];
i = Monday;
}
Implicit conversion resolves this but should it? There is also the issue of invalid convesion. What does this mean:
void f(WeekDays aDay) {}
void g()
{
f(WeekDays(24));
}
When I first learned enumerations I learned never to count on their value. That may be out of date now but it strikes me how many applications will fail if the order or value of the enumerations changes. Very fragile. (Yet, at work we use this technique extensively; because we have to.) As a type, this seems contrary to what is desired. I want consistency and predictability. Monday comes before Tuesday; by how much doesn't matter. I can't multiply Monday by any value and get Friday. I can't make a Weekday from a sow's ear.
YMMV
|
|
|
|
|
Basically enum serves for state machine and option stuffs.
class ToolKit
{
enum State {
st_Idle,
st_Init,
st_Tx,
st_Rx,
st_Swap,
st_More
};
State m_State;
protected:
bool ThreadProcedure() {
switch(m_State) {
case st_Init:
break;
}
return true;
}
public:
bool OnButtonClickInitialize() {
if(Busy()) {
return Reject();
}
return NewTask(st_Init);
}
};
Maxwell Chen
|
|
|
|
|
Member 754960 wrote: a compiler limitation. The language allows a class constant definition but the MSVC compiler (for one) doesn't handle it.
If I recall correctly, ISO C++ (1997) supports class constants, but MSVCs haven't been quite handling this well. Older versions of MSVCs just supports static const members. (But I haven't checked this with 2008.)
class ToolKit
{
public:
const int n = 10;
};
class ToolKit
{
public:
ToolKit() : n(10) { }
const int n;
};
Maxwell Chen
|
|
|
|
|
You're right. This works in MSVC 9 and you can use the constant in the class declaration.
|
|
|
|
|
Wait!! These two are different as below!
1.
class ToolKit
{
static const int a = 5;
};
2.
class ToolKit
{
const int a = 5;
};
Maxwell Chen
|
|
|
|
|
Hi Leslie,
AFAIK you are supposed to follow a strict order in your #define and #include lines,
basically Windows headers should come first, third-party headers second, everything else
after that.
The official reason is newer Windows releases may modify or add to the header files
and suddenly create conflicts; of course this still can happen in the suggested order too,
but then it more looks like the problem is yours...
BTW: you can protect yourself against some mishaps by doing an #undef before you
do your own #define (you can #undef an undefined item, that does not constitute an error).
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips:
- before you ask a question here, search CodeProject, then Google;
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get;
- use PRE tags to preserve formatting when showing multi-line code snippets.
|
|
|
|
|
My application's primary interface is a notify icon in the systray.
If explorer.exe crashes, then the app's notify icon doesn't come back when the taskbar reappears.
However, some applications' icons do come back such as the Avast anti-virus, and SQL Server.
Is it possible that these other apps are receiving some sort of notification to recreate their icons?
The client might ask me to fix this by claiming it's a bug, which I would disagree with.
Do you think this can be classified as a bug? How is it that those other apps' icons are able to come back when explorer.exe restarts?
“Cannot find REALITY.SYS...Universe Halted.”
~ God on phone with Microsoft Customer Support
|
|
|
|
|
See the Extras section of this article.
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
Thanks! You saved me!
“Cannot find REALITY.SYS...Universe Halted.”
~ God on phone with Microsoft Customer Support
|
|
|
|
|
In addition to DavidCrow's reply....
On Vista, you may need to use ChangeWindowMessageFilter() to
get your app to receive the TaskbarCreated message in certain
situations.
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Thanks very much! I'll check into that!
“Cannot find REALITY.SYS...Universe Halted.”
~ God on phone with Microsoft Customer Support
|
|
|
|
|
Hi Mark. What are those situations?
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
I haven't tested, but apparently apps running as administrator
won't receive the message from non administrator apps, like explorer
generally is running.
It's probably rare that an administrative application is going to care
about that message anyway, but it's possible
I should really try it out one of these days - it's on my TODO list....i just
only seem to be able to crash the explorer when I don't want to. hmmm - just
killing the process should do it...
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Hello,
I try to understand memory management and pointers in C on a simple level right now.
Here is the task: concatenate 2 buffers (in this case buffA & buffB to buffAB). The values of the following code-snipets are small for debugging purposes ... imagine the size of each source buffer is much larger: how can I propper clean up the "mess" after creating buffAB?
<br />
char buffA[] = {'a'};<br />
char* p_buffA = &buffA[0];<br />
<br />
char buffB[] = {'b','b'};<br />
char* p_buffB = &buffB[0];<br />
<br />
char buffAB[] = {'x','x','x'};<br />
char* p_buffAB = &buffAB[0];<br />
<br />
memcpy(p_buffAB, p_buffA, 1);
memcpy(p_buffAB+1, p_buffB, 2);
<br />
<br />
I am thankfull for all suggesting to improve the code.
|
|
|
|
|
since you didn't dynamically allocate anything (with new or malloc, etc), there's nothing to clean up.
|
|
|
|
|
_NielsB wrote: // buffAB is the only thing we want ... how to clean up the rest?
clean up is called free in C language and works on dynamic allocated memory, i.e. it should pair a previous call to malloc (or another allocation function, like calloc ).
For instance:
char * pB = malloc(2);
pB[0]='b';
pB[1]='b';
char * pA = malloc(1);
pA[0]='a';
char * PAB = malloc(3);
memcpy(pAB, pA, 1 );
memcpy(pAB+1, pB, 2);
free(pA);
free(pB);
Note that I've omitted (to keep code compact) malloc return value check, but you must always do it in real programming, i.e.
char *p=malloc(10);
if ( ! p )
{
}
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
[my articles]
|
|
|
|
|
Hello again!
Thank you for your adwises
The real task I want to test is to append buffB to buffA. Asume there is a function-call that gives me 2 pointers to buffers (p_buffA and p_buffB) and the size of each of them (n_A and n_B). The function should retur the new pointer to buffA and the new size (f.e. saved in a struct).
Now I am using c++ new / delete to allocate and free memory.
<br />
int n_A = 30; <br />
char* p_buffA = new char[n_A];<br />
memset(p_buffA, (int)'a', n_A);<br />
<br />
int n_B = 50;<br />
char* p_buffB = new char[n_B];<br />
memset(p_buffB, (int)'b', n_B);<br />
<br />
int n_AB = n_A + n_B;<br />
char* p_buffAB = new char[n_AB];<br />
<br />
memcpy(p_buffAB, p_buffA, n_A);
memcpy(p_buffAB+n_A, p_buffB, n_B);
<br />
delete [] p_buffA;<br />
delete [] p_buffB;<br />
<br />
p_buffA = p_buffAB;<br />
n_A = n_AB;<br />
<br />
Is this snipet ok?
Are there better ways to solve this issue?
|
|
|
|
|
It is ok. But you have always check for pointer validity, for instance
char* p_buffA = new char[n_A];
if ( !p_buffA )
{
}
...
You have also the option to use realloc :
int n_A = 30;
char* p_buffA = (char*) malloc(n_A);
memset(p_buffA, (int)'a', n_A);
int n_B = 50;
char* p_buffB = (char*) malloc(n_B);
memset(p_buffB, (int)'b', n_B);
char* p_buffA = realloc[p_BuffA, n_A + n_B];
memcpy(p_buffA + n_A, p_buffB, n_B);
free(p_buffB);
(as stated above you've always to check return value of allocation function)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
[my articles]
|
|
|
|
|
Looks nice
Thnaks alot.
|
|
|
|
|
Trying to search the web in a loop, hitting the search engine with a new query with each iteration (with thousands of terms for this particular application). Don't know frankly if that's even allowed, as google kicks you off if you try to do it. But the following code below uses the COM IWebBrowser2 interface to an open InternetExplorer. Don't know if that is considered antiquated or there's a faster way to do it, because right now I'm just getting about a page per second. Is there a faster way than the following:
SHDocVw::IWebBrowser2Ptr brwsr = NULL;
.
.
.
brwsr = SHDocVw::IWebBrowser2Ptr(spDisp);
.
.
.
strcpy(srch_engn,"http://www.mysearch.com/search/GGmain.jhtml?searchfor");
sprintf(szURL,"%s=%s+%%22",srch_engn,q_misc);
char* szSrch = szURL+strlen(szURL);
while (1) {
strcpy(szSrch,getNextSrch(...));
BSTR bstr = ConvertStringToBSTR(szURL);
brwsr->Navigate(bstr);
VARIANT_BOOL busy;
READYSTATE rs;
while(1) {
Sleep(200);
brwsr->get_Busy(&busy);
if (!busy) break;
brwsr->get_ReadyState(&rs);
if (rs == READYSTATE_LOADED) break;
if (rs == READYSTATE_COMPLETE) break;
}
brwsr->Stop();
.
.
.
}
|
|
|
|
|
After the Stop statement above, here's how I process the returned page (as I guess that might be the bottleneck):
if (rsltDoc = IHTMLDocument2Ptr(brwsr->GetDocument())) {
HRESULT hr = rsltDoc->get_body(&body);
if (!FAILED(hr)) {
BSTR bstr_b;
hr = body->get_innerText(&bstr_b);
if (!FAILED(hr)) {
char* szBody = ConvertBSTRToString(bstr_b);
if (!strstr(szBody,szNotFound)) {
pf_fmt(szSrch);
fprintf(f,"%s\n\n",szSrch);
printf("%s\n\n",szSrch);
bMatch = TRUE;
}
delete [] szBody;
SysFreeString(bstr_b);
}
}
}
|
|
|
|
|
Hello
How can I disable/enable hibernation programatically in C++ in VS.NET ?
Or need to change registry values?
Thank you for any help ..will really help
modified on Saturday, January 12, 2008 3:08:48 PM
|
|
|
|
|
tibiz wrote: Or need to change registry values?
You can use RegSetValueEx() .
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
Thanx, I know about registry setting.
I'm trying using the GLOBAL_POWER_POLICY and if it will not help, maybe the reg. settings. Can you post me the place where to find the reg. entry ?
|
|
|
|
|