Click here to Skip to main content
15,892,809 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
Questionany idea for "auto-cast"ing? Pin
includeh1010-Aug-10 0:24
includeh1010-Aug-10 0:24 
AnswerRe: any idea for "auto-cast"ing? Pin
Sauro Viti10-Aug-10 0:44
professionalSauro Viti10-Aug-10 0:44 
AnswerRe: any idea for "auto-cast"ing? Pin
CPallini10-Aug-10 0:45
mveCPallini10-Aug-10 0:45 
AnswerRe: any idea for "auto-cast"ing? PinPopular
Aescleal10-Aug-10 1:29
Aescleal10-Aug-10 1:29 
GeneralRe: any idea for "auto-cast"ing? Pin
Chris Losinger10-Aug-10 5:29
professionalChris Losinger10-Aug-10 5:29 
GeneralRe: any idea for "auto-cast"ing? Pin
Aescleal10-Aug-10 5:59
Aescleal10-Aug-10 5:59 
GeneralRe: any idea for "auto-cast"ing? Pin
Chris Losinger10-Aug-10 6:09
professionalChris Losinger10-Aug-10 6:09 
GeneralRe: any idea for "auto-cast"ing? Pin
Aescleal10-Aug-10 10:11
Aescleal10-Aug-10 10:11 
I'd have thought it would have shown that inheritance is not the correct thing to model "real life" in this case. If during processing of a collection you can't handle them in a uniform way then it's really dodgy gathering them up in the same place. If they haven't got a consistent interface to them then you're just asking for big headaches later.

In the "real world" (which is the place presumably programs deal with "real life") as soon as you start tacking on little lumps of interface on an ad-hoc basis you're opening the poor bugger that comes after you with a nightmare when he or she has to add a new concrete type that implements an interface. Not only have they got to implement the interface but they have to grub around in every client of the interface looking for the place where some form of dynamic_cast is happening and check there's no conversion there. If that's "real life" I'll stick with my ivory tower.

So if you really need to have something that processes two related classes in a different way, why not maintain a collection for each type? Something like:

std::vector<A> the_As;
std::vector<B> the_Bs;

You can then replace all the looping and dynamic_casting with:

std::for_each( the_As.begin(), the_As.end(), std::mem_fun( &A::do_something_A_related ) );
std::for_each( the_Bs.begin(), the_Bs.end(), std::mem_fun( &B::do_something_B_related ) );


replacing the final arguments with something that makes sense in either context (functors, bound functions taking different arguments)? Then it doesn't matter if A and B are related or not - you can process them independently or as a group. Admittedly you're still handling concrete types rather than interfaces but at least you're separating out the functionality rather than munging them together in one great melange. You can even have a collection for the interface pointers:

std::vector<I *> the_Is;

and not have to worry about deleting the objects pointed to as the other two collections handle that.

Another alternative is to say "A and B aren't related but share some characteristics I'd like to process similarly." You then implement A and B as concrete types without any inheritance relationship and THEN implement two bridges or adapters that implement I in terms of A and B.

Both these (rather stream of consciousnes) examples show that there's more ways to skin the feline than just saying "Oh sod it, dynamic_cast the difference." As I said before dynamic_casting or type switching is a sign that there's something cleaner inside waiting to get out.

Cheers,

Ash
GeneralRe: any idea for "auto-cast"ing? Pin
Chris Losinger10-Aug-10 10:33
professionalChris Losinger10-Aug-10 10:33 
GeneralRe: any idea for "auto-cast"ing? Pin
Aescleal10-Aug-10 19:53
Aescleal10-Aug-10 19:53 
GeneralRe: any idea for "auto-cast"ing? Pin
Moak10-Aug-10 23:33
Moak10-Aug-10 23:33 
GeneralRe: any idea for "auto-cast"ing? Pin
Chris Losinger11-Aug-10 1:08
professionalChris Losinger11-Aug-10 1:08 
QuestionLINK : fatal error LNK1104: cannot open file 'MyCplusplusWithSTL.lib' after converting VS2008 to VS2010 Pin
Chesnokov Yuriy9-Aug-10 22:43
professionalChesnokov Yuriy9-Aug-10 22:43 
AnswerRe: LINK : fatal error LNK1104: cannot open file 'MyCplusplusWithSTL.lib' after converting VS2008 to VS2010 Pin
Richard MacCutchan10-Aug-10 0:01
mveRichard MacCutchan10-Aug-10 0:01 
GeneralRe: LINK : fatal error LNK1104: cannot open file 'MyCplusplusWithSTL.lib' after converting VS2008 to VS2010 Pin
Chesnokov Yuriy10-Aug-10 0:17
professionalChesnokov Yuriy10-Aug-10 0:17 
GeneralRe: LINK : fatal error LNK1104: cannot open file 'MyCplusplusWithSTL.lib' after converting VS2008 to VS2010 Pin
Richard MacCutchan10-Aug-10 0:46
mveRichard MacCutchan10-Aug-10 0:46 
GeneralRe: LINK : fatal error LNK1104: cannot open file 'MyCplusplusWithSTL.lib' after converting VS2008 to VS2010 Pin
Chesnokov Yuriy10-Aug-10 0:56
professionalChesnokov Yuriy10-Aug-10 0:56 
GeneralRe: LINK : fatal error LNK1104: cannot open file 'MyCplusplusWithSTL.lib' after converting VS2008 to VS2010 Pin
Richard MacCutchan10-Aug-10 1:40
mveRichard MacCutchan10-Aug-10 1:40 
Questionshell32.dll Pin
dilara semerci9-Aug-10 22:27
dilara semerci9-Aug-10 22:27 
AnswerRe: shell32.dll Pin
«_Superman_»9-Aug-10 22:37
professional«_Superman_»9-Aug-10 22:37 
AnswerRe: shell32.dll Pin
ThatsAlok9-Aug-10 22:45
ThatsAlok9-Aug-10 22:45 
AnswerRe: shell32.dll Pin
Aescleal10-Aug-10 0:15
Aescleal10-Aug-10 0:15 
GeneralRe: shell32.dll Pin
dilara semerci10-Aug-10 0:23
dilara semerci10-Aug-10 0:23 
QuestionRe: shell32.dll Pin
David Crow10-Aug-10 3:45
David Crow10-Aug-10 3:45 
AnswerRe: shell32.dll Pin
dilara semerci10-Aug-10 3:59
dilara semerci10-Aug-10 3:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.