By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,227 Members | 1,214 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,227 IT Pros & Developers. It's quick & easy.

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

P: n/a

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


--------------------------------------------------------------------------------------


--
Rafał Maj
Raf256 - Raf256.org - C++ (comming soon)
Jun 23 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Raf256 <e7**********@inews.gazeta.pl> Friday 23 of June 2006 22:59
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?

--
Rafał Maj
Raf256 - Raf256.org - C++ (comming soon)
Jun 23 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.