C++ Learning Community Forum
August 01, 2010, 03:12:15 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: Hello. Smiley
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: When C Clashes with C++.  (Read 1253 times)
ih8censorship
Megalomaniac!!!
Administrator
C++ guru
*****
Posts: 1236



View Profile
« on: February 13, 2008, 04:12:23 AM »

Tonight i ran into an interesting issue. The naming convention i have been using, clashed with the name of a function from a C header that i wanted to use within a member function. So to fix it, I put the header in a namespace so i could call the right function. Heres a little code for illustration purposes:
Code
GeSHi (cpp):
namespace meh
{
#include <windows.h>
}
 
 
class foo
{
   public:
       void MessageBox();
 
};
 
 
void foo::MessageBox()
{
 meh::MessageBox(0,"hi","hello",0);
}
 
int main()
{
foo bar;
bar.MessageBox();
return 0;
}
Created by GeSHI 1.0.7.18

if anyone cares, the actual function that clashed with my method name was the write function from unistd.h . Thoughts? Right? Wrong? Smart or dumb?
Logged

PC==perfect_companion

Knowledge cannot come packaged and predigested; it must be chewed over carefully before swallowed.

What have you tried?
adeyblue
Dr. of C++ology
****
Posts: 653

Taming the turntables a beat at a time


View Profile WWW
« Reply #1 on: February 13, 2008, 01:13:44 PM »

The scope resolution operator ( :: ). If you've got a member function called write and you want to call the global one, it should disambiguate the two. This compiles fine.
Code
GeSHi (cpp):
#include <windows.h>
#include <iostream>
 
class Test
{
public:
   void WriteFile()
   {
       DWORD d;
       char a = 'h';
       ::WriteFile(NULL, &a, 1, &d, NULL);
   }
};
 
int main()
{
   Test t;
   t.WriteFile();
}
 
Created by GeSHI 1.0.7.18

I'm sure you know, but to anybody else it's a good idea not to have functions or classes with the same names as windows functions, since most of them are defined as macros which will bring in seemingly strange errors.
Code
GeSHi (cpp):
// example.cpp
class Example
{
public:
   void MessageBox();
};
 
#include <windows.h>
 
int doMsgBox()
{
   Example a;
   a.MessageBox(); // error, MessageBoxA/MessageBoxW is not a member of Example
}
 
Created by GeSHI 1.0.7.18
Logged

ih8censorship
Megalomaniac!!!
Administrator
C++ guru
*****
Posts: 1236



View Profile
« Reply #2 on: February 13, 2008, 08:15:38 PM »

Wow i just learned something  Grin I didn't know you could use the scope resolution operator like that to get to the global scope again. Cool!
Logged

PC==perfect_companion

Knowledge cannot come packaged and predigested; it must be chewed over carefully before swallowed.

What have you tried?
adeyblue
Dr. of C++ology
****
Posts: 653

Taming the turntables a beat at a time


View Profile WWW
« Reply #3 on: February 13, 2008, 08:51:50 PM »

You've not seen much MFC code have you Smiley
Logged

ih8censorship
Megalomaniac!!!
Administrator
C++ guru
*****
Posts: 1236



View Profile
« Reply #4 on: February 13, 2008, 10:17:38 PM »

Hm... I've seen enough of it to stay away from it   Cheesy
Logged

PC==perfect_companion

Knowledge cannot come packaged and predigested; it must be chewed over carefully before swallowed.

What have you tried?
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!