By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,279 Members | 2,178 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,279 IT Pros & Developers. It's quick & easy.

Can someone tell me what this syntax does please?

P: n/a
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
Jul 22 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
George Styles wrote in news:Hp*********************@news.easynews.com:
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.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #2

P: n/a
Rob - thank you very much...

Much clearer now :)

George
Jul 22 '05 #3

P: n/a

"George Styles" <us****@ripnet.co.uk> wrote in message
news:Hp*********************@news.easynews.com...
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.

Jul 22 '05 #4

P: n/a

"jeffc" <no****@nowhere.com> wrote in message
news:40********@news1.prserv.net...
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.


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
Jul 22 '05 #5

P: n/a
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
"Howard" <al*****@hotmail.com> wrote in message
news:bu********@dispatch.concentric.net...

"jeffc" <no****@nowhere.com> wrote in message
news:40********@news1.prserv.net...
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.


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

Jul 22 '05 #6

P: n/a

"Howard" <al*****@hotmail.com> wrote in message
news:bu********@dispatch.concentric.net...

"jeffc" <no****@nowhere.com> wrote in message
news:40********@news1.prserv.net...
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.


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());
};
Jul 22 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.