cyclic dependency in throw declaration
I am trying to solve a weird problem because of the following design
that I want to implement.
I have an 'exception' class that no other function must be able to
throw except a select group of functions. I want the compiler to
prohibit people who use my code the capability of throwing instances
of my exception class. However they will still be able catch objects
of the exception class. Why do I want to do this ? This brings clarity
to the working of the code - the source of all instances of my
exception class is only one - my code. I could have easily acheived
this in Java by making the constructor of my exception class package-
private ( which I acheive by not specifying any access modifier for
the constructor ) . I would then put the exception class inside the
package where my code will be making new instances of the exception
class. But C++ lacks this kind of access modifier. Now how should I
design my exception class to achieve this in C++ ?
I resorted to making the constructor of my exception class as private
and making all the functions of my code which will be allowed to throw
instances of this exception class, as 'friend's of the class. A
representative piece of code of this design is given here:
struct m
{
private:
m ( )
{ }
friend void myFunc ( ) throw ( m ) ;
} ;
void myFunc ( ) throw ( m )
{
}
int main ( )
{
return 0 ;
}
The problem is that the above code doesn't compile in g++ ( GNU
version of c++ compiler ). It throws the following errors:
structure.cpp:8: error: invalid use of undefined type =91struct m'
structure.cpp:3: error: forward declaration of =91struct m'
Even more surprising fact is that the above code won't compile even if
I change the line 'friend void myFunc ( ) throw ( m )' to :
friend void myFunc ( ) throw ( m * )
and also make the corresponding change in the function definition of
'myFunc' !
However, it does compile only when I change this line to :
friend void myFunc ( ) throw ( m * * )
Here's the complete code:
struct m
{
private:
m ( )
{ }
friend void myFunc ( ) throw ( m * * ) ;
} ;
void myFunc ( ) throw ( m * * )
{
}
int main ( )
{
return 0 ;
}
Why does g++ allow me throw a pointer to a pointer of type 'struct m'
but not a pointer of type 'struct m' ?