R
Raf256
I have base template class A<B>, and son class B.
Inside A<B> constrcutor, can I access a pointer to B, from "this"?
like
A<B>::A() : pointerToB(static_cast<B*>this) { }
I will use the pointer.
Can I use the pointer in A<B> constructor?
Can I use it at all, or is this Undefinned Behaviour (UB) - like, can the
pointer be invalidated after the point of calling A<B> ctor?
------
I need this for my small template framework for private implementation
pattern.
The idea is: I have a class cFoo, that have private implementation,
cFoo_impl.
cFoo is an "interface" class
cFoo_impl is a private class - holding details. The goal: the cFoo_impl is
hidden all in .cpp file along with implementation, so changes of details in
cFoo do not require all project files using cFoo class to recompile.
Recompilation of objects using cFoo is needed only if cFoo ABI/intrface
changes, and this is rare, since all the "details" are moved to cFoo_impl
class.
cFoo creates and holds a pointer to cFoo_impl and uses it.
cFoo_impl must have a pointer back to the cFoo, in case if cFoo_impl method
would like to call some general method from cFoo. This is causing the
problem.
Example code (seem to work, on g++, but is it guaranteed to work?)
A bit long.
No need to read it - just read question above.
--------------------------------------------------------------------------------------
=== 1) the PIMPL framework lib ===
template <typename typInterfClass>
class cImpl { // class that is a private implementation of real class /
interface
public:
typInterfClass *mInterf; // point only
cImpl(typInterfClass *aInterf) : mInterf(aInterf) { }
};
// ---------------------------------------
template <typename typImplClass, typename typInterfClass>
class cInter { // real class / interface that have a private implementation
public:
cPtrAuto<typImplClass> mImpl; // own, auto-delete
typInterfClass* ReturnThisAsInterf() { return
static_cast<typInterfClass*>(this); }
cInter() : mImpl( new typImplClass(ReturnThisAsInterf()) ) {}
};
=== 2) the .h interface of a class (we want it to be "constant") ===
class Foo_impl;
class Foo : public cInter<Foo_impl, Foo>, public cInfoStream {
// ..........
};
=== 3) the .cpp implementation (it could change a lot) ===
// implementation:
class Foo_impl : public cImpl<Foo> {
public:
Foo_impl(Foo *inter);
};
Foo_impl::Foo_impl(Foo *inter) : cImpl<Foo>(inter) { }
--------------------------------------------------------------------------------------
Inside A<B> constrcutor, can I access a pointer to B, from "this"?
like
A<B>::A() : pointerToB(static_cast<B*>this) { }
I will use the pointer.
Can I use the pointer in A<B> constructor?
Can I use it at all, or is this Undefinned Behaviour (UB) - like, can the
pointer be invalidated after the point of calling A<B> ctor?
------
I need this for my small template framework for private implementation
pattern.
The idea is: I have a class cFoo, that have private implementation,
cFoo_impl.
cFoo is an "interface" class
cFoo_impl is a private class - holding details. The goal: the cFoo_impl is
hidden all in .cpp file along with implementation, so changes of details in
cFoo do not require all project files using cFoo class to recompile.
Recompilation of objects using cFoo is needed only if cFoo ABI/intrface
changes, and this is rare, since all the "details" are moved to cFoo_impl
class.
cFoo creates and holds a pointer to cFoo_impl and uses it.
cFoo_impl must have a pointer back to the cFoo, in case if cFoo_impl method
would like to call some general method from cFoo. This is causing the
problem.
Example code (seem to work, on g++, but is it guaranteed to work?)
A bit long.
No need to read it - just read question above.
--------------------------------------------------------------------------------------
=== 1) the PIMPL framework lib ===
template <typename typInterfClass>
class cImpl { // class that is a private implementation of real class /
interface
public:
typInterfClass *mInterf; // point only
cImpl(typInterfClass *aInterf) : mInterf(aInterf) { }
};
// ---------------------------------------
template <typename typImplClass, typename typInterfClass>
class cInter { // real class / interface that have a private implementation
public:
cPtrAuto<typImplClass> mImpl; // own, auto-delete
typInterfClass* ReturnThisAsInterf() { return
static_cast<typInterfClass*>(this); }
cInter() : mImpl( new typImplClass(ReturnThisAsInterf()) ) {}
};
=== 2) the .h interface of a class (we want it to be "constant") ===
class Foo_impl;
class Foo : public cInter<Foo_impl, Foo>, public cInfoStream {
// ..........
};
=== 3) the .cpp implementation (it could change a lot) ===
// implementation:
class Foo_impl : public cImpl<Foo> {
public:
Foo_impl(Foo *inter);
};
Foo_impl::Foo_impl(Foo *inter) : cImpl<Foo>(inter) { }
--------------------------------------------------------------------------------------