 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Guest
|
Posted: Wed Nov 08, 2006 4:17 am Post subject: Virtual inheritance for pimpl idiom |
|
|
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 ] |
|
| Back to top |
|
 |
James Kanze Guest
|
Posted: Wed Nov 08, 2006 5:30 pm Post subject: Re: Virtual inheritance for pimpl idiom |
|
|
alexei.alexandrov (AT) gmail (DOT) com wrote:
| Quote: | 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.
| Quote: | 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.
| Quote: | 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 (AT) gmail (DOT) 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 ] |
|
| Back to top |
|
 |
Andrei Polushin Guest
|
Posted: Wed Nov 08, 2006 5:30 pm Post subject: Re: Virtual inheritance for pimpl idiom |
|
|
alexei.alexandrov (AT) gmail (DOT) com wrote:
| Quote: | (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.
| Quote: | 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 ] |
|
| Back to top |
|
 |
Guest
|
Posted: Thu Nov 09, 2006 5:21 pm Post subject: Re: Virtual inheritance for pimpl idiom |
|
|
James Kanze wrote:
| Quote: |
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.
| Quote: |
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 ] |
|
| Back to top |
|
 |
James Kanze Guest
|
Posted: Thu Nov 09, 2006 5:21 pm Post subject: Re: Virtual inheritance for pimpl idiom |
|
|
Andrei Polushin wrote:
| Quote: | alexei.alexandrov (AT) gmail (DOT) 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.
| Quote: | 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 (AT) gmail (DOT) 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 ] |
|
| Back to top |
|
 |
James Kanze Guest
|
Posted: Fri Nov 10, 2006 5:15 pm Post subject: Re: Virtual inheritance for pimpl idiom |
|
|
alexei.alexandrov (AT) gmail (DOT) com wrote:
| Quote: | 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).
| Quote: | 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.
| Quote: | 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 (AT) gmail (DOT) 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 ] |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|