vtable on stack

Axel Freyn axel-freyn@gmx.de
Tue Feb 2 20:39:00 GMT 2010

Hi Patrick,
On Tue, Feb 02, 2010 at 09:56:19AM -0800, Patrick Horgan wrote:
>> There is also another possibilty you could maybe think about: If you
>> create an additional structure, which "caches" the pointer to A or B and
>> returns references to it, you can have this new structure living on the
>> stack - and do with it whatever you want (e.g. you can insert it into a
>> std::vector without problems):
>>  struct Cache{
>>   Cache( A* _a):a(_a){}
>>   A *a;
>>   A& operator()(){ return *a;}
>> };
> I like this whole thing, but wouldn't operator* be more natural to  
> return the reference?
Even if we are probably quite off-topic now: Yes, you are absolutely
right. I only used () as, when I first (already a few years ago) wrote a
similar class (a bit more complex) which I wanted to use for fast
matrix-vector calculations, I realized a problem with "*": it can lead
to unresolved overloads when you write something like
class GeneralCache { ... };
class Cache : public GeneralCache { ... };
... operator * (const GeneralCache &, const GeneralCache & );
Cache a, b, c;
c = a * b;
Now is "*" dereferencing b, or is it multiplying a and b? 
I don't remember the precise definitions I used - but at least g++
claimed it would have been an unresolvable overloading (And my reading
and partial understanding of the C++-standard did confirm this). Of
course, by adding parenthesis or temporaries it would be possible to
solve the problem, but I switched to operator() ...


More information about the Gcc-help mailing list