473,695 Members | 2,797 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(stat ic_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(typInterf Class *aInterf) : mInterf(aInterf ) { }
};
// ---------------------------------------

template <typename typImplClass, typename typInterfClass>
class cInter { // real class / interface that have a private implementation
public:
cPtrAuto<typImp lClass> mImpl; // own, auto-delete
typInterfClass* ReturnThisAsInt erf() { return
static_cast<typ InterfClass*>(t his); }

cInter() : mImpl( new typImplClass(Re turnThisAsInter f()) ) {}
};

=== 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_i mpl(Foo *inter) : cImpl<Foo>(inte r) { }


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


--
Rafa³ Maj
Raf256 - Raf256.org - C++ (comming soon)
Jun 23 '06 #1
1 1599
Raf256 <e7**********@i news.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(stat ic_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
7868
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 impl within a class as such: a.h class A { ...stuff... struct AImpl; AImpl* _impl;
5
1475
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
1049
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
6110
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 declaration of pimpl class is provided. Example:
34
3689
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 thinking of making all these pimpl class declarations public. Reasoning is that since only the code within the ..cc file will need to ever access them, why protect them in ways that would make access to them more difficult and obfuscated? What...
3
2030
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 I'm explicitly trying to call the TYPE::TYPE() constructor on it by using the pointer. That doesn't compile, yet if I forget the usage of TYPE and write the line to use the actual constructor, then it's fine. See the code below for an example.
4
4671
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) Generic observer design in C++. I have been pushing the Austria "Twin" interface for a while and more recently the multi threaded version of it "TwinMT" (see the 6129 alpha on sourceforge)
6
2182
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 something like process(openEv& ev), process(closeEv& ev), ... no problem with this point. Going further in this way, i thought that the process member function would be a great candidate for a template. Hum ... it is not. The process function have to be...
50
4479
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): //------------------------------------------------------------------ template<typename Data_t> class SmartPointer { Data_t* data; void(*deleterFunc)(Data_t*);
0
8630
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8568
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9115
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8845
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6491
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5839
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4340
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4579
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3003
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.