![]() |
dynamic cast and derived classes
Hi,
I have a class D1 derived from a base class B. I also have some methods that accept either a pointer to D1 or a pointer by reference to D1, as in bool method1(D1* &d1_); bool method2(D1* d1_); //etc.. This worked well, until I discovered that I need to apply the same methods on ANOTHER class derived from B, D2. Since I was hoping it might be possible to use the same methods I use for D1, (without creating duplicates) I redefined all these methods in order that they will accept B instead of D1. I thought this was a "more general" version of these methods. So I had bool method1(B* &b_); bool method2(B* b_); //etc.. The problem is, nothing works any more. The compiler complains if I pass pointers of D1 or D2 to the "more generic" methods: "no matching function for call to.." I was considering the possibility of casting the D1/D2 pointer to a B pointer through dynamic_cast, but I don't know if this is the correct approach. Will I still be able to manipulate D1-specific or D2-specific features from within the method, if I pass a B-downcast pointer to it? What do you suggest to do? Thanks a lot F. |
Re: dynamic cast and derived classes
On Jul 29, 1:24*am, Fred Mangusta <a...@bbb.it> wrote:
> Hi, > > I have a class D1 derived from a base class B. I also have some methods > that accept either a pointer to D1 or a pointer by reference to D1, as in > > bool method1(D1* &d1_); > > bool method2(D1* d1_); > > //etc.. > > This worked well, until I discovered that I need to apply the same > methods on ANOTHER class derived from B, D2. > > Since I was hoping it might be possible to use the same methods I use > for D1, (without creating duplicates) I redefined all these methods in > order that they will accept B instead of D1. I thought this was a "more > general" version of these methods. So I had > > bool method1(B* &b_); > > bool method2(B* b_); > > //etc.. > > The problem is, nothing works any more. The compiler complains if I pass > * *pointers of D1 or D2 to the "more generic" methods: > > "no matching function for call to.." > > I was considering the possibility of casting the D1/D2 pointer to a B > pointer through dynamic_cast, but I don't know if this is the correct > approach. Will I still be able to manipulate D1-specific or D2-specific > features from within the method, if I pass a B-downcast pointer to it? > > What do you suggest to do? > > Thanks a lot > > F. >>> Will I still be able to manipulate D1-specific or D2-specific features from within the method If you need access to D1 _OR_ D2 specific stuff.. then you need 2 different methods. Or layer your design/implementation differently. If its just a couple of lines worth of a function, wouldn't it be easier to copy-paste and modify that function? Yeah, you'll go to Designer's Hell, but hey - time is money :) Perhaps: void myBaseStuff(B*); void myD1Stuff(D1* x) { myBaseStuff(x); moreD1Stuff(x); } void myD2Stuff(D2* x) { myBaseStuff(x); moreD2Stuff(x); } Cheers! -- raicuandi |
Re: dynamic cast and derived classes
Fred Mangusta wrote:
> Hi, > > I have a class D1 derived from a base class B. I also have some methods > that accept either a pointer to D1 or a pointer by reference to D1, as in > > bool method1(D1* &d1_); > > bool method2(D1* d1_); > > //etc.. > > > This worked well, until I discovered that I need to apply the same > methods on ANOTHER class derived from B, D2. > > Since I was hoping it might be possible to use the same methods I use > for D1, (without creating duplicates) I redefined all these methods in > order that they will accept B instead of D1. I thought this was a "more > general" version of these methods. So I had > Provided the methods make sense in the context of a B. Otherwise you could add a class derived from B with common components of D1 and D2 and derive D1 and D2 from that. > bool method1(B* &b_); > > bool method2(B* b_); > > //etc.. > > The problem is, nothing works any more. The compiler complains if I pass > pointers of D1 or D2 to the "more generic" methods: > > "no matching function for call to.." > You'll have to post some code if you want help with that. -- Ian Collins. |
Re: dynamic cast and derived classes
On Jul 28, 6:24 pm, Fred Mangusta <a...@bbb.it> wrote:
> I have a class D1 derived from a base class B. I also have > some methods that accept either a pointer to D1 or a pointer > by reference to D1, as in > bool method1(D1* &d1_); > bool method2(D1* d1_); > //etc.. > This worked well, until I discovered that I need to apply the > same methods on ANOTHER class derived from B, D2. > Since I was hoping it might be possible to use the same > methods I use for D1, (without creating duplicates) I > redefined all these methods in order that they will accept B > instead of D1. I thought this was a "more general" version of > these methods. So I had > bool method1(B* &b_); > bool method2(B* b_); > //etc.. > The problem is, nothing works any more. The compiler complains if I pass > pointers of D1 or D2 to the "more generic" methods: > "no matching function for call to.." Given a D1* or a D2*, you should be able to call method2. Or: bool method1a( B* const& b ) ; The reason you cannot call method1 is simple: it would require binding the reference to a temporary, and since the reference is to a non-const, presumably, you want to change it, and don't want the results of the change to just disappear when the temporary disappears. Note that while a D isA B (up to a certain point, at least), a D* is not a B*; the actual physical addresses may be different, for example. A D* converts to a B*, but the result of the conversion is a new, temporary pointer; in standard-speak, it is NOT an lvalue. > I was considering the possibility of casting the D1/D2 pointer to a B > pointer through dynamic_cast, but I don't know if this is the correct > approach. You can use static_cast as well, but since the conversion is implicit anyway, it probably won't change anything. The problem with method1 is that you are going to change the pointer. You'd have to do something like: D* original ; B* tmp = original ; method1( tmp ) ; original = tmp ; But why use a reference to a pointer anyway? Just have method1 take a pointer, and return a pointer (possibly null, if you want to indicate some error condition). Something like: B* method1b( B* ) ; D* original ; original = dynamic_cast< D* >( method1b( original ) ) ; > Will I still be able to manipulate D1-specific or D2-specific > features from within the method, if I pass a B-downcast > pointer to it? If the function knows that you are only going to pass it a D1*, and wants to use the D1 interface, you should declare it to take a D1*. If it takes a B*, it should be able to do everything it needs to do from the B interface. > What do you suggest to do? What is the actual problem? -- James Kanze (GABI Software) email:james.kanze@gmail.com Conseils en informatique orientée objet/ Beratung in objektorientierter Datenverarbeitung 9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34 |
Re: dynamic cast and derived classes
James Kanze wrote:
> What is the actual problem? > Hi James, I resorted to duplicate the needed methods (there are several) creating two of each: one that accepts D1 and one that accepts D2. I have some time constraints so I did this at the moment, but will study your suggestions and try to improve my code later. Thanks F. |
| All times are GMT. The time now is 12:20 PM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.