Question on dynamic_cast??

Hi evereyone,

I have a txt file with different shapes, I read the file and I store the different shapes on an array of a base class CBase* bptr[60]

After I have stored the shapes in the array, I have ask the user which one they like to see through a menu selection.

After the selection I have to display this using dynami_cast at run time.

I have no idea how to implement this in code, the array part confuses me.

any hints or ideas on how to aproach this would be apreciated

thanks for the time and help everyone

here is how my main looks:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// I have my classes , base is abstarct
// graphic lite functions as well
// prototypes

#include<iostream>
#include<fstream>
#include<typeinfo> // for dynamic_cast

int main()
{
  char ch;
  int n;
  CBase* bptr[60];
  
  disMenu(); // displays the menu for user selection
  n = addShapes(p, 60);// this is a function int addShapes(CBase**, int)

  //  I know how to display all of the shapes
  for(int i=0; i<= n ; i++)
  {
    bptr[i]-> // function desired, where polymorphism displays each shape

  }   

  // but I do not know how to display each individual shape
  // depending on the menu selection from user 
}


Please help me on the right direction, thanks again
someone?
You don't need dynamic_cast. If there is a common virtual function in the base class, just call that function and the appropriate derived class function will be called. No need to do any casting.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class CBase
{
public:
  virtual void DesiredFunction() = 0;
};

class CDerived : public CBase
{
public:
  virtual void DesiredFunction()
  {
    // do stuff here
  }
};

int main()
{
  CBase* bptr[60];
  //...

  bptr[i]->DesiredFunction(); // no need to cast, the "virtual" does everything automagically
}


Also, give us some time to respond. 1 hour isn't really that long for this forum (or most forums, for that matter).
Last edited on
I need to use that method, using dynamic_cast or the typeid.

But for some reason I like dynami_cast better

thanks for the reply, I'll try to be more patient next time
er, why do you need dynamic_cast or typeid? Is that a requirement for an assignment or something? If not, it really doesn't apply here, and should be avoided. Just make the function virtual and the problem is solved.

If you really must use dynamic_cast you can do it like this:

1
2
3
4
5
CDerived* derived = dynamic_cast<CDerived*>(bptr[i]);
if(derived)  // was the cast successful?
{
  derived->DesiredFunction();
}


But again note this is a horrible idea and is inferior to calling a virtual function in just about every way so I really don't advise it.
Last edited on
depending on the menu selection from user,

i have to display the object(shape) at run time,

so I am assuming I need the methods used above
Disch I tried your method and the compiler crashes

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include<iostream>
#include<fstream>
#include<typeinfo> // for dynamic_cast

int main()
{
  char ch;
  int n;
  CBase* bptr[60];
  
  disMenu(); // displays the menu for user selection
  n = addShapes(p, 60);// this is a function int addShapes(CBase**, int)

  //  I know how to display all of the shapes
  for(int i=0; i<= n ; i++)
  {
    bptr[i]-> desiredFunction(); 
  }   

// this is the adddition suggested by Disch
  CDerived* derived = dynamic_cast<CDerived*>(bptr[i]);
  if(derived)  // was the cast successful?
 {
   derived->DesiredFunction();
 }

 }

   // delete the bptr[i] 



I am not sure where the code crashes, but it has to be at dynamic_cast part since this was working properly earlier.

Any ide what might cause this crash??
Last edited on
Dude, if Cderived is not a real class you have included you can't just call it. That is like typing x = 3; when you never actually declared x.
Dynamic casts are something that you going to have to use if you have a hierarchy that is not completely virtual. The idea is that if you are a good and friendly programmer you will create an handle class that will do the dynamic casting required to access the entire hierarchy so that the client doesn't have to worry about it.

Dynamic casting is mainly used for two reasons. It can serve as a way to figure out the type being pointed to or it serve as a way to call a non-virtual method in a derived class from a base-class pointer.

For instance, if my derived class D had a method Set( ) but it was not virtual and I wanted to call it from the base class pointer B.

Well since the Set( ) is not virtual if I try to call it from the base class pointer B I would get a compile time bind. This means the the compiler will look for Set( ) in the base class only because that it the data type I am calling the method from.

So I would dynamically cast the pointer of base class D to a pointer of derived class D by using the syntax:

1
2
3
B* b_ptr;

D* d_ptr = dynamic_cast<D*> (b_ptr);


The cast will set d_ptr to NULL if it can't cast it.
Last edited on
IceThatJaw, thanks for the reply

CDerived is a class, and I am trying to solve your first reason where it figures out the where it points too

and I need to access it an display it
Well, the first thing you need to know is if this method is virtual or not.

If it is virtual and you are trying to access it from a base class pointer than just call it (check if the pointer is NULL first so u don't seg fault)

If the method is not virtual than you MUST call it from a pointer of that class's data type. You will do this by dynamically casting the pointer to a pointer of that class's data type.

If this still doesn't make sense, go and learn the difference between compile-time binding and run-time binding. Fully understand the difference and you will be good, I promise.

Good luck.
Last edited on
EDIT: removed my original rant. Now that I reread your post it appears you don't need me to re-explain


As for crashing, I doubt your compiler is crashing. Did you mean your program is crashing? Or you're getting a compiler error? If it's an error, what line is it on?

If your program is crashing, it's probably because you have a bad pointer. What does your addShapes function look like?
Last edited on
I have to remove this , sorry
Last edited on
If I change the ptr[i] to 1(1 happenes to match the shape) or a different # it runs without any problems


Er, well, yeah. i has to be in range. I assumed that was a given. If i is out of range of the array you'll be dereferencing a bad pointer and a crash is very likely.

EDIT: also that's not the whole function =P. Why are you decreasing i in that loop? should it be increasing?

But yeah my original point stands. There's no need for dynamic_cast here. Just make the desired function(s) virtual in the base class.
Last edited on
Thanks everyone for the help,

I figured it out, using the dynamic cast in the if statment.

It makes sense only too use virtual function Disch, but my instructions are to use the
1
2
dynamic_cast or 
typeid
. to acces the array and get the right shape

Thanks again for the time and help.
Topic archived. No new replies allowed.