473,396 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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


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
1 1583
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Icosahedron | last post by:
I've been going through some old code trying to clean it up and rearchitect it based on more modern C++ idioms. In the old code I often used the Pimpl idiom on a class by class basis, creating...
5
by: Gonçalo Rodrigues | last post by:
Hi all, (note: newbie alert) Suppose you have a hierarchy of objects that you deal with via smart pointers, e.g. something like: template<typename T> class Ref { private:
1
by: Ney André de Mello Zunino | last post by:
Hello. Given the following template class: template <unsigned int dim = 3> class Morfologia { public: static Pgm dilata(const Pgm&, const ElementoEstruturante<dim>&); // ...
2
by: Peteris Krumins | last post by:
Hello! I was playing around pimpl idiom and discovered that there is a problem with it if a class member template function exists which has to access private data, since only the forward...
34
by: Asfand Yar Qazi | last post by:
Hi, I'm creating a library where several classes are intertwined rather tightly. I'm thinking of making them all use pimpls, so that these circular dependancies can be avoided easily, and I'm...
3
by: mike.arsenault | last post by:
Hello I need some help from anyone that can provide it. Below is a function inside a template collection class that I'm writing. I have a TYPE * that points to an allocated memory location, and...
4
by: Gianni Mariani | last post by:
Two issues here: a) What is the accepted definition of "observer pattern". While I can't point to anything specific, I do remember having issues with inconsistency in the definition. b)...
6
by: pcrepieux | last post by:
Hi, I recently meet a problem while "playing" with the state pattern. I was wondering if each of the member function dedicated to handle events open(), close(), ack() could be change to...
50
by: Juha Nieminen | last post by:
I asked a long time ago in this group how to make a smart pointer which works with incomplete types. I got this answer (only relevant parts included): ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.