Scope Resolution Operator

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • exits funnel

    Scope Resolution Operator

    Hello,

    I'm confused by the use of the Scope resolution operator on the
    indicated lines in the following code (which was copied from Thinking in
    C++ by Bruce Eckel). Removing them seems to have no effect.

    //Begin Code
    #include <new> // Size_t definition
    #include <fstream>
    using namespace std;

    class Widget {
    enum { sz = 10 };
    int i[sz];
    public:
    Widget() { }
    ~Widget() { }
    void* operator new(size_t sz) {
    return ::new char[sz]; //I'm confused here
    }
    void operator delete(void* p) {
    ::delete []p; //and here
    }
    void* operator new[](size_t sz) {
    return ::new char[sz];// and here
    }
    void operator delete[](void* p) {
    ::delete []p; //yup, here as well.
    }
    };
    int main() { }
    //END Code

    Thanks in advance for any replies!

    -exits

  • Victor Bazarov

    #2
    Re: Scope Resolution Operator

    "exits funnel" <exitsfunnel@NO SPAMyahoo.com> wrote...[color=blue]
    > I'm confused by the use of the Scope resolution operator on the
    > indicated lines in the following code (which was copied from Thinking in
    > C++ by Bruce Eckel). Removing them seems to have no effect.[/color]

    It's an indicator (if nothing else) that the function used will
    be from the global namespace. This code illustrates the use of
    the name resolution that actually changes the behaviour of the
    program:

    int foo(int) {
    return 42;
    }

    struct bar {
    int foo(int a) {
    return ::foo(a + 1); // remove the '::' and you have
    // infinite recursion
    }
    };

    int main() {
    bar b;
    b.foo(13); // step into this function in debugger to see
    // which one is called when
    }
    [color=blue]
    >
    > //Begin Code
    > #include <new> // Size_t definition
    > #include <fstream>
    > using namespace std;
    >
    > class Widget {
    > enum { sz = 10 };
    > int i[sz];
    > public:
    > Widget() { }
    > ~Widget() { }
    > void* operator new(size_t sz) {
    > return ::new char[sz]; //I'm confused here
    > }
    > void operator delete(void* p) {
    > ::delete []p; //and here
    > }
    > void* operator new[](size_t sz) {
    > return ::new char[sz];// and here
    > }
    > void operator delete[](void* p) {
    > ::delete []p; //yup, here as well.
    > }
    > };
    > int main() { }
    > //END Code
    >
    > Thanks in advance for any replies!
    >
    > -exits
    >[/color]


    Comment

    • Dan W.

      #3
      Re: Scope Resolution Operator

      On Fri, 12 Dec 2003 04:56:58 GMT, exits funnel
      <exitsfunnel@NO SPAMyahoo.com> wrote:
      [color=blue]
      >Hello,
      >
      >I'm confused by the use of the Scope resolution operator on the[/color]

      There's no such thing as a 'scope resolution operator'.
      [color=blue]
      >indicated lines in the following code (which was copied from Thinking in
      >C++ by Bruce Eckel). Removing them seems to have no effect.[/color]

      You haven't used Widget, that's why there's no effect. Try using it
      and you'll see....
      [color=blue]
      >
      >//Begin Code
      >#include <new> // Size_t definition
      >#include <fstream>
      >using namespace std;
      >
      >class Widget {
      > enum { sz = 10 };
      > int i[sz];
      >public:
      > Widget() { }
      > ~Widget() { }
      > void* operator new(size_t sz) {
      > return ::new char[sz]; //I'm confused here
      > }
      > void operator delete(void* p) {
      > ::delete []p; //and here
      > }
      > void* operator new[](size_t sz) {
      > return ::new char[sz];// and here
      > }
      > void operator delete[](void* p) {
      > ::delete []p; //yup, here as well.
      > }
      >};
      >int main() { }
      >//END Code
      >
      >Thanks in advance for any replies!
      >
      >-exits[/color]

      Comment

      • Dan W.

        #4
        Re: Scope Resolution Operator

        >There's no such thing as a 'scope resolution operator'.

        Doh! I read 'scope resolution' but thought 'overload resolution'...

        I need some sleep. Good night!

        Comment

        • jeffc

          #5
          Re: Scope Resolution Operator


          "exits funnel" <exitsfunnel@NO SPAMyahoo.com> wrote in message
          news:3FD9550F.4 050106@NOSPAMya hoo.com...[color=blue]
          > Hello,
          >
          > I'm confused by the use of the Scope resolution operator on the
          > indicated lines in the following code (which was copied from Thinking in
          > C++ by Bruce Eckel). Removing them seems to have no effect.
          >
          > //Begin Code
          > #include <new> // Size_t definition
          > #include <fstream>
          > using namespace std;
          >
          > class Widget {
          > enum { sz = 10 };
          > int i[sz];
          > public:
          > Widget() { }
          > ~Widget() { }
          > void* operator new(size_t sz) {
          > return ::new char[sz]; //I'm confused here
          > }
          > void operator delete(void* p) {
          > ::delete []p; //and here
          > }
          > void* operator new[](size_t sz) {
          > return ::new char[sz];// and here
          > }
          > void operator delete[](void* p) {
          > ::delete []p; //yup, here as well.
          > }
          > };
          > int main() { }[/color]

          I think it has no effect because you've just compiled that code. You
          haven't actually called new. I imagine it would be a recursive function,
          such as:
          void f()
          {
          f();
          }
          int main()
          {
          f();
          }
          Try running that and see how it goes!


          Comment

          • exits funnel

            #6
            Re: Scope Resolution Operator

            Thank you Victor, this clear it up nicely.

            -exits

            Victor Bazarov wrote:[color=blue]
            > "exits funnel" <exitsfunnel@NO SPAMyahoo.com> wrote...
            >[color=green]
            >>I'm confused by the use of the Scope resolution operator on the
            >>indicated lines in the following code (which was copied from Thinking in
            >>C++ by Bruce Eckel). Removing them seems to have no effect.[/color]
            >
            >
            > It's an indicator (if nothing else) that the function used will
            > be from the global namespace. This code illustrates the use of
            > the name resolution that actually changes the behaviour of the
            > program:
            >
            > int foo(int) {
            > return 42;
            > }
            >
            > struct bar {
            > int foo(int a) {
            > return ::foo(a + 1); // remove the '::' and you have
            > // infinite recursion
            > }
            > };
            >
            > int main() {
            > bar b;
            > b.foo(13); // step into this function in debugger to see
            > // which one is called when
            > }
            >
            >[color=green]
            >>//Begin Code
            >>#include <new> // Size_t definition
            >>#include <fstream>
            >>using namespace std;
            >>
            >>class Widget {
            >> enum { sz = 10 };
            >> int i[sz];
            >>public:
            >> Widget() { }
            >> ~Widget() { }
            >> void* operator new(size_t sz) {
            >> return ::new char[sz]; //I'm confused here
            >> }
            >> void operator delete(void* p) {
            >> ::delete []p; //and here
            >> }
            >> void* operator new[](size_t sz) {
            >> return ::new char[sz];// and here
            >> }
            >> void operator delete[](void* p) {
            >> ::delete []p; //yup, here as well.
            >> }
            >>};
            >>int main() { }
            >>//END Code
            >>
            >>Thanks in advance for any replies!
            >>
            >>-exits
            >>[/color]
            >
            >
            >[/color]

            Comment

            Working...