Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Why can not define a function in global namespace whit '::" ?

Reply
Thread Tools

Why can not define a function in global namespace whit '::" ?

 
 
parmenides
Guest
Posts: n/a
 
      08-21-2013
Hi,

We can define a member function for a class like this:

class foo{
....
void mfunc(void);
};

void foo::mfunc(void)
{
... ...
}

Why we can not define a global function like this:

void ::func(void)
{
... ...
}

The former is modified by '::", while the latter can not. I wonder it seems
an odd strategy.



 
Reply With Quote
 
 
 
 
Stefan Ram
Guest
Posts: n/a
 
      08-21-2013
"parmenides" <(E-Mail Removed)> writes:
>Why we can not define a global function like this:


That's the way C++ was defined.

To define an identifier in the global namespace, one defines
it outside of any enclosing namespace, class, or function.

So, for example:

#include <iostream>
#include <ostream>

int f(){ return 71; }
int main(){ ::std::cout << ::f() << '\n'; }

»f« is in the global namespace.

To define f in the namespace ::a, one uses:

namespace a { int f ...}

, thus:

#include <iostream>
#include <ostream>

namespace a { int f(){ return 71; }}
int main(){ ::std::cout << ::a::f() << '\n'; }

. If one now substitutes "a"-->"", one gets:

#include <iostream>
#include <ostream>

namespace { int f(){ return 71; }}
int main(){ ::std::cout << ::::f() << '\n'; }

, but this is no valid C++ (even, if it would be, the fully
qualified name of »f« would be »::::f«, not »::f«).
»namespace { ...« already has another meaning in C++.

 
Reply With Quote
 
 
 
Reply

Thread Tools

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
a = [ "1", "2", "3" ] v/s a = new Array ( "1", "2", "3" )identical in all ways? okey Javascript 1 08-25-2009 12:56 PM
Can define a class for namespace A in some other namespace B? Peng Yu C++ 0 09-14-2008 04:18 PM
Why are "Array#push" and "pop" not "push!" and "pop!"? samppi Ruby 27 12-05-2007 07:24 AM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
<FORM METHOD="post" onSubmit="return fieldcheck()" name="orientation" action="http://ws-kitty.BU.edu/AT/survey/orientation/script/write.asp" language="JavaScript"> Joeyej ASP .Net 0 06-04-2004 07:55 PM



Advertisments