I need to implement the following interface
struct mutex;
struct interface
{
//...
mutex& getMutex();
};
Intuition would I could use using mutex = ParticularMutex
in my implementation, but gcc tells me otherwise:
error: conflicting declaration ‘using mutex = ’
error: ‘class mutex’ has a previous declaration as ‘class mutex’
I am not defining anything twice, only declaring twice, as usual when forward declaring, so
interface
?interface
have been defined? with template <typename mutex>
?One can declare the same variable using typedef in 2 or more different files and no error will be thrown till both refer to the same type. In C++, 'typedef' allows the programmer to declare multiple types at once, unlike the 'using' statement.
In Objective-C, classes and protocols can be forward-declared like this: @class MyClass; @protocol MyProtocol; In Objective-C, classes and protocols can be forward-declared if you only need to use them as part of an object pointer type, e.g. MyClass * or id<MyProtocol>.
A forward declaration allows us to tell the compiler about the existence of an identifier before actually defining the identifier. In the case of functions, this allows us to tell the compiler about the existence of a function before we define the function's body.
But you can't forward declare a typedef.
It does not work because the forward declaration struct mutex;
tells the compiler that mutex
is a new type. With using
you are then creating a type alias, which means it's not a new type (as promised to the compiler), but an alias to an existing type.
No.
Yes.
What you could do is:
struct mutex : ParticularMutex {
using ParticularMutex::ParticularMutex; // inherit constructors
};
Which does define a type derived from ParticularMutex
which is hopefully compatible enough. Of course, this is a new type which might lead to other problems.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With