Enumeration scope in namespace

B

Barzo

Hi,

I'm a little bit confusing about the enumerations scope into
namespaces.
I have this situation where MyLib.h is the interface header supplied
to the customers.

1. Is this a valid approch?
2. The MyLib.cpp build fails...I have to specify the
IMyBaseClass::MyEnum for each enumeraton?


MyLib.h
--------
namespace MyNamespace
{
class IMyBaseClass
{
enum MyEnum
{
VAL_A,
VAL_B
}

virtual MyEnum f() = 0;
}

class IMyClass : public IMyBaseClass
{
virtual void g( MyEnum value) = 0;
}

}


MyLibImpl.h
------------
#include "MyLib.h"

namespace MyNamespace
{
class MyClass : public IMyClass
{
virtual MyEnum f();
void g( MyEnum value );
}
}


MyLibImpl.cpp
-------------
#include "MyLib.h"
#include "MyLibImpl.h"

using namespace MyNamespace;
using MyNamespace::IMyBaseClass;

MyEnum MyClass::f()
{
...
};

void MyClass::g( MyEnum value )
{
...
};

Thanks.
Daniele.
 
A

Alf P. Steinbach

* Barzo alias Daniele:
I'm a little bit confusing about the enumerations scope into
namespaces.
I have this situation where MyLib.h is the interface header supplied
to the customers.

1. Is this a valid approch?

Probably, but see below.

2. The MyLib.cpp build fails...I have to specify the
IMyBaseClass::MyEnum for each enumeraton?

See the FAQ's advice on how to post a question about Code That Does Not Work.

Generally the key idea is to be as specific as possible.

MyLib.h
--------
namespace MyNamespace
{
class IMyBaseClass
{
enum MyEnum
{
VAL_A,
VAL_B
}

virtual MyEnum f() = 0;
}

Missing semicolon. That means that this is code you have typed in instead of
copied and pasted. And so it's *not* the code you're having problems with.

And that means that we can only guess about whatever the problem is.

class IMyClass : public IMyBaseClass
{
virtual void g( MyEnum value) = 0;
}

}


MyLibImpl.h
------------
#include "MyLib.h"

namespace MyNamespace
{
class MyClass : public IMyClass
{
virtual MyEnum f();
void g( MyEnum value );
}
}



MyLibImpl.cpp
-------------
#include "MyLib.h"
#include "MyLibImpl.h"

using namespace MyNamespace;
OK.


using MyNamespace::IMyBaseClass;

This latter using declaration is superflous; you have already brought
IMyBaseClass into the global namespace.

MyEnum MyClass::f()

Should be e.g.

MyClass::MyEnum MyClass::f()

I don't like the C++ rules here, but anyway, you have to imagine a compiler with
a very very narrow field of view and attention, scanning from left to right.
While scanning the result type it doesn't yet know that this is in the context
of a MyClass member function, and it doesn't look ahead to make sense of the
type specification. So it needs to be force-fed that information.

When the compiler gets to the formal arguments, however, it has understood that
yes, we're dealing with MyClass.

{
...
};

void MyClass::g( MyEnum value )
{
...
};

Thanks.
Daniele.

You're welcome.


Cheers & hth.,

- Alf
 
B

Barzo

First, thanks for the support Alf!
See the FAQ's advice on how to post a question about Code That Does Not Work.
Generally the key idea is to be as specific as possible.

I'm sorry, in the future I'll pay more attention.

Missing semicolon. That means that this is code you have typed in instead of
copied and pasted. And so it's *not* the code you're having problems with..

And that means that we can only guess about whatever the problem is.

Yes, I wrote this code only to expain my problem.
Another thing I should specify, sorry again.

Should be e.g.

   MyClass::MyEnum MyClass::f()

I don't like the C++ rules here, but anyway, you have to imagine a compiler with
a very very narrow field of view and attention, scanning from left to right.
While scanning the result type it doesn't yet know that this is in the context
of a MyClass member function, and it doesn't look ahead to make sense of the
type specification. So it needs to be force-fed that information.

When the compiler gets to the formal arguments, however, it has understood that
yes, we're dealing with MyClass.

Oh, ok...
I have thought that importing the namespace was sufficient for the
compiler.
I have modified my code and now seems to work!

You're welcome.

Cheers & hth.,

- Alf

Thanks again!
Daniele.
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top