Scope Resolution Operator

Discussion in 'C++' started by exits funnel, Dec 12, 2003.

  1. exits funnel

    exits funnel Guest

    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
     
    exits funnel, Dec 12, 2003
    #1
    1. Advertising

  2. "exits funnel" <> wrote...
    > 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.


    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
    }

    >
    > //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, Dec 12, 2003
    #2
    1. Advertising

  3. exits funnel

    Dan W. Guest

    On Fri, 12 Dec 2003 04:56:58 GMT, exits funnel
    <> wrote:

    >Hello,
    >
    >I'm confused by the use of the Scope resolution operator on the


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

    >indicated lines in the following code (which was copied from Thinking in
    >C++ by Bruce Eckel). Removing them seems to have no effect.


    You haven't used Widget, that's why there's no effect. Try using it
    and you'll see....

    >
    >//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
     
    Dan W., Dec 12, 2003
    #3
  4. exits funnel

    Dan W. Guest

    >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!
     
    Dan W., Dec 12, 2003
    #4
  5. exits funnel

    jeffc Guest

    "exits funnel" <> wrote in message
    news:...
    > 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() { }


    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!
     
    jeffc, Dec 12, 2003
    #5
  6. exits funnel

    exits funnel Guest

    Thank you Victor, this clear it up nicely.

    -exits

    Victor Bazarov wrote:
    > "exits funnel" <> wrote...
    >
    >>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.

    >
    >
    > 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
    > }
    >
    >
    >>//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
    >>

    >
    >
    >
     
    exits funnel, Dec 13, 2003
    #6
    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. richard pickworth

    scope resolution operator

    richard pickworth, Jun 5, 2005, in forum: C++
    Replies:
    3
    Views:
    612
    richard pickworth
    Aug 8, 2005
  2. sushant

    scope resolution operator???????

    sushant, Jan 7, 2005, in forum: C Programming
    Replies:
    16
    Views:
    945
    Lawrence Kirby
    Jan 10, 2005
  3. Jack
    Replies:
    6
    Views:
    578
  4. dwaach
    Replies:
    1
    Views:
    412
    Victor Bazarov
    Jul 6, 2006
  5. heng

    scope resolution operator

    heng, Dec 6, 2006, in forum: C++
    Replies:
    4
    Views:
    391
    Default User
    Dec 6, 2006
Loading...

Share This Page