Thread View: comp.sys.mac.programmer.codewarrior
6 messages
6 total messages
Started by Adin Hunter Baber
Wed, 24 Oct 2001 04:38
unnamed namespaces in CW5.3
Author: Adin Hunter Baber
Date: Wed, 24 Oct 2001 04:38
Date: Wed, 24 Oct 2001 04:38
32 lines
1841 bytes
1841 bytes
I was trying to put some classes in an unnamed namespace. The classes
provide some extra functionality in that header, and don't need to be
used outside of it. however, I am finding that I am able to create
instances of these classes inside my main function.
Is this an error in CW5.3? Is it fixed in CW7?
This compiles fine:
file: somestuff.h
namespace /*unnamed*/
{
class Stuff
{...};
}
#include "somestuff.h"
int main()
{
Stuff myStuff; // I don't think I should be able to do this
return 0;
};
--
Adin Hunter Baber
"Death is lighter than a feather, Duty heavier than a mountain"
"...nevertheless, they fought on even though their homes were piles of ashes,
even though they knew they could not win they still fought on,..."
Re: unnamed namespaces in CW5.3
Author: meeroh
Date: Wed, 24 Oct 2001 05:44
Date: Wed, 24 Oct 2001 05:44
45 lines
2154 bytes
2154 bytes
In article
<mjolnir_DELETE_-8A9595.23380323102001@east.usenetserver.com>,
Adin Hunter Baber <mjolnir_DELETE_@soltec.net> wrote:
>I was trying to put some classes in an unnamed namespace. The classes
>provide some extra functionality in that header, and don't need to be
>used outside of it. however, I am finding that I am able to create
>instances of these classes inside my main function.
>
>Is this an error in CW5.3? Is it fixed in CW7?
I think you misunderstand what the unnamed namespace is for. This is not
an error, as far as I know. The members of the unnamed namespace are
available throughout the compilation unit -- i.e. they will be available
in any cpp file which includes the header.
Perhaps if you show us how you are using class Stuff in the header file
we can suggest alternatives
hth
meeroh
>
>
>
>This compiles fine:
>
>file: somestuff.h
>namespace /*unnamed*/
> {
> class Stuff
> {...};
> }
>
>
>
>#include "somestuff.h"
>int main()
> {
> Stuff myStuff; // I don't think I should be able to do this
> return 0;
> };
--
Using random signatures is as simple as putting text files into a folder called "Random Signatures" in your Preferences folder.
Re: unnamed namespaces in CW5.3
Author: Adin Hunter Baber
Date: Wed, 24 Oct 2001 23:32
Date: Wed, 24 Oct 2001 23:32
37 lines
2752 bytes
2752 bytes
In article <macdev-854F03.01445224102001@senator-bedfellow.mit.edu>, meeroh <macdev@meeroh.org> wrote: > In article > <mjolnir_DELETE_-8A9595.23380323102001@east.usenetserver.com>, > Adin Hunter Baber <mjolnir_DELETE_@soltec.net> wrote: > > >I was trying to put some classes in an unnamed namespace. The classes > >provide some extra functionality in that header, and don't need to be > >used outside of it. however, I am finding that I am able to create > >instances of these classes inside my main function. > > > >Is this an error in CW5.3? Is it fixed in CW7? > > I think you misunderstand what the unnamed namespace is for. This is not > an error, as far as I know. The members of the unnamed namespace are > available throughout the compilation unit -- i.e. they will be available > in any cpp file which includes the header. > > Perhaps if you show us how you are using class Stuff in the header file > we can suggest alternatives You are correct, I did misunderstand the use of unnamed namespaces. I did not realize that they are supposed to be exposed in such a way. I guess I could just use a namespace BehindTheScenesStuffYouNoUse :) Please note my spam blocked address, it is included in the e-mail since I responded from my news reader. -- Adin Hunter Baber "Death is lighter than a feather, Duty heavier than a mountain" "...nevertheless, they fought on even though their homes were piles of ashes, even though they knew they could not win they still fought on,..."
Re: unnamed namespaces in CW5.3
Author: meeroh
Date: Wed, 24 Oct 2001 23:50
Date: Wed, 24 Oct 2001 23:50
12 lines
1463 bytes
1463 bytes
In article <mjolnir_DELETE_-3E19C3.18315324102001@east.usenetserver.com>, Adin Hunter Baber <mjolnir_DELETE_@soltec.net> wrote: >I guess I could just use a namespace BehindTheScenesStuffYouNoUse Or you could, like I said, post your code, so we can give you a more appropriate solution. meeroh -- The contents of text files in a folder called "Random Signatures" in your Preferences folder are used as random signatures.
Re: unnamed namespaces in CW5.3
Author: Adin Hunter Baber
Date: Fri, 26 Oct 2001 08:30
Date: Fri, 26 Oct 2001 08:30
70 lines
3033 bytes
3033 bytes
In article <macdev-B6C0DF.19501624102001@senator-bedfellow.mit.edu>,
meeroh <macdev@meeroh.org> wrote:
> In article
> <mjolnir_DELETE_-3E19C3.18315324102001@east.usenetserver.com>,
> Adin Hunter Baber <mjolnir_DELETE_@soltec.net> wrote:
>
> >I guess I could just use a namespace BehindTheScenesStuffYouNoUse
>
> Or you could, like I said, post your code, so we can give you a more
> appropriate solution.
>
> meeroh
namespace /*unnamed*/{
struct SmartBase
{
mutable int counter;
SmartBase(const int i)
:counter(i)
{}
virtual ~SmartBase() {}
};
}// end namespace unnamed
namespace Adin{
template<class T>
struct Smart : public SmartBase
{
T data;
~Smart() {}
Smart()
: SmartBase(0) {}
Smart(const T &rt)
: SmartBase(0), data(rt) {}
Smart( T &rt)
: SmartBase(0), data(rt) {}
// other constructors
};
template<class T>
class SmartPointer
{
public:
SmartPointer(Smart<T> *rt);
// lots of other stuff
};
}// end namespace Adin
According to my design, there is not any real reason for some to have a
SmartBase in existance outside of this file's scope.
Basically, the code would be used this way:
SmartPointer<someClass> myptr = new Smart<someClass>(parameters);
If you are wondering, it is a smart pointer class that uses intrusive
reference counting and handles polymorphism.
--
Adin Hunter Baber
"Death is lighter than a feather, Duty heavier than a mountain"
"...nevertheless, they fought on even though their homes were piles of ashes,
even though they knew they could not win they still fought on,..."
Re: unnamed namespaces in CW5.3
Author: meeroh
Date: Sat, 27 Oct 2001 03:29
Date: Sat, 27 Oct 2001 03:29
76 lines
3284 bytes
3284 bytes
In article
<mjolnir_DELETE_-A4247B.03301526102001@east.usenetserver.com>,
Adin Hunter Baber <mjolnir_DELETE_@soltec.net> wrote:
>> Or you could, like I said, post your code, so we can give you a more
>> appropriate solution.
>
>namespace /*unnamed*/{
>
>struct SmartBase
> {
> mutable int counter;
>
> SmartBase(const int i)
> :counter(i)
> {}
> virtual ~SmartBase() {}
> };
>}// end namespace unnamed
>
>namespace Adin{
>
>template<class T>
>struct Smart : public SmartBase
> {
> T data;
>
> ~Smart() {}
>
> Smart()
> : SmartBase(0) {}
> Smart(const T &rt)
> : SmartBase(0), data(rt) {}
> Smart( T &rt)
> : SmartBase(0), data(rt) {}
>
> // other constructors
> };
>
>template<class T>
>class SmartPointer
> {
> public:
> SmartPointer(Smart<T> *rt);
> // lots of other stuff
> };
>}// end namespace Adin
>
>According to my design, there is not any real reason for some to have a
>SmartBase in existance outside of this file's scope.
You should make the SmartBase constructors protected and therefore
prevent people from instantiating SmartBase unless they derive from
SmartBase, but there is no way to prevent people from seeing the whole
declaration, since you need it in order to inherit from it.
Since you are putting things inside your own namespace, though, you may
want to consider putting SmartBase inside that namespace, or even better
inside a subnamespace. Somethine like Adin::Private::SmartBase. That
should be a strong enough hint that people should stay away from it, and
the protected ctors will prevent them from directly instantiating the
class.
hth
meeroh
>
>Basically, the code would be used this way:
>
>SmartPointer<someClass> myptr = new Smart<someClass>(parameters);
>
>If you are wondering, it is a smart pointer class that uses intrusive
>reference counting and handles polymorphism.
--
The contents of text files in a folder called "Random Signatures" in your Preferences folder are used as random signatures.
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads