By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,035 Members | 1,543 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,035 IT Pros & Developers. It's quick & easy.

class vs namespace?

P: n/a
Hi all,

We have a few existing classes that we want to put into either another
class or a namespace, so that we can avoid naming conflicts. As far as
I can see the syntax is identical from a usage perspective.

Does anyone have an opinion on this either way?

Thanks

Dean

Sep 15 '06 #1
Share this Question
Share on Google+
22 Replies


P: n/a
dean <de*********@yahoo.comwrote:
Hi all,

We have a few existing classes that we want to put into either another
class or a namespace, so that we can avoid naming conflicts. As far as
I can see the syntax is identical from a usage perspective.

Does anyone have an opinion on this either way?
For your purpose, namespaces are the way to go. You can reopen a
namespace and add stuff across translation units. You cannot do this
with classes. In addition, using a class implies that you can create an
instance of that class, which, as I understood it, is not the case.

hth
--
jb

(reply address in rot13, unscramble first)
Sep 15 '06 #2

P: n/a
Jakob Bieling wrote:
dean <de*********@yahoo.comwrote:
>Hi all,

We have a few existing classes that we want to put into either another
class or a namespace, so that we can avoid naming conflicts. As far as
I can see the syntax is identical from a usage perspective.

Does anyone have an opinion on this either way?

For your purpose, namespaces are the way to go. You can reopen a
namespace and add stuff across translation units. You cannot do this
with classes. In addition, using a class implies that you can create an
instance of that class, which, as I understood it, is not the case.
Also, you can use using-declarations with namespaces, and AFAIK that's not
possible with classes, at least not if you don't derive from them.

Sep 15 '06 #3

P: n/a
So what to do about member variables of the class, if we use a
namespace instead? I mean, how can one hide a private variable and just
allow accessor (get and set) functions? Put the member variable outside
of the namespace but in the same unit as the namespace?
Rolf Magnus wrote:
Jakob Bieling wrote:
dean <de*********@yahoo.comwrote:
Hi all,

We have a few existing classes that we want to put into either another
class or a namespace, so that we can avoid naming conflicts. As far as
I can see the syntax is identical from a usage perspective.

Does anyone have an opinion on this either way?
For your purpose, namespaces are the way to go. You can reopen a
namespace and add stuff across translation units. You cannot do this
with classes. In addition, using a class implies that you can create an
instance of that class, which, as I understood it, is not the case.

Also, you can use using-declarations with namespaces, and AFAIK that's not
possible with classes, at least not if you don't derive from them.
Sep 15 '06 #4

P: n/a
"dean" <de*********@yahoo.comwrote in message news:11*********************@b28g2000cwb.googlegro ups.com...
So what to do about member variables of the class, if we use a
namespace instead? I mean, how can one hide a private variable and just
allow accessor (get and set) functions? Put the member variable outside
of the namespace but in the same unit as the namespace?
I would think the other way around.
Put the private variable in the same namespace but in another unit
(the unit in which you write the body of the get and set functions).
The advantage above a class implementation is that the private variables
are not even declared in the header file.
The header file discloses nothing about the implementation,
it has only the interface

Fred.Zwarts.
Rolf Magnus wrote:
>Jakob Bieling wrote:
dean <de*********@yahoo.comwrote:
Hi all,

We have a few existing classes that we want to put into either another
class or a namespace, so that we can avoid naming conflicts. As far as
I can see the syntax is identical from a usage perspective.

Does anyone have an opinion on this either way?

For your purpose, namespaces are the way to go. You can reopen a
namespace and add stuff across translation units. You cannot do this
with classes. In addition, using a class implies that you can create an
instance of that class, which, as I understood it, is not the case.

Also, you can use using-declarations with namespaces, and AFAIK that's not
possible with classes, at least not if you don't derive from them.
Sep 15 '06 #5

P: n/a
dean <de*********@yahoo.comwrote:
Rolf Magnus wrote:
>Jakob Bieling wrote:
>>dean <de*********@yahoo.comwrote:
>>>We have a few existing classes that we want to put into either
another class or a namespace, so that we can avoid naming
conflicts. As far as I can see the syntax is identical from a
usage perspective.

Does anyone have an opinion on this either way?
>> For your purpose, namespaces are the way to go. You can reopen a
namespace and add stuff across translation units. You cannot do this
with classes. In addition, using a class implies that you can
create an instance of that class, which, as I understood it, is not
the case.
>Also, you can use using-declarations with namespaces, and AFAIK
that's not possible with classes, at least not if you don't derive
from them.
So what to do about member variables of the class, if we use a
namespace instead? I mean, how can one hide a private variable and
just allow accessor (get and set) functions? Put the member variable
outside of the namespace but in the same unit as the namespace?
I think I am not following. You said you have several classes and
want to put them into a namespace (or class, but let's just stick to the
namespace now) to avoid naming conflicts:

class A { int a; };
class B { int b; };

becomes

namespace dean
{
class A { int a; };
class B { int b; };
};

As you can see, your classes do not change. Their members stay right
where they are. You only introduce a new namespace and put the stuff in
there.

Apologies, if I am misunderstanding you.

hth
--
jb

(reply address in rot13, unscramble first)
Sep 15 '06 #6

P: n/a
dean wrote:
So what to do about member variables of the class


Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or the group FAQ list:
<http://www.parashift.com/c++-faq-lite/how-to-post.html>


Brian
Sep 15 '06 #7

P: n/a
I have no idea what to top-post means. And anyway, what are you adding
to this discussion?
Default User wrote:
dean wrote:
So what to do about member variables of the class

Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or the group FAQ list:
<http://www.parashift.com/c++-faq-lite/how-to-post.html>


Brian
Sep 15 '06 #8

P: n/a

dean wrote:
I have no idea what to top-post means. And anyway, what are you adding
to this discussion?
You need to read the webpage to which you where directed.

Sep 15 '06 #9

P: n/a
Rolf - for better or worse, the company agreed not to use "using
namespace", but rather add the scope prefix onto each member, e.g.
CMyClass::MyFunc or CMyNamespace::MyFunc.

Dean

Rolf Magnus wrote:
Jakob Bieling wrote:
dean <de*********@yahoo.comwrote:
Hi all,

We have a few existing classes that we want to put into either another
class or a namespace, so that we can avoid naming conflicts. As far as
I can see the syntax is identical from a usage perspective.

Does anyone have an opinion on this either way?
For your purpose, namespaces are the way to go. You can reopen a
namespace and add stuff across translation units. You cannot do this
with classes. In addition, using a class implies that you can create an
instance of that class, which, as I understood it, is not the case.

Also, you can use using-declarations with namespaces, and AFAIK that's not
possible with classes, at least not if you don't derive from them.
Sep 15 '06 #10

P: n/a
Fred - that is a very good idea. Nice implementation hiding.

Thanks

Dean

Fred Zwarts wrote:
"dean" <de*********@yahoo.comwrote in message news:11*********************@b28g2000cwb.googlegro ups.com...
So what to do about member variables of the class, if we use a
namespace instead? I mean, how can one hide a private variable and just
allow accessor (get and set) functions? Put the member variable outside
of the namespace but in the same unit as the namespace?

I would think the other way around.
Put the private variable in the same namespace but in another unit
(the unit in which you write the body of the get and set functions).
The advantage above a class implementation is that the private variables
are not even declared in the header file.
The header file discloses nothing about the implementation,
it has only the interface

Fred.Zwarts.
Rolf Magnus wrote:
Jakob Bieling wrote:

dean <de*********@yahoo.comwrote:
Hi all,

We have a few existing classes that we want to put into either another
class or a namespace, so that we can avoid naming conflicts. As far as
I can see the syntax is identical from a usage perspective.

Does anyone have an opinion on this either way?

For your purpose, namespaces are the way to go. You can reopen a
namespace and add stuff across translation units. You cannot do this
with classes. In addition, using a class implies that you can create an
instance of that class, which, as I understood it, is not the case.

Also, you can use using-declarations with namespaces, and AFAIK that's not
possible with classes, at least not if you don't derive from them.
Sep 15 '06 #11

P: n/a
dean wrote [top posting corrected]
Default User wrote:
>dean wrote:
So what to do about member variables of the class

Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or the group FAQ list:
<http://www.parashift.com/c++-faq-lite/how-to-post.html>
I have no idea what to top-post means.
Top posting is to put your answer above the text that you are replying to.
It is frowned upon in this news group -- that's a cultural thing.

And anyway, what are you adding to this discussion?
E.g., that you might benefit from reading the FAQ (generally a good idea and
part of netiquette).
Best

Kai-Uwe Bux
Sep 15 '06 #12

P: n/a
Gosh! Clearly this is a very important issue for some people so I
apologise to them. I was replying to two people at the same time, not
just one - both of them had good responses. Should I have just quoted
one of them?

Kai-Uwe Bux wrote:
dean wrote [top posting corrected]
Default User wrote:
dean wrote:

So what to do about member variables of the class

Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or the group FAQ list:
<http://www.parashift.com/c++-faq-lite/how-to-post.html>
I have no idea what to top-post means.

Top posting is to put your answer above the text that you are replying to.
It is frowned upon in this news group -- that's a cultural thing.

And anyway, what are you adding to this discussion?

E.g., that you might benefit from reading the FAQ (generally a good idea and
part of netiquette).
Best

Kai-Uwe Bux
Sep 15 '06 #13

P: n/a
dean wrote:
I have no idea what to top-post means.
1. I explained it. Here it is again: Your replies belong following or
interspersed with properly trimmed quotes.

2. The FAQ goes over it as well. Did you read it?
And anyway, what are you adding to this discussion?
I'm helping you to get help by posting correctly. It so happens that
I've automated things a bit, so it's easy for me to quickly post the
information. I do that so others don't.


Brian
Sep 15 '06 #14

P: n/a

dean wrote:
Gosh! Clearly this is a very important issue for some people so I
apologise to them.
Usually when you want to apologise to someone you don't do the thing
you are apologising for at the same time; makes you appear inconcere.
If you aren't sorry then don't apologise (many here will end up
killfiling you) and if you are sorry then you won't do it again and
you'll try to learn how to communicate in the group effectively.
You've been given the resources to learn and now know that yes, this is
an important issue. Now you'll either stop top-posting or you won't.
You're decision will have an impact on how people percieve you in the
group and the quality and quantity of answers you recieve.

Sep 15 '06 #15

P: n/a

Default User wrote:
1. I explained it. Here it is again: Your replies belong following or
interspersed with properly trimmed quotes.

2. The FAQ goes over it as well. Did you read it?
And anyway, what are you adding to this discussion?

I'm helping you to get help by posting correctly. It so happens that
I've automated things a bit, so it's easy for me to quickly post the
information. I do that so others don't.

Brian
I already told you I replied to two people, not one. I assumed some
degree of intelligence to follow the discussion flow. Anyway, I got the
best solution from someone who was more interested in the topic than in
correcting other's thread etiquette.

Dean

Sep 15 '06 #16

P: n/a
dean wrote:
Gosh! Clearly this is a very important issue for some people so I
apologise to them. I was replying to two people at the same time, not
just one - both of them had good responses. Should I have just quoted
one of them?
[snip]
Reply to each of them separately. Quote enough material so that your answer
has enough context to be understood. Don't top-post. Just apply some common
sense taking into account the technicalities of news groups as a medium:
different posting propagate with different speeds through the news server
network. Thus, you cannot assume that the readers of your post have its
history nicely aligned above it. Instead you have to provide the context
for your answer. To make clear what your reply refers to, put your reply
below the quote. This way, references are always clear even if you address
several points of the posting that you are answering.
Best

Kai-Uwe Bux
Sep 15 '06 #17

P: n/a
dean wrote:
>
Default User wrote:
1. I explained it. Here it is again: Your replies belong following
or interspersed with properly trimmed quotes.

2. The FAQ goes over it as well. Did you read it?
And anyway, what are you adding to this discussion?
I'm helping you to get help by posting correctly. It so happens that
I've automated things a bit, so it's easy for me to quickly post the
information. I do that so others don't.
I already told you I replied to two people, not one. I assumed some
degree of intelligence to follow the discussion flow. Anyway, I got
the best solution from someone who was more interested in the topic
than in correcting other's thread etiquette.

Say what? That's non-responsive to what I said.

At any rate, trimmed and bottom-posted, so progress I suppose.

Brian

Sep 15 '06 #18

P: n/a

Default User wrote:
dean wrote:

Default User wrote:
1. I explained it. Here it is again: Your replies belong following
or interspersed with properly trimmed quotes.
>
2. The FAQ goes over it as well. Did you read it?
>
And anyway, what are you adding to this discussion?
>
I'm helping you to get help by posting correctly. It so happens that
I've automated things a bit, so it's easy for me to quickly post the
information. I do that so others don't.
I already told you I replied to two people, not one. I assumed some
degree of intelligence to follow the discussion flow. Anyway, I got
the best solution from someone who was more interested in the topic
than in correcting other's thread etiquette.


Say what? That's non-responsive to what I said.

At any rate, trimmed and bottom-posted, so progress I suppose.

Brian
I'd be interested in your opinion on the subject, if you have one
Brian. If not, then please just don't read the thread if it annoys you.

Sep 16 '06 #19

P: n/a

Kai-Uwe Bux wrote:
Reply to each of them separately. Quote enough material so that your answer
has enough context to be understood. Don't top-post. Just apply some common
sense taking into account the technicalities of news groups as a medium:
different posting propagate with different speeds through the news server
network. Thus, you cannot assume that the readers of your post have its
history nicely aligned above it. Instead you have to provide the context
for your answer. To make clear what your reply refers to, put your reply
below the quote. This way, references are always clear even if you address
several points of the posting that you are answering.
Ok. Thanks for being polite.

Can I ask how you're reading this? For me I just go to "google groups"
(http://groups.google.com/group/comp.lang.c++?hl=en) and it gives a
fairly simple tree view on the left, I don't generally have any trouble
seeing how the flow goes, because its neat and tidy and all the old
post text is hidden, is that not the case for you? It seems like it
must be worse for some people here considering the responses.

Dean

Sep 16 '06 #20

P: n/a
dean wrote:
[snip]
Can I ask how you're reading this? For me I just go to "google groups"
(http://groups.google.com/group/comp.lang.c++?hl=en) and it gives a
fairly simple tree view on the left, I don't generally have any trouble
seeing how the flow goes, because its neat and tidy and all the old
post text is hidden, is that not the case for you?
Nope, it is not the case for me: I use a news reader. That program shows me
a threaded list of postings available from the news server, i.e., posts
that have been deleted from the server disappear from my list as well
(Google keeps the complete archive since they got it from DejaNews,
however, most news servers don't). When I select a post, the program
displays that posting only and it does not hide any quoted material (which
is a good thing since the quotes are the only context that is readily
available).

It seems like it must be worse for some people here considering the
responses.
It's actually not worse or better; it's different. It happens to be the way
news groups worked for a few decades (before Google invaded this territory)
and posting styles have adjusted to the media. Personally, I find the
Google interface wanting since it hides the quotations by default. However,
I admit that this is probably a matter of what I am used to.
Best

Kai-Uwe Bux
Sep 16 '06 #21

P: n/a

Kai-Uwe Bux wrote:
Nope, it is not the case for me: I use a news reader. That program shows me
a threaded list of postings available from the news server, i.e., posts
that have been deleted from the server disappear from my list as well
(Google keeps the complete archive since they got it from DejaNews,
however, most news servers don't). When I select a post, the program
displays that posting only and it does not hide any quoted material (which
is a good thing since the quotes are the only context that is readily
available).
[snip]
>
Best

Kai-Uwe Bux
I tried a couple of news-readers once, and I didn't like them at all.
The good thing about google is that you can search all news groups and
get an answer fast, which is really important if you are working.

Thanks for the hints,

Dean

Sep 16 '06 #22

P: n/a

"Default User" <de***********@yahoo.comwrote in message news:4n************@individual.net...
dean wrote:
>So what to do about member variables of the class


Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or the group FAQ list:
<http://www.parashift.com/c++-faq-lite/how-to-post.html>
If you are so concerned that others use Netikette, please,
adhere to Netikette yourself. This newsgroup is about C++,
not about Netikette. If you want to talk about Netikette,
please, use other newsgroups, or use private Email.

Fred.Zwarts.
Sep 18 '06 #23

This discussion thread is closed

Replies have been disabled for this discussion.