473,406 Members | 2,620 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,406 software developers and data experts.

Is there 'private' in namespace?

Hi,

I'm wondering if there is something in namespace like the 'private'
keyword in class?

I want to define some class or function that can only be used within
that namespace.

Thanks,
Peng
Jul 24 '08 #1
17 30538
On Wed, 23 Jul 2008, Peng Yu wrote:
I want to define some class or function that can only be used within
that namespace.
I don't think it's possible to restrict access to a globally available
class. If you are looking for functions that can only be used within
namespaces, then use a class and static methods.
Jul 24 '08 #2
On Jul 24, 2:58 am, Peng Yu <PengYu...@gmail.comwrote:
I'm wondering if there is something in namespace like the
'private' keyword in class?
I want to define some class or function that can only be used
within that namespace.
Namespaces are open (anyone can add a member to a namespace,
anywhere), so there's no point in private.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jul 24 '08 #3
On Jul 24, 2:48 am, James Kanze <james.ka...@gmail.comwrote:
On Jul 24, 2:58 am, Peng Yu <PengYu...@gmail.comwrote:
I'm wondering if there is something in namespace like the
'private' keyword in class?
I want to define some class or function that can only be used
within that namespace.

Namespaces are open (anyone can add a member to a namespace,
anywhere), so there's no point in private.
I'd think a private in namespace might be useful.

For example, I'm code a class C in namespace N. To code C, I might
need define some classes or functions, which are not used by other
classes other than C. I would not want to defines these classes or
functions in side C, because if I do it this way, I would not be able
to separate them from C into two different files, which would make it
harder to test and understand the code.

Since these classes or functions are for C, I'd like to keep them in
the same namespace as C.

Since these classes or functions are not for other classes other than
C, ideally, I would like to specify somehow in C++ that they are for C
only. For now, what I can possibly think of is to add 'private' in
'namespace' such that at least classes outside N would not be able to
call these functions or classes.

Thanks,
Peng
Jul 24 '08 #4
On 24 Jul., 19:31, Peng Yu <PengYu...@gmail.comwrote:
On Jul 24, 2:48 am, James Kanze <james.ka...@gmail.comwrote:
On Jul 24, 2:58 am, Peng Yu <PengYu...@gmail.comwrote:
I'm wondering if there is something in namespace like the
'private' keyword in class?
I want to define some class or function that can only be used
within that namespace.
Namespaces are open (anyone can add a member to a namespace,
anywhere), so there's no point in private.

I'd think a private in namespace might be useful.

For example, I'm code a class C in namespace N. To code C, I might
need define some classes or functions, which are not used by other
classes other than C. I would not want to defines these classes or
functions in side C, because if I do it this way, I would not be able
to separate them from C into two different files, which would make it
harder to test and understand the code.

Since these classes or functions are for C, I'd like to keep them in
the same namespace as C.

Since these classes or functions are not for other classes other than
C, ideally, I would like to specify somehow in C++ that they are for C
only. For now, what I can possibly think of is to add 'private' in
'namespace' such that at least classes *outside N would not be able to
call these functions or classes.
Nothing prevents you from having code from one class reside in several
files. On a very few occasions (I remember only one, actually), I've
done exactly that.

/Peter
Jul 24 '08 #5
peter koch wrote:
Nothing prevents you from having code from one class reside in several
files. On a very few occasions (I remember only one, actually), I've
done exactly that.
The only problem is that the declaration of the class must contain
*everything* that belongs to that class declared in one single place,
while a namespace doesn't have that restriction.
Jul 24 '08 #6
On Jul 24, 7:31 pm, Peng Yu <PengYu...@gmail.comwrote:
On Jul 24, 2:48 am, James Kanze <james.ka...@gmail.comwrote:
On Jul 24, 2:58 am, Peng Yu <PengYu...@gmail.comwrote:
I'm wondering if there is something in namespace like the
'private' keyword in class? I want to define some class
or function that can only be used within that namespace.
Namespaces are open (anyone can add a member to a namespace,
anywhere), so there's no point in private.
I'd think a private in namespace might be useful.
You'll have to show what it might do that would be useful. It
certainly doesn't prevent anyone else from accessing it.
For example, I'm code a class C in namespace N. To code C, I
might need define some classes or functions, which are not
used by other classes other than C. I would not want to
defines these classes or functions in side C, because if I do
it this way, I would not be able to separate them from C into
two different files, which would make it harder to test and
understand the code.
I'm not following you. Typically, if you're writing library
code for concrete classes (no virtual functions), you'll have
each function in a separate file anyway.
Since these classes or functions are for C, I'd like to keep
them in the same namespace as C.
So put them in the same namespace as C. If they're not for
public consumation, I'll typically put them in a special
namespace, however, with a name indicating that they aren't for
public consumation.
Since these classes or functions are not for other classes
other than C, ideally, I would like to specify somehow in C++
that they are for C only. For now, what I can possibly think
of is to add 'private' in 'namespace' such that at least
classes outside N would not be able to call these functions
or classes.
Except that they would be able to. Given that namespaces are
open, making something private in a namespace would change
absolutely nothing.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jul 24 '08 #7
On Jul 24, 9:17 pm, Juha Nieminen <nos...@thanks.invalidwrote:
peter koch wrote:
Nothing prevents you from having code from one class reside
in several files. On a very few occasions (I remember only
one, actually), I've done exactly that.
The only problem is that the declaration of the class must
contain *everything* that belongs to that class declared in
one single place, while a namespace doesn't have that
restriction.
Which brings us back where we started: that's why private makes
no sense for namespaces.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jul 24 '08 #8
On Jul 24, 2:56 pm, James Kanze <james.ka...@gmail.comwrote:
On Jul 24, 7:31 pm, Peng Yu <PengYu...@gmail.comwrote:
On Jul 24, 2:48 am, James Kanze <james.ka...@gmail.comwrote:
On Jul 24, 2:58 am, Peng Yu <PengYu...@gmail.comwrote:
I'm wondering if there is something in namespace like the
'private' keyword in class? I want to define some class
or function that can only be used within that namespace.
Namespaces are open (anyone can add a member to a namespace,
anywhere), so there's no point in private.
I'd think a private in namespace might be useful.

You'll have to show what it might do that would be useful. It
certainly doesn't prevent anyone else from accessing it.
For example, I'm code a class C in namespace N. To code C, I
might need define some classes or functions, which are not
used by other classes other than C. I would not want to
defines these classes or functions in side C, because if I do
it this way, I would not be able to separate them from C into
two different files, which would make it harder to test and
understand the code.

I'm not following you. Typically, if you're writing library
code for concrete classes (no virtual functions), you'll have
each function in a separate file anyway.
What I meant was that a walkaround is to put these functions and
classes inside the class C and make them private.
This way make these functions and classes inaccessible by others, but
it also make it difficult to test them because you can not access them
from outside the class C. Therefore, this is not an viable option to
restrict the access of these class and functions. That is why I raised
the question whether there could be a 'private' keyword in
'namespace'.
Since these classes or functions are for C, I'd like to keep
them in the same namespace as C.

So put them in the same namespace as C. If they're not for
public consumation, I'll typically put them in a special
namespace, however, with a name indicating that they aren't for
public consumation.
Your approach would work, but it is not convenient. Suppose you
construct a namespace N_private for the functions and classes that are
not for public access. To access a function or class in N_private from
N, you have to use the qualifier N_private::.

Adding 'private' in 'namespace' would make refactoring easier. Before
you do code refactoring, you may not know what code should be private.
Therefore, you tend to put all of them in the same namespace N. After
refactoring, you may find some classes or functions can be made
'private'. When there is no such keyword 'private', you need to cut
all the classes and functions that you want to make private and paste
them to the namespace N_private. You then have to prepend all the
references of these functions and classes in N with 'N_private::'. But
if you have such keyword 'private', it would be much easier, you just
need to add 'private' to the declaration of the function and classes.

Your approach is also not safe. You can not prevent anybody from
accessing functions or classes in N_private delibrately.
Since these classes or functions are not for other classes
other than C, ideally, I would like to specify somehow in C++
that they are for C only. For now, what I can possibly think
of is to add 'private' in 'namespace' such that at least
classes outside N would not be able to call these functions
or classes.

Except that they would be able to. Given that namespaces are
open, making something private in a namespace would change
absolutely nothing.
Jul 24 '08 #9
On Jul 25, 7:23 am, Peng Yu <PengYu...@gmail.comwrote:
On Jul 24, 2:56 pm, James Kanze <james.ka...@gmail.comwrote:
On Jul 24, 7:31 pm, Peng Yu <PengYu...@gmail.comwrote:
On Jul 24, 2:48 am, James Kanze <james.ka...@gmail.comwrote:
On Jul 24, 2:58 am, Peng Yu <PengYu...@gmail.comwrote:
I'm wondering if there is something in namespace like the
'private' keyword in class? I want to define some class
or function that can only be used within that namespace.
Namespaces are open (anyone can add a member to a namespace,
anywhere), so there's no point in private.
I'd think a private in namespace might be useful.
You'll have to show what it might do that would be useful. It
certainly doesn't prevent anyone else from accessing it.
For example, I'm code a class C in namespace N. To code C, I
might need define some classes or functions, which are not
used by other classes other than C. I would not want to
defines these classes or functions in side C, because if I do
it this way, I would not be able to separate them from C into
two different files, which would make it harder to test and
understand the code.
I'm not following you. Typically, if you're writing library
code for concrete classes (no virtual functions), you'll have
each function in a separate file anyway.

What I meant was that a walkaround is to put these functions and
classes inside the class C and make them private.
This way make these functions and classes inaccessible by others, but
it also make it difficult to test them because you can not access them
from outside the class C. Therefore, this is not an viable option to
restrict the access of these class and functions. That is why I raised
the question whether there could be a 'private' keyword in
'namespace'.
Since these classes or functions are for C, I'd like to keep
them in the same namespace as C.
So put them in the same namespace as C. If they're not for
public consumation, I'll typically put them in a special
namespace, however, with a name indicating that they aren't for
public consumation.

Your approach would work, but it is not convenient. Suppose you
construct a namespace N_private for the functions and classes that are
not for public access. To access a function or class in N_private from
N, you have to use the qualifier N_private::.

Adding 'private' in 'namespace' would make refactoring easier. Before
you do code refactoring, you may not know what code should be private.
Therefore, you tend to put all of them in the same namespace N. After
refactoring, you may find some classes or functions can be made
'private'. When there is no such keyword 'private', you need to cut
all the classes and functions that you want to make private and paste
them to the namespace N_private. You then have to prepend all the
references of these functions and classes in N with 'N_private::'. But
if you have such keyword 'private', it would be much easier, you just
need to add 'private' to the declaration of the function and classes.

Your approach is also not safe. You can not prevent anybody from
accessing functions or classes in N_private delibrately.
Since these classes or functions are not for other classes
other than C, ideally, I would like to specify somehow in C++
that they are for C only. For now, what I can possibly think
of is to add 'private' in 'namespace' such that at least
classes outside N would not be able to call these functions
or classes.
Except that they would be able to. Given that namespaces are
open, making something private in a namespace would change
absolutely nothing.
The "using" keyword avoids the need for explicit N_private::
prefixes. Anyway, when the private functions are only needed by one
implementation file, they can be put in an anonymous namespace
therein. If they're needed in the header you're out of luck. If
they're needed by multiple implementation files, you do need to put
them in a shared file and should use namespace "N_private", but it
doesn't have to be included from the N.h header, so is less exposed to
clients. C++ is not designed to make such restrictions totally
binding: after all, client code can reinterpret cast and access memory
directly so it's a losing battle. All you can do is make it obvious
that the client is doing something unsupported. N_private or
N_implementation is a clear indication.

Tony
Jul 25 '08 #10
On Jul 25, 12:23 am, Peng Yu <PengYu...@gmail.comwrote:
On Jul 24, 2:56 pm, James Kanze <james.ka...@gmail.comwrote:
On Jul 24, 7:31 pm, Peng Yu <PengYu...@gmail.comwrote:
On Jul 24, 2:48 am, James Kanze <james.ka...@gmail.comwrote:
On Jul 24, 2:58 am, Peng Yu <PengYu...@gmail.comwrote:
I'm wondering if there is something in namespace like the
'private' keyword in class? I want to define some class
or function that can only be used within that namespace.
Namespaces are open (anyone can add a member to a namespace,
anywhere), so there's no point in private.
I'd think a private in namespace might be useful.
You'll have to show what it might do that would be useful.
It certainly doesn't prevent anyone else from accessing it.
For example, I'm code a class C in namespace N. To code C, I
might need define some classes or functions, which are not
used by other classes other than C. I would not want to
defines these classes or functions in side C, because if I do
it this way, I would not be able to separate them from C into
two different files, which would make it harder to test and
understand the code.
I'm not following you. Typically, if you're writing library
code for concrete classes (no virtual functions), you'll have
each function in a separate file anyway.
What I meant was that a walkaround is to put these functions
and classes inside the class C and make them private. This
way make these functions and classes inaccessible by others,
but it also make it difficult to test them because you can not
access them from outside the class C.
So which is it: should they be accessible from outside the class
C, or not?
Therefore, this is not an viable option to restrict the access
of these class and functions. That is why I raised the
question whether there could be a 'private' keyword in
'namespace'.
And as I have pointed out, even if you could declare something
"private" in a namespace, it wouldn't change anything.
Since these classes or functions are for C, I'd like to
keep them in the same namespace as C.
So put them in the same namespace as C. If they're not for
public consumation, I'll typically put them in a special
namespace, however, with a name indicating that they aren't
for public consumation.
Your approach would work, but it is not convenient. Suppose
you construct a namespace N_private for the functions and
classes that are not for public access. To access a function
or class in N_private from N, you have to use the qualifier
N_private::.
Or in your implementation code (but not in the header), "using
namespace N_private;".

From experience, I can assure you that it's not really a
problem. I have one case where the class in question is a
template, which means that its implementation code is also in a
header, so I can't use "using namespace". And even most of the
time when I can, I don't (or else just in one or two functions).
Adding 'private' in 'namespace' would make refactoring easier.
How? That's what you've failed to show. As far as I can see,
private in a namespace would be completely meaningless.
Before you do code refactoring, you may not know what code
should be private.
What kind of silliness is that? At any point in time, you do
know (or you should) what should be private, and what shouldn't
be. (Of course, that can change in time; you might find in time
that it is useful to make some private function public, even to
the extent of locking you into a specific implementation. While
not a question of private/public, per se, see the recent
evolution of std::string for a good example of this.)

At any rate, the *only* evolution along these lines I've ever
seen is from private to public, not the other way around.
Therefore, you tend to put all of them in
the same namespace N. After refactoring, you may find some
classes or functions can be made 'private'. When there is no
such keyword 'private', you need to cut all the classes and
functions that you want to make private and paste them to the
namespace N_private. You then have to prepend all the
references of these functions and classes in N with
'N_private::'. But if you have such keyword 'private', it
would be much easier, you just need to add 'private' to the
declaration of the function and classes.
You're avoiding the basic question: what does it mean to make
something private in a namespace. Nothing, as far as I can see.

And as I said, in a class, the evolution is in the opposite
sense: the public parts are your constraining contract, and you
start with them as small as possible, only opening things up
when experience shows that 1) the actual implementation works,
so you won't have to change it, and 2) some client code can
possibly take advantage of it.
Your approach is also not safe. You can not prevent anybody
from accessing functions or classes in N_private delibrately.
And? What does private in a namespace do that this doesn't?

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jul 25 '08 #11
On Jul 25, 7:35 am, tony_in_da...@yahoo.co.uk wrote:
On Jul 25, 7:23 am, Peng Yu <PengYu...@gmail.comwrote:
[...]
The "using" keyword avoids the need for explicit N_private::
prefixes.
In my experience, the need for them isn't very bothersome
anyway. But unless you're dealing with a template class (whose
implementation must be in a header), "using namespace" is a
solution if it does become a bother.
Anyway, when the private functions are only needed
by one implementation file, they can be put in an anonymous
namespace therein. If they're needed in the header you're out
of luck. If they're needed by multiple implementation files,
you do need to put them in a shared file and should use
namespace "N_private", but it doesn't have to be included from
the N.h header, so is less exposed to clients.
In my own code, it's fairly standard practice in such cases to
define a .lhh file for this. Which isn't exported by my
makefiles (nor considered by Doxygen), so client code never sees
it.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jul 25 '08 #12
Peng Yu <Pe*******@gmail.comwrote in news:2f3d83f6-91e4-487f-b8b2-
c3**********@w39g2000prb.googlegroups.com:
>
Adding 'private' in 'namespace' would make refactoring easier. Before
you do code refactoring, you may not know what code should be private.
Therefore, you tend to put all of them in the same namespace N. After
refactoring, you may find some classes or functions can be made
'private'. When there is no such keyword 'private', you need to cut
all the classes and functions that you want to make private and paste
them to the namespace N_private. You then have to prepend all the
references of these functions and classes in N with 'N_private::'. But
if you have such keyword 'private', it would be much easier, you just
need to add 'private' to the declaration of the function and classes.

Your approach is also not safe. You can not prevent anybody from
accessing functions or classes in N_private delibrately.
What it sounds like you really want is modules. Anything short of that
is going look like a hack. Adding private to namespace isn't
sufficient. A namespace is just a space for names, not a compilation
scope (like a class). One problem is that if I declared something as
private in a namespace, that would imply that I had access to that
anywhere I was in that namespace. This wouldn't be true in anything
close to the current c++ idea of namespace. For example:

t.cpp
=====

namespace blotto {

private:
int f() { return 5; }

public:

int fn() { return f(); } // ok
}
t1.cpp
======

namespace blotto {
public:

int f3() { return f(); } // Bzzzt no idea what f() is

}

namespaces just don't have the same requirements as classes in terms of
their definitions. You don't have to include a header to access them
currently. To make them work the way one would expect, you would have
to come close to turning them into classes, possibly without the ability
to instantiate them. In fact, you can actually use classes for this
today. All you really lose is the using namespace statement.

What it sounds like you really want (or maybe its just what I really
want and I am putting that on you) is the ability to expose a set of
functionality that is implemented by a possibly larger set of classes.
And you want to do it in a fairly straight forward way (rather than
nested classes etc). In other words, modules. They were working on
this for C++0x but time got away from them and they dropped it for now.
I sure hope it comes back at somepoint though.

joe
Jul 25 '08 #13
On Jul 25, 6:05 am, Joe Greer <jgr...@doubletake.comwrote:
Peng Yu <PengYu...@gmail.comwrote in news:2f3d83f6-91e4-487f-b8b2-
c3f5cd69d...@w39g2000prb.googlegroups.com:
Adding 'private' in 'namespace' would make refactoring easier. Before
you do code refactoring, you may not know what code should be private.
Therefore, you tend to put all of them in the same namespace N. After
refactoring, you may find some classes or functions can be made
'private'. When there is no such keyword 'private', you need to cut
all the classes and functions that you want to make private and paste
them to the namespace N_private. You then have to prepend all the
references of these functions and classes in N with 'N_private::'. But
if you have such keyword 'private', it would be much easier, you just
need to add 'private' to the declaration of the function and classes.
Your approach is also not safe. You can not prevent anybody from
accessing functions or classes in N_private delibrately.

What it sounds like you really want is modules. Anything short of that
is going look like a hack. Adding private to namespace isn't
sufficient. A namespace is just a space for names, not a compilation
scope (like a class). One problem is that if I declared something as
private in a namespace, that would imply that I had access to that
anywhere I was in that namespace. This wouldn't be true in anything
close to the current c++ idea of namespace. For example:

t.cpp
=====

namespace blotto {

private:
int f() { return 5; }

public:

int fn() { return f(); } // ok

}

t1.cpp
======

namespace blotto {
public:

int f3() { return f(); } // Bzzzt no idea what f() is

}

namespaces just don't have the same requirements as classes in terms of
their definitions. You don't have to include a header to access them
currently. To make them work the way one would expect, you would have
to come close to turning them into classes, possibly without the ability
to instantiate them. In fact, you can actually use classes for this
today. All you really lose is the using namespace statement.

What it sounds like you really want (or maybe its just what I really
want and I am putting that on you) is the ability to expose a set of
functionality that is implemented by a possibly larger set of classes.
And you want to do it in a fairly straight forward way (rather than
nested classes etc). In other words, modules. They were working on
this for C++0x but time got away from them and they dropped it for now.
I sure hope it comes back at somepoint though.

joe
Hi Joe,

You completely understand me. I didn't know the appropriate word
should be 'module' before you told me.

What I need is to refactor my code a more or less mechanical way,
which can be automated easily or with minimalist manual editing. As
there is not such support for 'module', to mimic 'module', I'll have
to use a nested class, which makes the refactoring process much
harder. Therefore, a practical resolution is just not to refactor in
this aspect, right?

Thanks,
Peng
Jul 27 '08 #14
On Jul 24, 10:35 pm, tony_in_da...@yahoo.co.uk wrote:
On Jul 25, 7:23 am, Peng Yu <PengYu...@gmail.comwrote:
On Jul 24, 2:56 pm, James Kanze <james.ka...@gmail.comwrote:
On Jul 24, 7:31 pm, Peng Yu <PengYu...@gmail.comwrote:
On Jul 24, 2:48 am, James Kanze <james.ka...@gmail.comwrote:
On Jul 24, 2:58 am, Peng Yu <PengYu...@gmail.comwrote:
I'm wondering if there is something in namespace like the
'private' keyword in class? I want to define some class
or function that can only be used within that namespace.
Namespaces are open (anyone can add a member to a namespace,
anywhere), so there's no point in private.
I'd think a private in namespace might be useful.
You'll have to show what it might do that would be useful. It
certainly doesn't prevent anyone else from accessing it.
For example, I'm code a class C in namespace N. To code C, I
might need define some classes or functions, which are not
used by other classes other than C. I would not want to
defines these classes or functions in side C, because if I do
it this way, I would not be able to separate them from C into
two different files, which would make it harder to test and
understand the code.
I'm not following you. Typically, if you're writing library
code for concrete classes (no virtual functions), you'll have
each function in a separate file anyway.
What I meant was that a walkaround is to put these functions and
classes inside the class C and make them private.
This way make these functions and classes inaccessible by others, but
it also make it difficult to test them because you can not access them
from outside the class C. Therefore, this is not an viable option to
restrict the access of these class and functions. That is why I raised
the question whether there could be a 'private' keyword in
'namespace'.
Since these classes or functions are for C, I'd like to keep
them in the same namespace as C.
So put them in the same namespace as C. If they're not for
public consumation, I'll typically put them in a special
namespace, however, with a name indicating that they aren't for
public consumation.
Your approach would work, but it is not convenient. Suppose you
construct a namespace N_private for the functions and classes that are
not for public access. To access a function or class in N_private from
N, you have to use the qualifier N_private::.
Adding 'private' in 'namespace' would make refactoring easier. Before
you do code refactoring, you may not know what code should be private.
Therefore, you tend to put all of them in the same namespace N. After
refactoring, you may find some classes or functions can be made
'private'. When there is no such keyword 'private', you need to cut
all the classes and functions that you want to make private and paste
them to the namespace N_private. You then have to prepend all the
references of these functions and classes in N with 'N_private::'. But
if you have such keyword 'private', it would be much easier, you just
need to add 'private' to the declaration of the function and classes.
Your approach is also not safe. You can not prevent anybody from
accessing functions or classes in N_private delibrately.
Since these classes or functions are not for other classes
other than C, ideally, I would like to specify somehow in C++
that they are for C only. For now, what I can possibly think
of is to add 'private' in 'namespace' such that at least
classes outside N would not be able to call these functions
or classes.
Except that they would be able to. Given that namespaces are
open, making something private in a namespace would change
absolutely nothing.

The "using" keyword avoids the need for explicit N_private::
prefixes. Anyway, when the private functions are only needed by one
implementation file, they can be put in an anonymous namespace
therein. If they're needed in the header you're out of luck. If
they're needed by multiple implementation files, you do need to put
them in a shared file and should use namespace "N_private", but it
doesn't have to be included from the N.h header, so is less exposed to
clients. C++ is not designed to make such restrictions totally
binding: after all, client code can reinterpret cast and access memory
directly so it's a losing battle. All you can do is make it obvious
that the client is doing something unsupported. N_private or
N_implementation is a clear indication.

Tony
Hi Tony,

I'm using templates. So everything has to be in header files for now.

Thanks,
Peng
Jul 27 '08 #15
On Jul 25, 5:21 am, James Kanze <james.ka...@gmail.comwrote:
On Jul 25, 12:23 am, Peng Yu <PengYu...@gmail.comwrote:
On Jul 24, 2:56 pm, James Kanze <james.ka...@gmail.comwrote:
On Jul 24, 7:31 pm, Peng Yu <PengYu...@gmail.comwrote:
On Jul 24, 2:48 am, James Kanze <james.ka...@gmail.comwrote:
On Jul 24, 2:58 am, Peng Yu <PengYu...@gmail.comwrote:
I'm wondering if there is something in namespace like the
'private' keyword in class? I want to define some class
or function that can only be used within that namespace.
Namespaces are open (anyone can add a member to a namespace,
anywhere), so there's no point in private.
I'd think a private in namespace might be useful.
You'll have to show what it might do that would be useful.
It certainly doesn't prevent anyone else from accessing it.
For example, I'm code a class C in namespace N. To code C, I
might need define some classes or functions, which are not
used by other classes other than C. I would not want to
defines these classes or functions in side C, because if I do
it this way, I would not be able to separate them from C into
two different files, which would make it harder to test and
understand the code.
I'm not following you. Typically, if you're writing library
code for concrete classes (no virtual functions), you'll have
each function in a separate file anyway.
What I meant was that a walkaround is to put these functions
and classes inside the class C and make them private. This
way make these functions and classes inaccessible by others,
but it also make it difficult to test them because you can not
access them from outside the class C.

So which is it: should they be accessible from outside the class
C, or not?
Therefore, this is not an viable option to restrict the access
of these class and functions. That is why I raised the
question whether there could be a 'private' keyword in
'namespace'.

And as I have pointed out, even if you could declare something
"private" in a namespace, it wouldn't change anything.
Since these classes or functions are for C, I'd like to
keep them in the same namespace as C.
So put them in the same namespace as C. If they're not for
public consumation, I'll typically put them in a special
namespace, however, with a name indicating that they aren't
for public consumation.
Your approach would work, but it is not convenient. Suppose
you construct a namespace N_private for the functions and
classes that are not for public access. To access a function
or class in N_private from N, you have to use the qualifier
N_private::.

Or in your implementation code (but not in the header), "using
namespace N_private;".

From experience, I can assure you that it's not really a
problem. I have one case where the class in question is a
template, which means that its implementation code is also in a
header, so I can't use "using namespace". And even most of the
time when I can, I don't (or else just in one or two functions).
Unfortunately, I'm using templates.
Adding 'private' in 'namespace' would make refactoring easier.

How? That's what you've failed to show. As far as I can see,
private in a namespace would be completely meaningless.
Before you do code refactoring, you may not know what code
should be private.

What kind of silliness is that? At any point in time, you do
know (or you should) what should be private, and what shouldn't
be. (Of course, that can change in time; you might find in time
that it is useful to make some private function public, even to
the extent of locking you into a specific implementation. While
not a question of private/public, per se, see the recent
evolution of std::string for a good example of this.)

At any rate, the *only* evolution along these lines I've ever
seen is from private to public, not the other way around.
In the post by Joe, he mentioned 'module', which is what I really
meant (I didn't know the appropriate word should be 'module', before
Joe told me).
Therefore, you tend to put all of them in
the same namespace N. After refactoring, you may find some
classes or functions can be made 'private'. When there is no
such keyword 'private', you need to cut all the classes and
functions that you want to make private and paste them to the
namespace N_private. You then have to prepend all the
references of these functions and classes in N with
'N_private::'. But if you have such keyword 'private', it
would be much easier, you just need to add 'private' to the
declaration of the function and classes.

You're avoiding the basic question: what does it mean to make
something private in a namespace. Nothing, as far as I can see.

And as I said, in a class, the evolution is in the opposite
sense: the public parts are your constraining contract, and you
start with them as small as possible, only opening things up
when experience shows that 1) the actual implementation works,
so you won't have to change it, and 2) some client code can
possibly take advantage of it.
Your approach is also not safe. You can not prevent anybody
from accessing functions or classes in N_private delibrately.

And? What does private in a namespace do that this doesn't?

--
James Kanze (GABI Software) email:james.ka...@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France,+33 (0)1 30 23 00 34
Jul 27 '08 #16
On Jul 27, 5:55 am, Peng Yu <PengYu...@gmail.comwrote:
On Jul 25, 5:21 am, James Kanze <james.ka...@gmail.comwrote:
At any rate, the *only* evolution along these lines I've ever
seen is from private to public, not the other way around.
In the post by Joe, he mentioned 'module', which is what I
really meant (I didn't know the appropriate word should be
'module', before Joe told me).
Modules are something very different; they actually do
something. I'm not up to date with the actual proposal in C++,
but presumably, once you've delivered a compiled module
interface file, the client code cannot add to the module. In
other words, modules are closed, like classes (except that they
are closed later in the development process---a class is closed
as soon as you write the closing brace).

Not that that affects my comment: you never evolve from public
to private (which would break the interface), but from private
to public.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jul 27 '08 #17
Peng Yu <Pe*******@gmail.comwrote in news:c8a3348d-4e73-4aa1-afdd-
f0**********@k36g2000pri.googlegroups.com:
Hi Joe,

You completely understand me. I didn't know the appropriate word
should be 'module' before you told me.

What I need is to refactor my code a more or less mechanical way,
which can be automated easily or with minimalist manual editing. As
there is not such support for 'module', to mimic 'module', I'll have
to use a nested class, which makes the refactoring process much
harder. Therefore, a practical resolution is just not to refactor in
this aspect, right?
Yes, this would be correct. There really isn't a mechanical way to get the
protections you desire. There are, of course, ways to specify your
interfaces such that getting access to classes outside the way they are
supposed to be used is obviously wrong. This wouldn't fit under the
category of 'mechanical with minimal manual intervention' though. I think
your best bet is to add some comments to classes that shouldn't be used by
end users. Most end users aren't really out to hurt themselves.

joe
Jul 28 '08 #18

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

Similar topics

4
by: Hans De Winter | last post by:
Why does my compiler complain with the message "`int*My::Test::_shape' is private" when I try to compile the following code? It seems it has something to do with the namespace since when I leave...
20
by: Patrick Guio | last post by:
Dear all, I have some problem with insertion operator together with namespace. I have a header file foo.h containing declaration of classes, typedefs and insertion operators for the typedefs in...
6
by: Robert Warnestam | last post by:
I've two class libraries, where one is referencing the other. The first library looks like this; (assembly name and root name space is set to Codab.Parser) namespace Codab.Parser { public class...
29
by: Natan | last post by:
When you create and aspx page, this is generated by default: using System; using System.Collections; using System.Collections.Specialized; using System.Configuration; using System.Text; using...
14
by: Jon Rea | last post by:
I am currently cleaning up an application which was origainlly hashed together with speed of coding in mind and therefore contains quite a few "hacky" shortcuts. As part of this "revamping"...
4
by: newbie120 | last post by:
Hi all maybe its just been a long day, but i have a question about call access modifiers in C#. Consider the following code. namespace Application { private class Class1 { int i;
3
by: sjt003 | last post by:
I have been developing web apps in Visual Studio 2003, but since the other developers in my office don't use Visual Studio, I may have to stop too unless there is an efficient way for them to...
2
by: tshad | last post by:
I am using vb.net VS 2003 and can't seem to see my namespace. I have 2 files (form1.vb and DBTypes.vb). My DBTypes.vb is: ************************************************ ' Create to new...
13
by: PragueExpat | last post by:
I (think) that I've come up with a pattern that I haven't seen in any publications so far and I would like some feedback. Basically, I was looking for a way to inherit private functions and I came...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.