GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 30-Jan-2009, 02:13
Hasini Hasini is offline
New Member
 
Join Date: Jan 2009
Posts: 3
Hasini is on a distinguished road

How to implement a global function function in C++


Hi,

Could you tell me how to declare and implement a global function in C++, which can be accessed from any of the class in a particular application.

My requirement is to programmatically terminate a MFC application from a generic C++ class added to the application. I found the following method which is supposed to be implemented as a global function.

CPP / C++ / C Code:
void ExitMFCApp()
   {
        // same as double-clicking on main window close box
        ASSERT(AfxGetMainWnd() != NULL);
        AfxGetMainWnd()->SendMessage(WM_CLOSE);
   }

but I could not find the correct way to implement a global function.
Any help would be greatly appreciated.
Thank you very much in advance.
regards,
Haini.
Last edited by LuciWiz : 30-Jan-2009 at 07:19. Reason: Please insert your C++ code between [cpp] & [/cpp] tags
  #2  
Old 30-Jan-2009, 23:09
nowocien nowocien is offline
Awaiting Email Confirmation
 
Join Date: Nov 2008
Posts: 30
nowocien is an unknown quantity at this point

Re: How to implement a global function function in C++


well im just a begginer but I would assume create an implemintation file, ExitMFC.cpp for example or add the function to an existing one and declare the prototype in the header.h file used by all other modules?

DAn
  #3  
Old 30-Jan-2009, 23:25
Hasini Hasini is offline
New Member
 
Join Date: Jan 2009
Posts: 3
Hasini is on a distinguished road

Re: How to implement a global function function in C++


Hi,
Thanks for replying. But my concern is where in the header file I declare the prototype. If I declare a prototype of a function in a header file of a particular class, that function is accessible only within the scope of that class. But I need the function to be globally visible.
Thank you.
regards,
Hasini.
  #4  
Old 02-Feb-2009, 07:45
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 761
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: How to implement a global function function in C++


Quote:
Originally Posted by Hasini
If I declare a prototype of a function in a header file of a particular class, that function is accessible only within the scope of that class.
This is not true. You can mix class definitions and function prototypes as you wish. You can put multiple class definitions in the same header file if you wish. Here is an example:
CPP / C++ / C Code:
// MyHeader.h

// Global Function Prototype
void MyGlobalFunction();

// Class A
class A
{
public:
  int a;
};

// Class B
class B
{
public:
  int b;
};
You may also need to use the extern "C" {} syntax around your C code in order to avoid name mangling.
 
 

Recent GIDBlogProblems with the Navy (Enlisted) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
run script command on ns2.26 newbie06 Computer Software Forum - Linux 66 16-Jan-2010 10:53
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 07:44
Flex and bison coding lucky88star C++ Forum 5 24-Dec-2007 11:57
Need Help with input files. Efferus C++ Forum 2 24-Nov-2007 16:19
[Include] Doubly-linked List dsmith C Programming Language 6 14-Apr-2006 13:12

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 00:15.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.