Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Can someone tell me what this syntax does please?

Reply
Thread Tools

Can someone tell me what this syntax does please?

 
 
George Styles
Guest
Posts: n/a
 
      01-15-2004
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


 
Reply With Quote
 
 
 
 
Rob Williscroft
Guest
Posts: n/a
 
      01-15-2004
George Styles wrote in news:HpuNb.4685021$:

> 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/
 
Reply With Quote
 
 
 
 
George Styles
Guest
Posts: n/a
 
      01-15-2004
Rob - thank you very much...

Much clearer now

George


 
Reply With Quote
 
jeffc
Guest
Posts: n/a
 
      01-15-2004

"George Styles" <> wrote in message
news:HpuNb.4685021$...
> 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.



 
Reply With Quote
 
Howard
Guest
Posts: n/a
 
      01-15-2004

"jeffc" <> wrote in message
news:...
>
> > 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


 
Reply With Quote
 
George Styles
Guest
Posts: n/a
 
      01-16-2004
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" <> wrote in message
news:bu76ie$...
>
> "jeffc" <> wrote in message
> news:...
> >
> > > 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
>
>



 
Reply With Quote
 
jeffc
Guest
Posts: n/a
 
      01-16-2004

"Howard" <> wrote in message
news:bu76ie$...
>
> "jeffc" <> wrote in message
> news:...
> >
> > > 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());
};


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can someone please tell me what this operator does? Pep C++ 5 09-23-2011 11:19 AM
CAN any one tell it whats the code tell it yogesh C++ 1 03-14-2007 01:12 PM
Can someone tell me what this is doing Richard Sloan Perl 1 05-19-2006 01:59 PM
HELP Can someone please tell me how to use a crossover cable! =?Utf-8?B?WmVscGhyaW4=?= Microsoft Certification 2 03-29-2006 10:20 PM
Can someone tell me why I can't delete this file? and why it blue screens WinXP Pro on delete? zZz Computer Support 1 01-12-2005 02:37 AM



Advertisments