⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wpw_mfc_objects_95.html

📁 VC programing
💻 HTML
字号:
<HTML>

<HR><A NAME=MFC_COBJLIST>
Return to <a href="wpw_mfc_index.html#TOC">Table of Contents for this chapter</a><br> 
</A><H4>Subject: MFC: Question about CObList</H4><PRE>
perkel@dolphin.upenn.edu (Jeffrey M. Perkel) writes:

>Hi, all,

>I have a question about derived classes in CObList.  I was just reading 
>the comp.lang.c++ FAQ file, and I found this (Q51, if you're interested):

>C++ allows a Derived* to be converted to a Base*, since a Derived object
>is a kind of a Base object.  However trying to convert a Derived** to a
>Base** is (correctly) flagged as an error (if it was allowed, the Base**
>could be dereferenced (yielding a Base*), and the Base* could be made to
>point to an object of a DIFFERENT derived class.  This would be an error. 

>As a corollary, an array of Deriveds is-NOT-a-kind-of array of Bases.

>-----------
>Now, my question is this:  suppose I want to have a CObList (or 
>derived-class thereof) that contains a linked list of CObject-derived 
>classes ALL OF DIFFERENT TYPES.  For example:

>class A : public CObject { };
>class B : public CObject { };
>class C : public CObject { };

>class D : public CObject { 
>       CObList *m_pList; <-- needs to contain objects of A, B, and C
>       ...
>}

>Does the FAQ answer above mean that such a list is illegal?  
No. Works fine.
>Secondly, 
>suppose the list was created, and I needed to obtain a pointer to a 
>certain element.  How would I be able to know how to cast the obtained 
>pointer (is this one of the times when IsKindOf() is required? ).
Yes.
>Thanks for any advice!

>Jeff

>--
>Jeffrey Perkel perkel@dolphin.upenn.edu
>"I think I'm a clone now, 'cause every chromosome is a hand-me-down!"
>                                      "Wierd" Al Yankovic
-- 
   Niels Ull Jacobsen, Dep. of CS, U of Copenhagen (null@diku.dk)
   Roenne Alle 3 st.th, 2860 Soeborg, Denmark, tel. +45 39 66 39 86
In article <3rpifb$rjc@netnews.upenn.edu>
           perkel@dolphin.upenn.edu "Jeffrey M. Perkel" writes:

> ....
> Now, my question is this:  suppose I want to have a CObList (or 
> derived-class thereof) that contains a linked list of CObject-derived 
> classes ALL OF DIFFERENT TYPES.  For example:
> 
> class A : public CObject { };
> class B : public CObject { };
> class C : public CObject { };
> 
> class D : public CObject { 
>         CObList *m_pList; <-- needs to contain objects of A, B, and C
>         ...
> }
> 
> Does the FAQ answer above mean that such a list is illegal?  Secondly, 
> suppose the list was created, and I needed to obtain a pointer to a 
> certain element.  How would I be able to know how to cast the obtained 
> pointer (is this one of the times when IsKindOf() is required? ).

No it should be fine.  IsKindOf() is indeed useful for this.

However a suggestion.  Why not try

class MyGenericObject : public CObject

class A : public MyGenericObject 
class B : public MyGenericObject 
class C : public MyGenericObject 
class D : public MyGenericObject 

Then add all the virtual functions you need to these.  Than when
you get a (CObject *) from your list, just cast it to
(MyGenericObject *) and call the appropriate virtual functions.

It will avoid lots of if, then, elses with IsKindOf()s in them.

Dave
-- 
+---------------------------+-----------------------------------+
| Dave Webber                        dave@musical.demon.co.uk   |
| Author of MOZART, the Windows 3.1 shareware Music Processor   |
| and member of the Association of Shareware Professionals      |
+---------------------------+-----------------------------------+
<HR>
In article: <3rpifb$rjc@netnews.upenn.edu>  perkel@dolphin.upenn.edu (Jeffrey M. Perkel) writes:

> Now, my question is this:  suppose I want to have a CObList (or 
> derived-class thereof) that contains a linked list of CObject-derived 
> classes ALL OF DIFFERENT TYPES.  For example:
> 
> class A : public CObject { };
> class B : public CObject { };
> class C : public CObject { };
> 
> class D : public CObject { 
>       CObList *m_pList; <-- needs to contain objects of A, B, and C
>       ...
> }
> 
> Does the FAQ answer above mean that such a list is illegal?  Secondly, 
> suppose the list was created, and I needed to obtain a pointer to a 
> certain element.  How would I be able to know how to cast the obtained 
> pointer (is this one of the times when IsKindOf() is required? ).
> 

You could try this approach:

class CBaseClass : public CObject { };

class A : public CBaseClass { };
class B : public CBaseClass { };
class C : public CBaseClass { };

class D : public CObject { 
  CObList *m_pList;         // Could be 'CObList m_List', I think...
        ...
}

This way you could cast your list objects to CBaseClass *, providing 
you only ever add CBaseClass objects to your list.  You could make 
your own type-safe list by deriving it from CObList:

class CBaseClassList : CObList { };

class D : public CObject { 
  CBaseClassList *m_pList;
  CBaseClassList m_List;    // this would work too
        ...
}

There's another thread going on at present about how to make this 
work, so I won't repeat that here.

If you need to know whether your list object is a class A, B or C 
(rather than a CBaseClass), then you may be missing the point.  What 
you should do is make sure that everything you want to do with A, B and 
C is listed in CBaseClass.  Use virtual functions if the derived 
classes do a particular operation slightly differently.

Sorry this isn't very clear, I'm an engineer, not a teacher :-)

Let me know if I can help more

Andrew Gebbie
<HR>
perkel@dolphin.upenn.edu (Jeffrey M. Perkel) wrote:

>Hi, all,

>I have a question about derived classes in CObList.  I was just reading 
>the comp.lang.c++ FAQ file, and I found this (Q51, if you're interested):

>C++ allows a Derived* to be converted to a Base*, since a Derived object
>is a kind of a Base object.  However trying to convert a Derived** to a
>Base** is (correctly) flagged as an error (if it was allowed, the Base**
>could be dereferenced (yielding a Base*), and the Base* could be made to
>point to an object of a DIFFERENT derived class.  This would be an error. 

>As a corollary, an array of Deriveds is-NOT-a-kind-of array of Bases.

>-----------
>Now, my question is this:  suppose I want to have a CObList (or 
>derived-class thereof) that contains a linked list of CObject-derived 
>classes ALL OF DIFFERENT TYPES.  For example:

>class A : public CObject { };
>class B : public CObject { };
>class C : public CObject { };

>class D : public CObject { 
>       CObList *m_pList; <-- needs to contain objects of A, B, and C
>       ...
>}

>Does the FAQ answer above mean that such a list is illegal?

No, it just means that when you get a pointer from the CObList, you
can only use it as a CObject, i.e. only access the interface provided
by CObject, not that provided by your derived classes.

>Secondly, 
>suppose the list was created, and I needed to obtain a pointer to a 
>certain element.  How would I be able to know how to cast the obtained 
>pointer (is this one of the times when IsKindOf() is required? ).

What are you trying to do? I mean, what are the requirements which you
are trying to satisfy with this design? A design using a completely
heterogeneous list and run time type identification may indicate that
you have missed identifying an abstraction -- i.e. that there is a
class Base which provides the interface which all the objects in the
list need, and that A,B,C and D should be derived from base.

If the objects really are different, why are they in the same list?


>Thanks for any advice!

I'll be interested to hear what your application needs to do.

Regards,
        Tom Davies

>Jeff

>--
>Jeffrey Perkel perkel@dolphin.upenn.edu
>"I think I'm a clone now, 'cause every chromosome is a hand-me-down!"
>                                       - "Wierd" Al Yankovic

</PRE 
></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -