design problem using c++ : casting to correct template. 
Author Message
 design problem using c++ : casting to correct template.

question:

first i am  using the ms Visual C++ 5.0 so the solution has to work for
this compiler
(it does not take member templates)

here is my problem i have defined a stl vector of pointers to type
AbstractEventSink

class AbstractEventSink
{
public:
 AbstractEventSink() {}
 virtual ~AbstractEventSink() {}

Quote:
};

the member actually being stored in the vector is of class:

template <class P1>
class EventSink  : public AbstractEventSink
{
 typedef EventSink<P1> Self;
public:
 EventSink(const CBFunctor1<P1> & cbf1) : _cbf1(cbf1) {}
 virtual ~EventSink() {}

 CBFunctor1<P1> _cbf1; // this functor can take

Quote:
};

my problem is once i have cast EventSink<P1> to AbstractEventSink
how do i get back to Event<P1> ?

typedef vector<AbstractEvnetSink*>  myvector;

myvector vec;

vec.push_back(  new  EventSink<int>(....) );
vec.push_back(  new  EventSink<long>(....) );

how do it do the following operation.

myvector::iterator iter;

for(iter = vec.begin() ; iter != vec.end() ; iter++)
{
    AbstractEventSink *p = *iter;

        SomeOperation(p)->_cbf1;

    //how do i cast/retrieve to correct type of EventSink<P1> ?
    //so i can access the   _cbf1 member?

Quote:
}

jim


Wed, 27 Jun 2001 03:00:00 GMT  
 design problem using c++ : casting to correct template.

Quote:

>question:

>first i am  using the ms visual c++ 5.0 so the solution has to work for
>this compiler
>(it does not take member templates)

>here is my problem i have defined a stl vector of pointers to type
>AbstractEventSink

>class AbstractEventSink
>{
>public:
> AbstractEventSink() {}
> virtual ~AbstractEventSink() {}

>};

>the member actually being stored in the vector is of class:

>template <class P1>
>class EventSink  : public AbstractEventSink
>{
> typedef EventSink<P1> Self;
>public:
> EventSink(const CBFunctor1<P1> & cbf1) : _cbf1(cbf1) {}
> virtual ~EventSink() {}

> CBFunctor1<P1> _cbf1; // this functor can take

>};

>my problem is once i have cast EventSink<P1> to AbstractEventSink
>how do i get back to Event<P1> ?

casting will *not* change the value of its argument but only yield a result
of the specified type.
Any reason the callbacks can't be virtual?  If not, then the right function
is selected at run time.  Recall, public inheritance models "is-a".

- Show quoted text -

Quote:

>typedef vector<AbstractEvnetSink*>  myvector;

>myvector vec;

>vec.push_back(  new  EventSink<int>(....) );
>vec.push_back(  new  EventSink<long>(....) );

>how do it do the following operation.

>myvector::iterator iter;

>for(iter = vec.begin() ; iter != vec.end() ; iter++)
>{
>    AbstractEventSink *p = *iter;

>        SomeOperation(p)->_cbf1;

>    //how do i cast/retrieve to correct type of EventSink<P1> ?
>    //so i can access the   _cbf1 member?
>}

>jim



Wed, 27 Jun 2001 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Problems using c++ templates in vc++6

2. help in Modern C++ Design ( new template Feature)

3. Question, templates using templates, CTypedPtrList problem

4. template implicit reference of pointer cast problem

5. correct way to cast pointers with offsets?

6. Type Casting? Correct Format

7. Is This Cast from GUID to SafeArray correct?

8. is this Cast correct?

9. This C++ seems to me to be correct (but Visual C++ 5.0 SP3 claims otherwise)

10. type cast problem of C++ in .Net

11. Problem using cast

12. C++ 101 - basic casting problem in ODBC Connection

 

 
Powered by phpBB® Forum Software