Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > dynamic cast and derived classes

Reply
Thread Tools

dynamic cast and derived classes

 
 
Fred Mangusta
Guest
Posts: n/a
 
      07-28-2008
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.

 
Reply With Quote
 
 
 
 
raicuandi
Guest
Posts: n/a
 
      07-28-2008
On Jul 29, 1:24*am, Fred Mangusta <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      07-29-2008
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.
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      07-29-2008
On Jul 28, 6:24 pm, Fred Mangusta <(E-Mail Removed)> 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:(E-Mail Removed)
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
 
Reply With Quote
 
Fred Mangusta
Guest
Posts: n/a
 
      07-29-2008
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is the result of valid dynamic cast always equal to the result ofcorrespondent static cast? Pavel C++ 7 09-18-2010 11:35 PM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 01:44 PM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 12:07 AM
Vector of abstract classes filled with derived classes ? Manuel C++ 8 01-05-2006 09:07 PM
Base Classes in .exe, derived classes in .dll Colin Goudie C++ 6 01-26-2004 03:18 PM



Advertisments