I have never seen anyone post a joke/game message so I'm going to now. Here are three games that I have written. There on my web site and I know there not that great but I want to shere them any way. You probably will not be interested in "grace game" but the other two may be kinda neat. Any comments are welcome.
"To an optimist the glass is half full.
To the pessimist the glass is half empty.
To the programmer the glass is twice as big as it needs to be."
GC13: "Mature-Ages 16 and up. 'your mature, right?'"
FARGORE: "no, but I am over 16."
in the case u describe it is used to pass the object by address or by reference.
It will depend on the signature of the called function. If I'm not mistaken this
function GetWindowRect expects a pass by address (a pointer) because its
based on the an API call which is written in plain C where there are no references
int iValue = 3;
int* pValue = &iValue; // pointer to value
*pValue = 4;
int& rValue = &iValue; // reference to value
rValue = 5;
Here you van see that when using a pointer (pValue) you need the to dereference it
using the '*' operator;
When using the reference (rValue) you can use it like you would have used iValue;
Now this brings the following benefit, look at the following code:
When using FunctionA the Point object will be copied onto the stack and
in the function you only use this copy. [pass by value]
When using FunctionB only the address is placed on the stack and inside the function
you can access the original object. So when changing the object inside the function is will
be changed outside the function (its the same object) [pass by address]
When using FunctionC alsoo only the address is placed on the stack and inside we are using the original object. So when changing the object inside the function is will
be changed outside the function. [pass by reference]
When passing large objects (classes with many members, or large ones) the pass by value is not so good (slow, stack space consuming). So either pass by address or pass by reference are preferred.
The reason why is? When windows was created using GDI things were written in good old C!
Unlike C++, C didnt use pass references, so you had to use pass by address.
Also since pass by Value such as :
CClientDC dc(this);<br />
CPen penBlue = new CPen();<br />
dc.SelectObject(penBlue) // pass by value, makes a copy - uses up a lot of memory (BAD)<br />
//instead use<br />
dc.SelectObject(&penBlue) // pass by address - fast and effecient, using original object (GOOD)<br />
also you could use<br />
CPen& rPenBlue = &penBlue;<br />
dc.SelectObject(rPenBlue) // not sure if this is right but pass by reference<br />
I think I understand some of it now, but theres still some things that elude me.
An example is
CClient* pDC(this);<br />
CPen penRed = new CPen()<br />
penRed.Create(PS_SOLID,1,RGB(255,0,0); // Create a solid red pen<br />
CPen penOld = dc->SelectObject(&penRed); // Select the red pen and store the old pen in // penOld, but why use the address of it?<br />
dc.DrawText(300,400,penRed,"Hello World"); // Print Hello World<br />
dc.SelectObject(penOld) //Select the old object using the pointer<br />
I still am a bit confused like the previous snippet
CPen *penRed = new CPen();
penRed->Create(PS_SOLID,1,RGB(255,0,0)); // Create a solid red penCPen *penOld = dc->SelectObject(penRed); // Select the red pen and store the old pen in // penOld, but why use the address of it?//dc.DrawText(300,400,penRed,"Hello World"); // Print Hello World
dc.SelectObject(penOld) //Select the old object using the pointer
I've marked up the code you posted. Notice the bold areas.
1. The new operator returns the address of an object allocated on the heap. The value on the left side of the assignment therefore has to be a pointer variable.
2. If you refer to a member of an object through a pointer, you have to use the -> operator, as in penRed->Create.
3. The SelectObject function in the CDC class takes a pointer to a CGdiObject.
4. The CDC class doesn't have a DrawText member that takes a pen argument.
The function declaration for GetWindowRect is CWnd::GetWindowRect(LPRECT lpRect) const; so it takes a pointer to a RECT structure as it's parameter. The reason why you can pass a CRect object, either as a pointer or by 'reference' (remember that the values of the window rectangle are copied into the CRect object, so you can not pass it in using the copy c'tor) is because the CRect class is derived from the RECT structure and has member operator LPRECT. If you pass it by address, GetWindowRect() assumes that you passed in the address of a RECT structure, or the base class, and it works from there. If you pass it by reference, then the CRect::operator LPRECT() kicks in to pass it's own address ( return this; ).
It's all in the magic of overloaded operators. So the difference between the two examples you gave is that there is really no difference functionally, although passing by address will save a few CPU cycles as it saves a call to the CRect::operator LPRECT
You may be right
I may be crazy
But it just may be a lunatic you’re looking for
-- Billy Joel --
In C, variables can be passed to a function either by-value or by-reference. For example:
// pass-by-valuevoid foo( int num )
num = 12;
int x = 5;
printf("%x\n", x); // will print 5
printf("%x\n", x); // will also print 5// pass-by-referencevoid foo( int *num )
*num = 12; // change the value pointed to by 'num'
int x = 5;
printf("%x\n", x); // will print 5
printf("%x\n", x); // will now print 12
Another consideration is the amount of memory pushed onto the stack when one or the other is used. For integral types such as int, char, and double, there is little, if any, difference between the two. A char is 1 byte, a double is 8 bytes, and a pointer to either is 4 bytes. Where this matters more is when large data structures are being passed to a function. Imagine an object that is 1497 bytes in size. Passing the object by-value will result in 1497 bytes being piushed onto the stack, whereas passing the object by-reference will result in only 4 bytes being pushed onto the stack.
"Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain
"There is no death, only a change of worlds." - Native American Proverb
I have some message boxes that are triggered from some buttons on a dialog. The dialog is set to open in full screen mode. The funny thing is that when the message boxes are triggered they get focus but they go behind my dialog.
Any idea why this is happening and how to get the message box on top of the dialog?
"Your message text goes here",
"Caption for your message box",
(MB_OK | MB_ICONINFORMATION));
In this case, CMyDialog is the name of your dialog class, derived from CDialog (which is in turn derived from CWnd). One of the members of all CWnd-derived objects is m_hWnd, which is the window handle.