One more way windows can ruin your day

M

Marcin Kalicinski

Hi,

///////////////////////////////////////////////
// File Module.h

struct Object
{
static Object *GetObject(int i);
};

///////////////////////////////////////////////
// File Module.cpp

#include "Module.h"
#include <windows.h>

Object *Object::GetObject(int i)
{
return NULL;
}

Now invoke MS C++ compiler on module.cpp:

cl.exe module.cpp

And you get:

Module.cpp(7) : error C2039: 'GetObjectA' : is not a member of 'Object'
Module.h(5) : see declaration of 'Object'

Besides GetObject, they have also macro-redefined lots of other useful
names, such as 'DrawText' or 'MessageBox'. This is _really_ annoying.

Best regards,
Marcin
 
J

John Harrison

Besides GetObject, they have also macro-redefined lots of other useful
names, such as 'DrawText' or 'MessageBox'. This is _really_ annoying.

Simple, don't use camel notation

get_object, draw_text, message_box, you know it makes sense.

john
 
A

Andrey Tarasevich

John said:
Simple, don't use camel notation

get_object, draw_text, message_box, you know it makes sense.
...

Actually, it was Pascal notation :) So its more like "better _use_ camel
notation".
 
M

Mike Wahler

John Harrison said:
Simple, don't use camel notation

get_object, draw_text, message_box, you know it makes sense.

And more to the point, one needs to pay attention to any
libraries one is using(*), and not step on their names.
C++ namespaces help greatly with this. Since the Windows
API is not in a namespace (it's mainly intended as a C
interface), you can insulate your names from it by putting
your own code in a namespace(s) (except 'main()', that is).

<OT>
Marcin:

Those symbols 'GetObject', 'DrawText', 'MessagBox', are names
from the Windows API. Many of these are macros which expland
to the same names with either 'A' or 'W' suffixes depending upon
a #define, for choosing between ASCII and Unicode. That's
"Just The Way It Is". Those who don't like it can simply choose
not to use the API, but perhaps some 'wrapper' library.

(*)What, you're not using the Windows API? Then what's the
windows.h there for? I don't see anything in your code that
uses it. Remove it, and then you can use those symbols
as your own.

And if I'm writing code that *does* use the Windows API, I
don't use their 'camel case' for my own symbols, it makes
it obvious which symbols are from the API, and which not.
(except for the all-uppercase macro names of course).

</OT>


-Mike
 
J

Julie J.

Then don't include windows.h!

windows.h is just like *any* other C include file. Name collisions and and
will exist.
 

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

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top