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

Scope Resolution Operator

P: n/a
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

Jul 22 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
"exits funnel" <ex*********@NOSPAMyahoo.com> 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

Jul 22 '05 #2

P: n/a
On Fri, 12 Dec 2003 04:56:58 GMT, exits funnel
<ex*********@NOSPAMyahoo.com> 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


Jul 22 '05 #3

P: n/a
>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!

Jul 22 '05 #4

P: n/a

"exits funnel" <ex*********@NOSPAMyahoo.com> wrote in message
news:3F**************@NOSPAMyahoo.com...
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!
Jul 22 '05 #5

P: n/a
Thank you Victor, this clear it up nicely.

-exits

Victor Bazarov wrote:
"exits funnel" <ex*********@NOSPAMyahoo.com> 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



Jul 22 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.