Can someone tell me what this syntax does please?

G

George Styles

Hi,
I am trying to work out what a block of C++ does, but I am having trouble
understanding the syntax.
I know Delphi well, so am happy with objects etc, its just this syntax I
dont understand.

The code is


void CRipPanel::OnPaint()
{
CPaintDC dc(this); // device context for painting

CDC memDc;

if (!memDc.CreateCompatibleDC(&dc)) return;

m_bmpNormal.LoadBitmap(IDB_PANELBITMAP);

HBITMAP m_hOldBitmap = (HBITMAP)::SelectObject(memDc.GetSafeHdc(),
m_bmpNormal);
dc.BitBlt(0,0,240,80, &memDc, 0,0,SRCCOPY);
::SelectObject(memDc.GetSafeHdc(), m_hOldBitmap);
memDc.DeleteDC();
m_bmpNormal.DeleteObject();
}


I dont understand:

1. CPaintDC dc(this); // device context for painting

CPaintDC is a type right? dc is the instance, but what does the (this) mean
(i know this is the current object, but havnt seen it used in this way
before)

2. I dont understand the line starting :: (
::SelectObject(memDc.GetSafeHdc(), m_hOldBitmap);)
What object is that acting on? i have only seen :: in c++ before when
defining methods of a class, not in code like this.

It looks to me like some kind of WITH block.... but I didnt know c++
supported with...

help!

thanks
George
 
R

Rob Williscroft

George Styles wrote in
Hi,
I am trying to work out what a block of C++ does, but I am having
trouble understanding the syntax.
I know Delphi well, so am happy with objects etc, its just this syntax
I dont understand.

The code is


void CRipPanel::OnPaint()
{
[rearranged]

I dont understand:
CPaintDC dc(this); // device context for painting

This defines a variable dc of type CPaintDC and initializes it
with a pointer to the current object (this).

I would guess from the above that CPaintDC has a constructor like this:

CPaintDC::CPaintDC( CWindow *window );

Where CRipPanel is derived (possibly via another base class)
from CWindow:

class CRipPanel: public CWindow
{
// ...
};

[snip]
::SelectObject(memDc.GetSafeHdc(), m_hOldBitmap);

This call's the SelectObject() function declared in the global
namespace, such syntax is helpful when CRipPanel also has
a member function called SelectObject(), or there is a different
SelectOBject() visible from the current scope.

[snip]
2. I dont understand the line starting :: (
::SelectObject(memDc.GetSafeHdc(), m_hOldBitmap);)
What object is that acting on? i have only seen :: in c++ before when
defining methods of a class, not in code like this.

It looks to me like some kind of WITH block.... but I didnt know c++
supported with...

:: is the scope resolution operator, example:

namespace A
{
int value = 0;
};

namespace B
{
int value = 1;
}

int value = 2;

int f()
{
return value + A::value + B::value;
}

namespace C
{
using B::value;
int g()
{
return ::value + A::value + value;
}
}

In the above both f() ( ::f() ) and C::g() return the same sum:
the value defined in the global namespace + the value defined
in namespace A + the value defined in namespace B.

HTH.

Rob.
 
J

jeffc

George Styles said:
Hi,
I am trying to work out what a block of C++ does, but I am having trouble
understanding the syntax.
I know Delphi well, so am happy with objects etc, its just this syntax I
dont understand.

The code is


void CRipPanel::OnPaint()
{
CPaintDC dc(this); // device context for painting

CDC memDc;

if (!memDc.CreateCompatibleDC(&dc)) return;

m_bmpNormal.LoadBitmap(IDB_PANELBITMAP);

HBITMAP m_hOldBitmap = (HBITMAP)::SelectObject(memDc.GetSafeHdc(),
m_bmpNormal);
dc.BitBlt(0,0,240,80, &memDc, 0,0,SRCCOPY);
::SelectObject(memDc.GetSafeHdc(), m_hOldBitmap);
memDc.DeleteDC();
m_bmpNormal.DeleteObject();
}


I dont understand:

1. CPaintDC dc(this); // device context for painting

CPaintDC is a type right? dc is the instance, but what does the (this) mean
(i know this is the current object, but havnt seen it used in this way
before)

You don't show it, but I'll assume CPaintDC is a class. "this" isn't the
current object - rather it's a pointer to the current object. Therefore,
you can assume that it has a constructor that looks like:

CPaintDC(CRipPanel*)
2. I dont understand the line starting :: (
::SelectObject(memDc.GetSafeHdc(), m_hOldBitmap);)
What object is that acting on? i have only seen :: in c++ before when
defining methods of a class, not in code like this.

You might do this kind of thing if the base class and subclass (the class
you're in) both define the same function, so you want to specify that you
want to call the base class version.
 
H

Howard

jeffc said:
You might do this kind of thing if the base class and subclass (the class
you're in) both define the same function, so you want to specify that you
want to call the base class version.

I thought that that syntax calls the function in the global namespace, not
in any base class the current object might descend from. Am I wrong...?
-Howard
 
G

George Styles

I think its the global namespace thing, as ive had to use it elsewhere, and
it worked as a global namespace thing :)

thanks for the answers

George
 
J

jeffc

Howard said:
I thought that that syntax calls the function in the global namespace, not
in any base class the current object might descend from. Am I wrong...?

No, you're right. I was thinking of the case
class A
{
public:
void f();
};
class B : public A
{
public:
void f(A::f());
};
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top