A<B>::ctor() : pointer( (B*) this ) - used for private implementaion (PIMPL) pattern - template fram

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) { }




--------------------------------------------------------------------------------------
 
R

Raf256

Raf256 said:
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?

In my friends opinion:

[23:02] <pfeilspitze> raf256: in the base class's constructor, the derived
class is not yet constructed, so you cannot use it.

[23:03] <raf256> pfeilspitze: ok. But, can I hold a pointer to derived
class, and use it later (after all CTORs are done), or will the pointer
address change?

[23:03] <pfeilspitze> holding a pointer is safe
[23:03] <pfeilspitze> it's like it's an incomplete type, raf256


So we are correct, I can acquire a pointer to B inside A<B>::A<B>(), and I
can use it - only I have to NOT use it inside the constructor?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top