473,288 Members | 1,718 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,288 software developers and data experts.

Implementing a class of a nameless namespace outside the namespaceblock

This is possible:

namespace X { class A; }

class X::A { <implementation};

However, what about nameless namespaces? Does this do what I want?

namespace { class A; }

class ::A { <implementation};

Or does "::A" mean something else?
That compiles, but is it doing what I want? Note that this seems
to compile too:

namespace { class A; }

class A { <implementation};

I don't really understand what is the meaning of that.
Mar 20 '07 #1
7 2054
Juha Nieminen wrote:
This is possible:

namespace X { class A; }

class X::A { <implementation};

However, what about nameless namespaces? Does this do what I want?
It's not nameless, it's anonymous (its name is hidden from you). What's
the logical outcome of this classification on the terminology then?
>
namespace { class A; }

class ::A { <implementation};

Or does "::A" mean something else?
That compiles, but is it doing what I want? Note that this seems
to compile too:

namespace { class A; }

class A { <implementation};

I don't really understand what is the meaning of that.
Mar 20 '07 #2
Fei Liu wrote:
>Does this do what I want?
It's not nameless, it's anonymous (its name is hidden from you). What's
the logical outcome of this classification on the terminology then?
A "yes" or "no" answer would have been more helpful.
Mar 20 '07 #3
Juha Nieminen wrote:
namespace { class A; }

class A { <implementation};

For what I understand, an anonymous namespace is something like that:

namespace { class A;}

translates to something like:

namespace some_unique_name_generated_from_compiler { class A; }
using some_unique_name_generated_from_compiler;
Otherwise you wouldn't have access to an anonymous namespace. So I think
your second solution is quite right.
lg,
Michael
Mar 20 '07 #4
On Mar 20, 7:42 am, Juha Nieminen <nos...@thanks.invalidwrote:
Fei Liu wrote:
Does this do what I want?
It's not nameless, it's anonymous (its name is hidden from you). What's
the logical outcome of this classification on the terminology then?

A "yes" or "no" answer would have been more helpful.
Yes, there is something known as "unnamed" namespaces. If you have
access to Bjarne Stroustrup's "C++ Programming Language," he does a
good job of explaining it. The basic idea of unnamed namespaces is to
keep names within the unnamed namespace local to the translation unit
(i.e. source file) in which they were declared. What is unique about
unnamed namespaces is that when you use an unnamed namespace, there is
an implied using-directive. That way you can use the names that were
declared within the namespace, inside the same translation unit.

You also asked about this syntax:
class ::A { <implementation};

My understanding is that whenever you proceed a name with "::", that
refers to the global namespace, which would be different from the
unnamed namespace. Do an experiment. Try creating a class 'A' in the
global namespace and one in an unnamed namespace. Give them different
public members or methods, so you can easily tell them apart and see
how you can use them in the same source file.

Good luck!

Mar 20 '07 #5
Juha Nieminen wrote:
Fei Liu wrote:
>>Does this do what I want?
>It's not nameless, it's anonymous (its name is hidden from you). What's
the logical outcome of this classification on the terminology then?

A "yes" or "no" answer would have been more helpful.
What meaning does it hold for me to tell you 'yes' or 'no'. You would
blindly take someone else's answer over the internet? If you so disire,
then my answer is 'no'. I guess that's more helpful for you now...
Mar 20 '07 #6
Fei Liu wrote:
What meaning does it hold for me to tell you 'yes' or 'no'. You would
blindly take someone else's answer over the internet? If you so disire,
then my answer is 'no'. I guess that's more helpful for you now...
Then is it possible to do what I want? If yes, how?

It would seem odd to me that you can pre-declare a class in a
namespace and then declare/implement it outside of the namespace
block (with a "class TheNameSpace::TheClass { <implementation};"
kind of syntax) but you couldn't do the same with an anonymous
namespace.

One could justly ask why not just implement it inside the namespace
block, as it's just visible inside that compilation unit (ie. that
source file) anyways. It's just a question of indentation (I would
prefer being able to implement the class outside the namespace block
to avoid an extra indentation level).

Of course one could then ask why not use a named namespace (and
a 'using' to effectively give the same effect as the anonymous
namespace). Well, that feels like a slightly worse solution because
a named namespace is not local to the current compilation unit like
an unnamed one is.
Mar 21 '07 #7
On Mar 20, 6:40 am, Juha Nieminen <nos...@thanks.invalidwrote:
This is possible:

namespace X { class A; }

class X::A { <implementation};

However, what about nameless namespaces? Does this do what I want?

namespace { class A; }

class ::A { <implementation};

Or does "::A" mean something else?
That compiles, but is it doing what I want? Note that this seems
to compile too:
No, ::A is in the global scope, the other A is not - they do not refer
to the same "A" class.
namespace { class A; }

class A { <implementation};
Same as above.
I don't really understand what is the meaning of that.
Since an anoymous namespace has no name, there is no way to define any
member of anonymous namespace outside of the namespace, since there is
no name that can be used to qualify the definition (and that would at
the same time distinguish it from a definition in the current scope).
So any effort along these lines just ends up with a declaration in the
current scope and another one (with the same name) inside the
anonymous namespace.

Greg

Mar 21 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Robert | last post by:
Python doesn't know the class of a method when container not direct class attribute: >>> class X: .... def f():pass .... g=f .... l= .... >>> X.g <unbound method X.f>
6
by: Martyn Lawson | last post by:
Hi, I am currently working as an Analyst on a .NET Web Project using ASP.NET and C#.NET. I have a couple of, at least what should be, quick questions: 1. My understanding of UML says that...
4
by: marco_segurini | last post by:
Hi, the following test program shows a solution to a problem I have had. Now, this test program is compiled and linked by VS2003 and g++ while Comeau-on-line-compiler fails with this messages:...
3
by: Tony Johansson | last post by:
Hello! I have these two statements typedef const char *enum_txt; and enum_txt phase_tab ={"IDLE","LAUNCHING","LAUNCHED","ROLLING","BOOSTERRELEASE","SPACE"}; where in the class definition...
16
by: Eric | last post by:
I have a static class member variable as follows: struct A { static void Set (int i) { v = i; } static int& Get () { return v; } static int v; }; int A::v; // define A::v in the cpp file
3
by: John Goche | last post by:
Hello, I have come across the following directive but I don't see a class name specified in front of the ::Check function. Does anyone know what this means and how the directive is supposed to...
3
by: Stephen Torri | last post by:
Below is a class that is suppose to represent a segment of memory or a contents of a binary image (e.g. ELF executable). I have started to read Modern C++ Design and thought the best way to ensure...
3
by: Stephen Torri | last post by:
Here is my attempt at implementing a object factory. The purpose of this is to replace a large switch statement in a factory class with the functors. I get an error at line 88, marked, "expected...
1
by: rn5a | last post by:
If a namespace isn't supplied, which namespace does a class belong to? Is it a nameless global namespace or System? Thanks
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.