Separating exception classes from interface

Hi, I'm creating exception classes for one of my class (say, Foo), and would like to separate (not only the implementation but also) the declaration from the declaration of Foo.

Before separating it's like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Foo
{
  // ...
  
  // exception classes
  class xBar
  {
    // ...
    virtual void SomeFunc();
  };
  class xBarOne : public xBar
  {
    // ...
  };
  class xBarTwo : public xBar
  {
    // ...
  };
  
  // ...
};


It's quite a bunch of hierarchies over there, I don't like them appearing to "flood" the interface :\


Is it possible to separate? (actually I got a few more questions)

If yes,
Is it a good idea to do so? Or to put it this way: should the declarations of exception classes be part of the "interface", i.e. the interface for the users?

If no,
can I separate the function definition in another *.cpp like
1
2
3
4
void Foo::xBar::SomeFunc
{
  // ...
}

?

Thanks in advance :)
Last edited on
It is possible to separate.

Making them members of Foo basically implies that these exceptions are specific to Foo and will never
be thrown by anything other than a Foo object.

If you are concerned about polluting the outer namespace (ie, global namespace), then I'd suggest creating
a namespace into which you should place Foo and its exception classes.

To answer your last question, yes, you can, provided that the exception classes are declared public (which
I'm assuming they are).
Thanks!

Actually I'm not very sure about how they should be declared in Foo...
like this?
1
2
3
4
5
class xRange;
class xSize;
   class xSizeBig : public xSize;
   class xSizeSmall : public xSize;
   // ... 


The compiler is complaining about line 3, "invalid use of undefined type", as xSize is only forward declared at that time.
Ah, sorry, I think I misunderstood you and led you down the wrong path.

You'll have to put the entire declaration of the exception classes in Foo's body.
You can put the implementation of those classes in a separate file provided they
are declared public inside Foo.

Sorry for the confusion.
I see, thanks.
Probably my wordings are somehow confusing :P
afaik, you can do this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Foo
{
public:
  class xBarOne;
  class xBarTwo;
};

//--------------------
// elsewhere

class Foo::xBarOne : public xBar
{
 // ...
};

//etc 


At least this works in GCC
Topic archived. No new replies allowed.