|
Normally when you overload an operator in a class, the left has side operand is taken as the caller.
So assume you have operator + overloaded in class A .
Consider the following code -
Class A
{
public:
void operator +(int i) {}
};
int main()
{
A Obj;
Obj + 3;
}
Here the operator call is going to be converted to Obj.operator+(3);
But if you write 3 + Obj it will fail because it becomes 3.operator+(Obj) which is illegal in C++.
Because of this what is normally done is to create two global functions of the forms -
void operator +(const A& obj, int i);
void operator +(int i, const A& obj);
Then these 2 operators are declared as friend functions of the class A so that the functions can access all members of the class A .
|
|
|
|
|
To define an operator out side of a class, you place "friend" before the return type.
class A {
public:
A(int x) :num(x) {}
~A() {}
int GetVal() {return num;}
friend std::ostream& operator<<(std::ostream& o,A& a) {return o << a.GetVal();}
private:
int num;
};
Which is the same as:
class A {
public:
A(int x) :num(x) {}
~A() {}
int GetVal() {return num;}
private:
int num;
};
std::ostream& operator<<(std::ostream& o,A& a) {return o << a.GetVal();}
modified on Tuesday, April 20, 2010 5:55 PM
|
|
|
|
|
Gwenio wrote: To define an operator out side of a class, you place "friend" before the return type.
Only when the 'outside' (i.e. global namespace's) operator (like any other global namespace method) needs to access class protected or private members.
Gwenio wrote: or you might do it like this (have not tried, but is should work):
class A {
public:
A(int x) :num(x) {}
~A() {}
int GetVal() {return num;}
private:
int num;
};
friend std::ostream& operator<<(std::ostream& o,A& a) {return o << a.GetVal();}
Why do you think it works?
I doesn't, of course.
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.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
I CString is defined as a friend to most of the MFC class which take strings as arguments
|
|
|
|
|
And so?
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.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
This is a part 2 on a question i posted earlier on this forum. The problem:
In a Keydown handler, when a user pressed the VK_MENU key an editbox is created. So far so good. Problem is that the standard behaviour of windows for a KEYUP-message of this key is to open a context menu. I was suggested:
1) to create the editbox in the keyup handler. This is not an option.
2) to swallow the keyup-message. This sounds good.
My question: how to swallow the keyup message? Is it setting up a local message dispatcher that dispatches messages till the keyup event comes in view, and then just trow it away? What happens to the keyboardstate then? I'm using C. A light on the right approach would be welcome...
Rozis
|
|
|
|
|
If you're fixated on WM_KEYDOWN, rather than WM_KEYUP, you have another problem to consider. When someone holds down a key, you get multiple down messages, followed by a single up message.
Are you requiring someone to hold down the menu button? If not, what's wrong with using the UP message? If you are requiring they hold down this button, then a) typing will be hard, and b) you could use the up button to dismiss your edit box.
Answering your question more directly, if you can handle the down message, can't you just handle the up message with 99% identical code:
case WM_KEYDOWN:
if (wParam == VK_MENU)
DoSomething ();
else
DoAnotherThing ();
break;
case WM_KEYUP:
if (wParam == VK_MENU)
;
else
DoYetAnotherThing ();
break;
You say you're using C, so I assume some win32 code stuff.
Yours trying to help but puzzled why your tying your shoe laces together,
Iain.
I have now moved to Sweden for love (awwww).
|
|
|
|
|
I've been curious and looked at your previous question.
I've made a small app, and it has an edit box.
Both right clicking in the edit box, and pressing the menu key end up generating a WM_CONTEXTMENU message. *This* is what pops up the menu.
You could subclass the edit window in question, and handle the WM_CONTEXTMENU message for that window. Handle it by doing very little.
The details of how you do that vary a lot depending on what programming framework you are using.
Iain.
I have now moved to Sweden for love (awwww).
|
|
|
|
|
Hi Iain,
Thank you for your answers, and the time to get the answer. Of course you're right about the button down vs button up. I have to reconcider that. But actually you found out how it really works (WM_CONTEXTMENU). So i'll track that one and throw in the void...
Thanks again
Rozis
|
|
|
|
|
Hi, all
I'm going to implement a hook program to get a snapshot of the dialog when the mouse button is click. But as we know, when the mouse is over a button, such as the "close" button, an effect of glow will be displayed and the snapshot I captured is including this effect. But I'd like to get a snapshot with out the effect of glow or other speciall effect of UI, so is there any idea that I can fix this problem.
Thanks a lot! 
|
|
|
|
|
I installed VS.2010 yesterday, and to my surprise our project build without any major hiccups!
I do notice now though, that after doing a full/clean compile, if I try to "build" again (which I assume will be an incremental build), it tends to build files that it thinks are stale, but in fact, are not. Nothing has changed to any of the projects.
Anyone seen anything like this?
I've done the usual, check file dates/times vs. system time etc.
Anyone have any tips you could send my way?
Cheers.
Mike.
|
|
|
|
|
|
Maybe you need CDateTimeCtrl::SetFormat().
|
|
|
|
|
|
See here.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
|
|
|
|
|
I have an mfc dialog containing a DateTimePicker,
I have assigned it the variable "picker",
I have formated the dates to year month
day with this command ...
picker.SetFormat("yyyymmdd");
|
|
|
|
|
I was wondering if anyone has any ideas on processing HTTP headers in C++.
|
|
|
|
|
I'm sure someone does. What exactly is your question? If you are unsure of how to ask this question, see here.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
|
|
|
|
|
The client sends an HTTP request, the server replies with an HTTP response.
I want to parse the HTTP response sent back from the server.
I thought I read an article, but I'm unable to find it again.
|
|
|
|
|
Fareed Rizkalla wrote: I thought I read an article, but I'm unable to find it again.
RFC2616[^]
|
|
|
|
|
Fareed Rizkalla wrote: I want to parse the HTTP response sent back from the server.
Ok, so what exactly is the problem?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
|
|
|
|
|
I am currently analyzing some nasty application crashes. Fortunately I can already reproduce these crashes within a few minutes and I get a dump that is supposed to be useful (note that it is a crash dump from an optimized release version). Let me outline the code where the application crashes (pseudo code):
int myNumberOfElements = myCalculation();
[...].
if(myNumberOfElements < 1 || myNumberOfElements > 100000)
{
return;
}
MyArray* pArray = AllocArray(myNumberOfElements);
AllocArray unfortunately belongs to a 3rd party library, but it basically calls malloc . It then crashes as it does not check accordingly if malloc succeeded. However, the root cause of this problem seems to be the large number of elements (which is definitely not the result of my calculation). Further there is only one assignment to myNumberOfElements and if used as parameter 'call by value' is used.
What do you guys think, is the dump file corrupt? Is there a way that the stack gets corrupted, causing this problem?
|
|
|
|
|
How are you analysing the dump file? Can show me a stack track for a start?
Steve
|
|
|
|
|
As posted in my other reply I use Visual Studio for analyzing the dumps. I would like to avoid posting a call stack as I think that my superiors would not be very happy finding parts of our code on codeproject (even if they are small and not useful to anyone).
|
|
|
|
|
A call stack is unlikely to give away trade secrets.
Steve
|
|
|
|