class vs namespace?

D

dean

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
 
J

Jakob Bieling

dean said:
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
 
R

Rolf Magnus

Jakob said:
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.
 
D

dean

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?
 
F

Fred Zwarts

dean said:
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.
 
J

Jakob Bieling

dean said:
Rolf Magnus wrote:
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
 
D

dean

I have no idea what to top-post means. And anyway, what are you adding
to this discussion?
 
N

Noah Roberts

dean said:
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.
 
D

dean

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
 
K

Kai-Uwe Bux

dean wrote [top posting corrected]
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
 
D

dean

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 said:
dean wrote [top posting corrected]
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
 
D

Default User

dean said:
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
 
N

Noah Roberts

dean said:
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.
 
D

dean

Default said:
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?


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
 
K

Kai-Uwe Bux

dean said:
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
 
D

Default User

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
 
D

dean

Default said:
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.
 
D

dean

Kai-Uwe Bux said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top