A
Andy Lomax
I'm using a library where the supplier has provided base class 'foo',
and a reference counting class which wraps 'foo':
------------------- library code -----------------------
template<class T>
refcount {
... // reference counting wrapper; like shared_ptr
};
class foo {
...
};
typedef refcount<foo> fooref;
--------------------------------------------------------
Now, when I use the library, I have to derive from 'foo' to make it
useful. I also have to add a new function which is *not* declared
virtual in 'foo':
----------------------- my code ------------------------
class myfoo : public foo {
...
void myfunc(void);
}
--------------------------------------------------------
Problem: if I know that a 'fooref' actually points to a 'myfoo', how
do I call 'myfunc'?
void a(fooref b) {
b->myfunc(); // compiler error: foo has no myfunc
static_cast<myfooref>(b)->myfunc(); // 1
static_cast<myfoo *>(&*b)->myfunc(); // 2
}
Both (1) and (2) compile, but are they the same thing? I understand
(2), but (1) is more confusing. Can I cast a templated class in this
way?
Cheers
AL
and a reference counting class which wraps 'foo':
------------------- library code -----------------------
template<class T>
refcount {
... // reference counting wrapper; like shared_ptr
};
class foo {
...
};
typedef refcount<foo> fooref;
--------------------------------------------------------
Now, when I use the library, I have to derive from 'foo' to make it
useful. I also have to add a new function which is *not* declared
virtual in 'foo':
----------------------- my code ------------------------
class myfoo : public foo {
...
void myfunc(void);
}
--------------------------------------------------------
Problem: if I know that a 'fooref' actually points to a 'myfoo', how
do I call 'myfunc'?
void a(fooref b) {
b->myfunc(); // compiler error: foo has no myfunc
static_cast<myfooref>(b)->myfunc(); // 1
static_cast<myfoo *>(&*b)->myfunc(); // 2
}
Both (1) and (2) compile, but are they the same thing? I understand
(2), but (1) is more confusing. Can I cast a templated class in this
way?
Cheers
AL