|
Could be, i already have one of those 10000 questions in my mind: "What the hell does he mean by 't'?"
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> Sometimes you just have to hate coding to do it well. <
|
|
|
|
|
|
#include <iostream.h>
struct structA
{
int iMember;
char *cMember;
};
int main(int argc, char* argv[])
{
structA instant1,instant2;
char c = 'a';
instant1.iMember = 1;
instant1.cMember = &c;
instant2 = instant1;
cout << *(instant1.cMember) << endl;
*(instant2.cMember) = 'b';
cout << *(instant1.cMember) << endl;
getchar();
}
|
|
|
|
|
wbgxx wrote: char c = 'a';
-here you make c equal to 'a'
wbgxx wrote: instant1.cMember = &c;
-here you make the cMember pointer of instant1 point at the variable c, so instant1.cMember now contains the memory location of c
wbgxx wrote: instant2 = instant1;
-here you copy the contents of instant1 into instant2, so the members in instant2 (iMember, cMember) will contain the same values as the corresponding ones in instant1. This means, instant2.cMember now also points at your c variable, both instant1.cMember and instant2.cMember contains the memory address of the variable c.
wbgxx wrote: cout << *(instant1.cMember) << endl; -here you say "print out the value that is stored in the memory location pointed at by instant1.cMember", this memory location happens to be c's, and since you set c to 'a', it will print 'a'.
wbgxx wrote: *(instant2.cMember) = 'b'; - here you say, "copy the value 'b' to the memory location pointed at by instant2.cMember", which is -as you remember- the same as instant1.cMember, the address of your c variable.
wbgxx wrote: cout << *(instant1.cMember) << endl; - here you say now, "print out the value that is stored in the memory location pointed at by instant1.cMember" again, but since this memory location is the same as the one stored in instant2, and previously you changed the value stored in that memory location to 'b', it will print 'b'.
This is basic pointer stuff, i recommend reading some good books or checking out online tutorials about pointers in C/C++...
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> Sometimes you just have to hate coding to do it well. <
|
|
|
|
|
When you do instant2 = instant1; it does a bitwise copy.
This means instant1 and instant2 will be exactly alike.
Which means the address pointed to by instant1.cMember and instant2.cMember will be the same.
And so changing the value using any of the pointers will reflect in both the pointers.
This is a classic example of why we need a copy constructor and assignment operator.
|
|
|
|
|
wbgxx wrote: cout << *(instant1.cMember) << endl; //I want to know why change instant2 and the instant1 change!
I want to know why you think instant2 has changed; all you did is look at instant1
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
|
|
|
|
|
#include <iostream>
using namespace std;
#pragma pack(8)
struct example1
{
short a;
long b;
};
struct example2
{
char c;
example1 struct1;
short e;
};
#pragma pack()
int main()
{
example2 struct2;
cout << sizeof(example1) << endl;
cout << sizeof(example2) << endl;
cout << (unsigned int)(&struct2.struct1) - (unsigned int)(&struct2)
<< endl;
getchar();
}
|
|
|
|
|
I am not sure what your problem is, but i will guess.
wbgxx wrote: cout << sizeof(example1) << endl; //8
- here you wonder why you get 8 instead of 6 when short is suposed to be 2 bytes and long is suposed to be 4 bytes, so 2+4 is 6, not 8.
wbgxx wrote: cout << sizeof(example2) << endl; //16
- here you wonder why you get 16 when char is 1 byte, short is 2 byte and your struct1 is 8 bytes so you expect it to be 11 bytes altogether, not 16.
wbgxx wrote: cout << (unsigned int)(&struct2.struct1) - (unsigned int)(&struct2)
<< endl; //4
- here you wonder why you get 4 bytes instead of 1 byte, since there's only a char infront of the struct2 member which is suposed to be 1 byte long, not 4.
If i am right then see this[^] and this[^].
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> Sometimes you just have to hate coding to do it well. <
|
|
|
|
|
Code-o-mat has pointed in the right direction.
It has everything to do with the structure packing and the controlling #pragma pack directive.
In you're example it says #pragma pack(8) .
This means pack the struct on 8 byte boundaries.
For the structure example1 , short is 2 bytes and long is 4 bytes.
Here the compiler will reserve 4 bytes for the short so that it becomes 8 bytes total.
If you change the pragma to #pragma pack(1) , you should get the results as 6 and 9.
|
|
|
|
|
cout << (unsigned int)(&struct2.struct1) - (unsigned int)(&struct2)
<< endl;
and what is (unsigned int)(&struct2.struct1)means?
|
|
|
|
|
wbgxx wrote: (unsigned int)(&struct2.struct1
You first take the address of a variable... Then cast it to a number, so when cout "prints" it, it will be shown as a number, not some fancier thing.
As the answers to your (slightly) later question said, this is very basic C/C++ stuff - a book is better able to help you than a message board.
Pointers are one of that concepts that can explained for ages, but you'll wake up one morning and go "Oooooh!". Think of the difference between the place you live - and address written on a postcard to you.
Good luck with your journey.
Iain.
I have now moved to Sweden for love (awwww).
|
|
|
|
|
Windows explorer shows a tooltip if the contents of a column is not entirely visible. I'm trying to emulate that behaviour but when I move the tooltip to cover the clipped text I end up with a flashing tooltip. This seems to be because the tooltip is now under the cursor which causes it to be closed immediately which leaves the cursor back over the clipped text which fires the tooltip and around we go.
Has anyone run into this before and found a solution?
|
|
|
|
|
Just a guess but try adding the WS_EX_TRANSPARENT style to your tooltip and see if it changes anything, don't know what it might screw up though...
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> Sometimes you just have to hate coding to do it well. <
|
|
|
|
|
I tried something like this, but no success:
SendMessage(WM_SYSKEYDOWN, VK_MENU,NULL);
Any ideas?
modified on Tuesday, May 11, 2010 10:32 AM
|
|
|
|
|
Don't know what exactly you wish to achieve, but could SendKeys[^] do the job?
[EDIT] Oups, that link should be keybd_event[^], not SendKeys...[/EDIT]
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> Sometimes you just have to hate coding to do it well. <
modified on Tuesday, May 11, 2010 11:57 AM
|
|
|
|
|
keybd_event worked for me. I just thought by doing SendMessage would do the same thing. Oh well...
|
|
|
|
|
Oups, i just noticed, i pasted the wrong link there in my post, in fact i meant keybd_event[^] too...sorry, but i guess you got it already.
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> Sometimes you just have to hate coding to do it well. <
|
|
|
|
|
#include <iostream>
#include <string>
using namespace std;
class CBuffer
{
char * m_pBuffer;
int m_size;
public:
CBuffer()
{
m_pBuffer=NULL; }
~CBuffer()
{
Free();
}
void Allocte(int size) (3) {
m_size=size;
m_pBuffer= new char[size];
}
private:
void Free()
{
if(m_pBuffer!=NULL)
{
delete m_pBuffer;
m_pBuffer=NULL;
}
}
public:
void SaveString(const char* pText) const
{
strcpy(m_pBuffer, pText);
}
char* GetBuffer() const
{
return m_pBuffer;
}
};
int main ()
{
CBuffer buffer1;
buffer1.SaveString("Microsoft");
printf("%s", buffer1.GetBuffer());
return 0;
}
|
|
|
|
|
And what is the problem exactly ?
|
|
|
|
|
|
What I meant is: try to give more details. "It doesn't work" is way too vague. Did you have a compilation error, a run-time crash, your computer exploded, ... ?
Keep in mind that we can't see what's on your screen.
|
|
|
|
|
wbgxx wrote: "Microsoft"
?
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]
|
|
|
|
|
In your constructor you set buffer to NULL.
In your main function you just create an instance of CBuffer and call SaveString.
SaveString accesses your buffer (what is NULL), so you get an access violation.
int main ()
{
CBuffer buffer1;
buffer1.Allocate(4096);
buffer1.SaveString("Microsoft");
printf("%s", buffer1.GetBuffer());
return 0;
}
Greetings
Covean
|
|
|
|
|
Maybe he needs a new pair of sneakers?
The wonderful thing about the Darwin Awards is that everyone wins, especially the members of the audience.
|
|
|
|
|
I want to create a round rect button with three image , one for left round, one for middle and one for right round. Middle image I m rendring according to with of button, but I don't know how to use the both side images so that my button sud look like reound rectangular.
Here is my code to render the middle image.
void CRoundButton2::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
// Get DC of Item
CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
ASSERT (pDC != NULL);
// Should Buttons be generated?
bool bGenerate = !m_rBtnSize.EqualRect(&lpDrawItemStruct->rcItem) || m_bRedraw;
// If Rectangles of Button are not the same
if (bGenerate)
{
// Generate Bitmap to hold Buttons
GenButtonBMPs(pDC, lpDrawItemStruct->rcItem);
// Redraw done
m_bRedraw = false;
}
// Generate DC to draw in Memory
CDC *MemDC = new CDC();
MemDC->CreateCompatibleDC(pDC);
HGDIOBJ hOldBmp = MemDC->SelectObject(m_tBmpBtn);
CString sActualCaption;
// Get actual caption
GetWindowText(sActualCaption);
// Check, if caption has changed
if (sActualCaption != m_sOldCaption)
bGenerate = true;
// Store old caption
m_sOldCaption = sActualCaption;
// If Rectangles of Button are not the same
if (bGenerate)
{
// Draw Buttons
DrawButtonFace(MemDC);
// Draw Button-Caption
DrawButtonCaption(MemDC);
}
int nButtonState;
nButtonState = BS_ENABLED;
if (m_bIsHotButton && m_bMouseOnButton)
nButtonState = BS_HOT;
if ((lpDrawItemStruct->itemState & ODS_DISABLED) == ODS_DISABLED)
nButtonState = BS_DISABLED;
else
{
if ((lpDrawItemStruct->itemState & ODS_SELECTED) == ODS_SELECTED)
nButtonState = BS_PRESSED;
else
{
if (this->m_bIsChecked)
{
nButtonState = BS_CLICKED;
}
}
}
CBitmap bitmap2;
bitmap2.LoadBitmap(IDB_BITMAP1);
BITMAP bmpInfo;
bitmap2.GetBitmap(&bmpInfo);
CBitmap* pOldBitmap = MemDC->SelectObject(&bitmap2);
//int nX = m_rBtnSize.left + (m_rBtnSize.Width() - bmpInfo.bmWidth) / 2;
//int nY = m_rBtnSize.top + (m_rBtnSize.Height() - bmpInfo.bmHeight) / 2;
// Copy correct Bitmap to Screen
for ( int i = 0 ; i<117 ; i++)
{
MemDC->SelectObject(bitmap2);
pDC->BitBlt(0+i,0,bmpInfo.bmWidth,bmpInfo.bmHeight, MemDC,0, 0, SRCCOPY);
//MemDC->SelectObject(hOldBmp);
MemDC->SelectObject(pOldBitmap);
}
UINT state = lpDrawItemStruct->itemState; //Get state of the button
if ( (state & ODS_SELECTED) ) // If it is pressed
pDC->DrawEdge(m_rBtnSize,EDGE_SUNKEN,BF_RECT); // Draw a sunken face
// else
// pDC->DrawEdge(rt,EDGE_RAISED,BF_RECT); // Draw a raised face
}
Please help me out..
Thanks in Advance
|
|
|
|