Topic: Virtual inheritance for pimpl idiom
Author: "James Kanze" <james.kanze@gmail.com>
Date: Fri, 10 Nov 2006 11:15:28 CST Raw View
alexei.alexandrov@gmail.com wrote:
> James Kanze wrote:
> > Virtual heritance has nothing to do with the pimpl idiom. I
> > don't know where you got that idea. The elements in the
> > implementation class of a pimpl are totally invisible to the
> > user of the class. The elements of a virtual base class are an
> > integral part of the class, as the user sees it.
> If I could inherit in virtual-private way from an incomplete type and
> then define it in *.cpp file, the details would be completely hidden
> from the external user.
Some of the details. Not all of them. The most obvious
exception is sizeof. But also, if the virtual base declares a
function as virtual, it will also be virtual in the derived
class(es). Any class which derives from your class must call
the constructor of the virtual base class, even if the
derivation is private (which pretty much means that the virtual
base constructor cannot require parameters, since you cannot
name it).
> I understand that probably it's not implementable because of other
> language features. But I think there is an analogy: indirect creation
> and referencing.
I'm not sure what you mean by indirect creation and referencing.
A virual base class.
> > I'm missing something: why you think that pimpl and virtual
> > inheritance are in any way related.
> They look alike because they increase the size of the derived class by
> a constant (sizeof(Impl *) or sizeof(int) in case of offset), not by
> sizeof(Impl).
What? Virtual inheritance increases the size of the derived
class by at least sizeof(VirtualBase) (unless the virtual base
is empty, and the empty base class optimization is in effect).
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S mard, 78210 St.-Cyr-l' cole, France, +33 (0)1 30 23 00 34
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: alexei.alexandrov@gmail.com
Date: Thu, 9 Nov 2006 10:16:21 CST Raw View
Andrei Polushin wrote:
>
> It's not the same as pointer, because it could be implemented as a
> constant offset stored in the vtable.
>
> class Base {};
> class Derived : virtual public Base {};
>
> Could be implemented as:
>
> struct Base {};
>
> struct Derived {
> const Derived_vtable* vptr;
>
> operator Base&() {
> return *(Base*)( // calculate pointer to Base
> (char*)this + vptr->Base_offset
> );
> }
> };
>
> struct Derived_vtable {
> const size_t Base_offset;
> // ...
> };
>
> In other words, virtual inheritance is a static mechanism, and pimpl
> pointer needs to be dynamic.
>
>
Hmm, but the sizeof(Derived) still doesn't depend on sizeof(Base), does
it?
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: AlbertoBarbati@libero.it (Alberto Ganesh Barbati)
Date: Thu, 9 Nov 2006 17:10:40 GMT Raw View
alexei.alexandrov@gmail.com ha scritto:
> Andrei Polushin wrote:
>>
>> class Base {};
>> class Derived : virtual public Base {};
>>
>
> Hmm, but the sizeof(Derived) still doesn't depend on sizeof(Base), does
> it?
>
Of course it does. A Derived object contains an entire sub-object of
type Base, not just a pointer to it, so how could it be otherwise?
Ganesh
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: "James Kanze" <james.kanze@gmail.com>
Date: Thu, 9 Nov 2006 11:21:49 CST Raw View
Andrei Polushin wrote:
> alexei.alexandrov@gmail.com wrote:
> > (since virtual inheritance is essentially
> > exactly the same as pimpl - pointer to a class),
> It's not the same as pointer, because it could be implemented as a
> constant offset stored in the vtable.
Usually is, I think. Regardless of the implementation, however,
there's virtually no relationship between pimpls and virtual
base classes. Members of a virtual base class are members of
the derived class, which is definitly not the case for a virtual
base class. This affects all sorts of things, from sizeof, to
whether a function is virtual, or even whether it could be
called, and numerous other things.
> In other words, virtual inheritance is a static mechanism, and pimpl
> pointer needs to be dynamic.
The most important point, of course, is that the virtual base
class is part of the derived object. Contiguous with it in
memory.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S mard, 78210 St.-Cyr-l' cole, France, +33 (0)1 30 23 00 34
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: alexei.alexandrov@gmail.com
Date: Thu, 9 Nov 2006 11:21:01 CST Raw View
James Kanze wrote:
>
> Virtual heritance has nothing to do with the pimpl idiom. I
> don't know where you got that idea. The elements in the
> implementation class of a pimpl are totally invisible to the
> user of the class. The elements of a virtual base class are an
> integral part of the class, as the user sees it.
>
If I could inherit in virtual-private way from an incomplete type and
then define it in *.cpp file, the details would be completely hidden
from the external user.
I understand that probably it's not implementable because of other
language features. But I think there is an analogy: indirect creation
and referencing.
>
> I'm missing something: why you think that pimpl and virtual
> inheritance are in any way related.
>
They look alike because they increase the size of the derived class by
a constant (sizeof(Impl *) or sizeof(int) in case of offset), not by
sizeof(Impl).
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: alexei.alexandrov@gmail.com
Date: Tue, 7 Nov 2006 22:17:13 CST Raw View
Hi,
does anybody know why C++ compilers don't allow you to inherit
virtually from an incomplete class? Logically I don't see any reasons
for it to be impossible (since virtual inheritance is essentially
exactly the same as pimpl - pointer to a class), but probably there is
something I'm missing and it would be nice if someone could shed some
light on this for me.
What I would like to do is to use virtual inheritance for pimpl idiom:
// foo.hpp
class FooImpl;
class Foo : virtual public FooImpl
{
public:
Foo();
void doSomething();
};
// foo.cpp
class FooImpl
{
public:
int m_data;
}
Foo::Foo()
{
m_data = 0;
}
void doSomething()
{
m_data++;
}
The convenience would be that you use the data members as usually.
Is it possible in theory?
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: "Andrei Polushin" <polushin@gmail.com>
Date: Wed, 8 Nov 2006 11:30:16 CST Raw View
alexei.alexandrov@gmail.com wrote:
> (since virtual inheritance is essentially
> exactly the same as pimpl - pointer to a class),
It's not the same as pointer, because it could be implemented as a
constant offset stored in the vtable.
class Base {};
class Derived : virtual public Base {};
Could be implemented as:
struct Base {};
struct Derived {
const Derived_vtable* vptr;
operator Base&() {
return *(Base*)( // calculate pointer to Base
(char*)this + vptr->Base_offset
);
}
};
struct Derived_vtable {
const size_t Base_offset;
// ...
};
In other words, virtual inheritance is a static mechanism, and pimpl
pointer needs to be dynamic.
> What I would like to do is to use virtual inheritance for pimpl idiom:
> [...]
> The convenience would be that you use the data members as usually.
> Is it possible in theory?
Three days ago, I've opened a topic about "Inheriting from reference"
http://groups.google.com/group/comp.std.c++/browse_frm/thread/787899de55746225/
that makes it "possible in theory", but there is still no feedback.
--
Andrei Polushin
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: "James Kanze" <james.kanze@gmail.com>
Date: Wed, 8 Nov 2006 11:30:07 CST Raw View
alexei.alexandrov@gmail.com wrote:
> does anybody know why C++ compilers don't allow you to inherit
> virtually from an incomplete class?
Because it can't be made to work. I don't actually see where
virtuality of inheritance changes much here.
> Logically I don't see any reasons
> for it to be impossible (since virtual inheritance is essentially
> exactly the same as pimpl - pointer to a class),
Virtual heritance has nothing to do with the pimpl idiom. I
don't know where you got that idea. The elements in the
implementation class of a pimpl are totally invisible to the
user of the class. The elements of a virtual base class are an
integral part of the class, as the user sees it.
> but probably there is something I'm missing and it would be
> nice if someone could shed some light on this for me.
I'm missing something: why you think that pimpl and virtual
inheritance are in any way related.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S mard, 78210 St.-Cyr-l' cole, France, +33 (0)1 30 23 00 34
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]