473,796 Members | 2,740 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Jul 22 '05 #1
5 2656
"exits funnel" <ex*********@NO SPAMyahoo.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
On Fri, 12 Dec 2003 04:56:58 GMT, exits funnel
<ex*********@NO SPAMyahoo.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
>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

"exits funnel" <ex*********@NO SPAMyahoo.com> wrote in message
news:3F******** ******@NOSPAMya hoo.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
Thank you Victor, this clear it up nicely.

-exits

Victor Bazarov wrote:
"exits funnel" <ex*********@NO SPAMyahoo.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
6479
by: Anonymous | last post by:
Is namespace the same thing as scope? While reading the book "Thinking in C++", I was under the impression that namespace is, well, a namespace--a feature to create a hiearchy for identifiers within the Global Namespace. And that, all identifiers within a given namespace, however deeply nested they are, each of them has the Global Scope. So, if namespace outer { int a = 1;
7
2094
by: Richard Hayden | last post by:
Hi, I have the following code for example: /**********************************/ #include <iostream> class C1 { public:
0
1101
by: Howard Gardner | last post by:
/* I don't have to provide scope resolution in sit 0 below: adl finds the function that I mean to use. I do have to provide scope resolution in sits 1 and 2. Can anyone teach me a trick to get around the need for scope resolution in sit 1 or sit 2? sit 1 is my motivating case, but if there's a trick for sit 2 I'd like
3
4420
by: richard pickworth | last post by:
Hello :) I am familiar with using scope resolution operator to define classes, but why does it turn up elsewhere? thanks richard
16
1839
by: sushant | last post by:
hi , can we use scope resolution operator (::) in C??? sushant
6
5404
by: Jack | last post by:
I want to write code that can be compiled as C and C++. In the Windows API, there are many macros that help with scope resolution. For example: #ifdef __cplusplus #define SNDMSG ::SendMessage #else #define SNDMSG SendMessage #endif
1
2585
by: Steven T. Hatton | last post by:
All of the following terms are used in some way to describe where and how a name is relevant to a particular location in a program: visible, declarative region, scope, potential scope, valid, introduced, used, potentially evaluated and accessible. They all seem to have subtle difference in meanings. Any positive contribution to the following will be appreciated. Here is my attempt to make sense of these:
2
1870
by: xtrigger303 | last post by:
Hi to all, I was reading Mr. Alexandrescu's mojo article and I've a hard time understanding the following. Let's suppose I have: //code struct A {}; struct B : A {};
5
1651
by: Shan | last post by:
In the following code: func() { int SmeVar; { int SmeVar; //Here I want to use SmeVar defined in the outer block how can I do that ? }
0
9535
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10467
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10244
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9061
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7558
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5454
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5582
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4130
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.