473,725 Members | 2,341 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 5399
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
6476
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
2646
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
2091
by: Richard Hayden | last post by:
Hi, I have the following code for example: /**********************************/ #include <iostream> class C1 { public:
0
1093
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
4411
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
1827
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
1867
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
1646
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
8874
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
8748
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
9393
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
9248
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
6695
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
6000
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
4775
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3212
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
2622
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.