Scope Resolution Operator

E

exits funnel

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
 
V

Victor Bazarov

exits funnel said:
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
 
D

Dan W.

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
 
D

Dan W.

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!
 
J

jeffc

exits funnel said:
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!
 
E

exits funnel

Thank you Victor, this clear it up nicely.

-exits

Victor said:
exits funnel said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top