Can someone tell me what this syntax does please?

Discussion in 'C++' started by George Styles, Jan 15, 2004.

  1. 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
     
    George Styles, Jan 15, 2004
    #1
    1. Advertising

  2. 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/
     
    Rob Williscroft, Jan 15, 2004
    #2
    1. Advertising

  3. Rob - thank you very much...

    Much clearer now :)

    George
     
    George Styles, Jan 15, 2004
    #3
  4. George Styles

    jeffc Guest

    "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.
     
    jeffc, Jan 15, 2004
    #4
  5. George Styles

    Howard Guest

    "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
     
    Howard, Jan 15, 2004
    #5
  6. 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
    >
    >
     
    George Styles, Jan 16, 2004
    #6
  7. George Styles

    jeffc Guest

    "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());
    };
     
    jeffc, Jan 16, 2004
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dildo Boy
    Replies:
    6
    Views:
    321
    Flash Gordon
    Mar 6, 2007
  2. E-Dot
    Replies:
    15
    Views:
    527
  3. Jim
    Replies:
    7
    Views:
    301
    christian.bau
    Mar 22, 2008
  4. Pep
    Replies:
    5
    Views:
    363
    Rui Maciel
    Sep 23, 2011
  5. What-a-Tool
    Replies:
    4
    Views:
    622
    What-a-Tool
    Jul 2, 2004
Loading...

Share This Page