473,785 Members | 2,326 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Scope resolution operator question

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

If compiled as C++ code, the scope resolution operator is used. I can
see how that can be useful in some circumstances, but I don't think I
need it for my particular situation. But I don't know, that's why I'm
posting.

I'm writing some simple wrappers in the following form:

// MyWrappers.h

#ifdef __cplusplus
extern "C"
{
#endif

__inline int MyWrapper()
{
// Global function: Any need for scope resolution operator?
return (int)SendMessag e( NULL, WM_USER, 0, 0 );
}

#ifdef __cplusplus
extern "C"
}
#endif

// eof

Is there any situation where not using the scope resolution operator
would cause problems?

Thanks,
Jack
May 12 '06 #1
6 5403
Jack wrote:
I want to write code that can be compiled as C and C++.
Exactly why?

Suggestion: Write in clean C, and your code will compile as C++.

When the time comes to ... do whatever you think your extra work will
achieve, then if your code is clean, you can easily upgrade to ... whatever.
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
There are just so many reasons that's not a good role model.
Is there any situation where not using the scope resolution operator
would cause problems?


The Windows API does that because of another bad MS decision. In MFC, a
class such as CWnd presents a member SendMessage(), with the same name. Many
OS-level objects have MFC representations , and these all duplicate their
function names as method names.

Of course SendMessage() soon dispatches to ::SendMessage() . So MS must
relentlessly disambiguate their SDK from their own MFC.

This is their burden. You should emulate very little of MFC, and you should
not replicate any of it. So if your own system has a C style wrapper on C++
code, or vice versa, it should give explicitely different names to different
functions. Even when they have a similar purpose.

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
May 12 '06 #2
On Fri, 12 May 2006 05:04:05 GMT, "Phlip" <ph******@yahoo .com> wrote:
Jack wrote:
I want to write code that can be compiled as C and C++.


Exactly why?

Suggestion: Write in clean C, and your code will compile as C++.

When the time comes to ... do whatever you think your extra work will
achieve, then if your code is clean, you can easily upgrade to ... whatever.
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


There are just so many reasons that's not a good role model.
Is there any situation where not using the scope resolution operator
would cause problems?


The Windows API does that because of another bad MS decision. In MFC, a
class such as CWnd presents a member SendMessage(), with the same name. Many
OS-level objects have MFC representations , and these all duplicate their
function names as method names.

Of course SendMessage() soon dispatches to ::SendMessage() . So MS must
relentlessly disambiguate their SDK from their own MFC.

This is their burden. You should emulate very little of MFC, and you should
not replicate any of it. So if your own system has a C style wrapper on C++
code, or vice versa, it should give explicitely different names to different
functions. Even when they have a similar purpose.

Language recommendations , explanations about MFC, and pedantic
opinions aside, all I want to know is this:
Will not using the scope resolution operator ever cause problems with
this function?

__inline int MyWrapper()
{
// Global function: Any need for scope resolution operator?
return (int)SendMessag e( NULL, WM_USER, 0, 0 );
}

Thanks,
Jack
May 12 '06 #3
Jack wrote:
Language recommendations , explanations about MFC, and pedantic
opinions aside, all I want to know is this:
Will not using the scope resolution operator ever cause problems with
this function?

__inline int MyWrapper()
{
// Global function: Any need for scope resolution operator?
return (int)SendMessag e( NULL, WM_USER, 0, 0 );
}


I just told you. You mistook it for a pedantic opinion.

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
May 12 '06 #4
Jack wrote:
Will not using the scope resolution operator ever cause problems with
this function?

__inline int MyWrapper()
{
// Global function: Any need for scope resolution operator?
return (int)SendMessag e( NULL, WM_USER, 0, 0 );
}


If MyWrapper() is in the global namespace, no, it wouldn't do anything
different. However, if MyWrapper() is in a namespace or in a class,
enclosing scopes will be searched for first and the wrong function
might get chosen.
Jonathan

May 12 '06 #5
On Fri, 12 May 2006 05:38:25 GMT, "Phlip" <ph******@yahoo .com> wrote:
Jack wrote:
Language recommendations , explanations about MFC, and pedantic
opinions aside, all I want to know is this:
Will not using the scope resolution operator ever cause problems with
this function?

__inline int MyWrapper()
{
// Global function: Any need for scope resolution operator?
return (int)SendMessag e( NULL, WM_USER, 0, 0 );
}


I just told you. You mistook it for a pedantic opinion.


You did? Maybe my news reader didn't download that part of your post.
Could you give me an example where the MyWrapper() function would have
problems because of its lack of a scope resolution operator?

Btw, I write code in C and C++. I need reusable code for both. I'm not
using MFC. I hate MFC. Please don't mention it again. I have MFC (and
lactose) intolerance. ;) The MyWrapper() function does not have the
same name as the global function it calls. The MyWrapper() function is
not a member function of a class.

Thanks,
Jack
May 12 '06 #6
On 11 May 2006 22:51:33 -0700, "Jonathan Mcdougall"
<jo************ ***@gmail.com> wrote:
If MyWrapper() is in the global namespace, no, it wouldn't do anything
different. However, if MyWrapper() is in a namespace or in a class,
enclosing scopes will be searched for first and the wrong function
might get chosen.


Yes, it is definitely global. I thought that made sense, but I needed
the confirmation. Thanks dude.

L8
May 12 '06 #7

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;
5
2656
by: exits funnel | last post by:
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;
7
2093
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
7
315
by: John Salerno | last post by:
I have the following code: class DataAccessFrame(wx.Frame): menu_items = def __init__(self): wx.Frame.__init__(self, None, title='Database Access Panel')
2
1869
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
1650
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
9647
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9489
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
10356
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
10162
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...
1
10100
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9959
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8988
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...
0
6744
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5396
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...

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.