O
Ole Nielsby
I want to create (with new) and delete a forward declared class.
(I'll call them Zorgs here - the real-life Zorks are platform-dependent
objects (mutexes, timestamps etc.) used by a cross-platform scripting
engine. When the scripting engine is embedded in an application, a
platform-specific support library is linked in.)
My first attempt goes here:
---code begin (library)---
class Zorg_implementation; //to be defined by another library
class Zorg {
public:
Zorg(): zi(new Zorg_implementation) {} //compile error
~Zorg() {delete zi;}
private:
Zorg_implementation *zi;
};
---code end---
which won't compile
(VC8 says: no appropriate default constructor available)
Then I stuffed it in a dummy template:
---code begin---
class Zorg_implementation; //to be defined by another library
template <typename Z> class Zorg_t {
public:
Zorg_t(): zi(new Z) {}
~Zorg_t() {delete zi;}
private:
Z *zi;
};
typedef Zorg_template<Zorg_implementation> Zorg;
---code end---
This compiles and runs fine. Zorg_wrapper instances can be created
by one library, while another library defines the Zorg_implementation class.
This strikes me as odd - I would expect the templated version to have the
same effect as the simple version.
Can anybody explain what is going on here? Is this standard behaviour
or a VC8 quirk? Can I rely on it - or should I define a factory class?
Ole Nielsby
(my reply address has a question that must be answered)
(I'll call them Zorgs here - the real-life Zorks are platform-dependent
objects (mutexes, timestamps etc.) used by a cross-platform scripting
engine. When the scripting engine is embedded in an application, a
platform-specific support library is linked in.)
My first attempt goes here:
---code begin (library)---
class Zorg_implementation; //to be defined by another library
class Zorg {
public:
Zorg(): zi(new Zorg_implementation) {} //compile error
~Zorg() {delete zi;}
private:
Zorg_implementation *zi;
};
---code end---
which won't compile
(VC8 says: no appropriate default constructor available)
Then I stuffed it in a dummy template:
---code begin---
class Zorg_implementation; //to be defined by another library
template <typename Z> class Zorg_t {
public:
Zorg_t(): zi(new Z) {}
~Zorg_t() {delete zi;}
private:
Z *zi;
};
typedef Zorg_template<Zorg_implementation> Zorg;
---code end---
This compiles and runs fine. Zorg_wrapper instances can be created
by one library, while another library defines the Zorg_implementation class.
This strikes me as odd - I would expect the templated version to have the
same effect as the simple version.
Can anybody explain what is going on here? Is this standard behaviour
or a VC8 quirk? Can I rely on it - or should I define a factory class?
Ole Nielsby
(my reply address has a question that must be answered)