how to convert base class pointer to sub class pointer 
Author Message
 how to convert base class pointer to sub class pointer

My proble is below:
I have defined a Base class (derive from CObject),
and from the base class, derive some subclass.
and i save them into CObList (m_List).

then i use  Base* pCurrent = (Base *) m_List.GetNext(pos)
to get every sub class internal info.

but pCurrent is a pointer to Base class, can not found
sub class private data.
how can i do, how can i convert pCurrent to sub class pointer,
to get subclass private data.



Sat, 26 Jun 2004 17:24:49 GMT  
 how to convert base class pointer to sub class pointer
Assuming you have used DECLARE_DYNAMIC, you can do
        if(pCurrent->IsKindOf(RUNTIME_CLASS(CSubclass1)))
                 { /* subclass 1 */
                  CSubclass1 * sub = (CSubclass1 *)pCurrent;
                  .... do things with sub->members
                 } /* subclass 1 */
             else
             if(pCurrent->IsKindOf(RUNTIME_CLASS(COtherSubclass)))
                 { /* other subclass */
                  COtherSubclass * sub = (COtherSubclass *)pCurrent;
             ...do things with sub->members
                 } /* other subclass */
and so on.
                        joe

Quote:

>My proble is below:
>I have defined a Base class (derive from CObject),
>and from the base class, derive some subclass.
>and i save them into CObList (m_List).

>then i use  Base* pCurrent = (Base *) m_List.GetNext(pos)
>to get every sub class internal info.

>but pCurrent is a pointer to Base class, can not found
>sub class private data.
>how can i do, how can i convert pCurrent to sub class pointer,
>to get subclass private data.

Joseph M. Newcomer [MVP]

Web: http://www3.pgh.net/~newcomer
MVP Tips: http://www3.pgh.net/~newcomer/mvp_tips.htm


Sat, 26 Jun 2004 18:17:58 GMT  
 how to convert base class pointer to sub class pointer
you can use dynamic_cast<type>(ptr) - it is safe, but you need to
turn on RTTI in your project. And it is slow. Conversion is done during
runtime.

you can use static_cast<type>(ptr) - it is unsafe, but fast and you
don't need turn on anything. Conversion is done during compile-time.

CSubClass *sub=dynamic_cast<CSubClass *>(pCurrent);
or
CSubClass *sub=static_cast<CSubClass *>(pCurrent);

Quote:
> My proble is below:
> I have defined a Base class (derive from CObject),
> and from the base class, derive some subclass.
> and i save them into CObList (m_List).

> then i use  Base* pCurrent = (Base *) m_List.GetNext(pos)
> to get every sub class internal info.

> but pCurrent is a pointer to Base class, can not found
> sub class private data.
> how can i do, how can i convert pCurrent to sub class pointer,
> to get subclass private data.



Sat, 26 Jun 2004 18:07:01 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Converting from Base Class to Sub-Class

2. Inherited class function calls from a pointer assigned to the base class

3. TreeNode : Cast base class into sub class

4. how to convert base class pointer to derive class pointer ???

5. ATL and Base Class Pointers in COM

6. -- What is base class of smart pointer --

7. How to extract a pointer to another CDialog base class

8. pointer to base class

9. base class pointers

10. virtual base class pointers

11. virtual base classes pointers

12. STL-based Class with pointer problem

 

 
Powered by phpBB® Forum Software