RUN TIME ERROR

P

Pranav

#include <iostream>
#include <conio.h>
using namespace std;

class Base{
public :
Base(){ ib = 123; cb = 'B'; }
int ib;
char cb;
virtual void print(){ cout << "Base Class\n" ;}
~Base(){ }
};

class Der : public Base{
public :
Der():Base(){ id = 456; cd = 'D'; }
int id;
char cd;
void print(){ cout << "Deerived Class\n"; }
~Der(){ }
};

main()
{
Der d1, *dp = new Der;
Base b1, *bp;

Base *bp2 = dynamic_cast<Base *> (dp) ;

bp2->print( );

// Base *bp3 = dynamic_cast<Base *> (bp2) ;
// Der *dp3 = dynamic_cast<Der *> (bp2) ;



getche();
return 0;
}

COMMENTED LINES are generating RUN TIME ERROR
 
P

peter koch

#include <iostream>
#include <conio.h>
using namespace std;

class Base{
public :
        Base(){ ib = 123; cb = 'B'; }
        int ib;
        char cb;
        virtual void print(){ cout << "Base Class\n" ;}
        ~Base(){ }

};

class Der : public Base{
public :
        Der():Base(){ id = 456; cd = 'D'; }
        int id;
        char cd;
        void print(){ cout << "Deerived Class\n"; }
        ~Der(){ }

};

main()
{
        Der d1, *dp = new Der;
        Base b1, *bp;

        Base *bp2 = dynamic_cast<Base *> (dp) ;

        bp2->print( );

//    Base *bp3 = dynamic_cast<Base *> (bp2) ;
//      Der *dp3 = dynamic_cast<Der *> (bp2) ;

getche();
return 0;

}

COMMENTED LINES are generating RUN TIME ERROR

I see no errors in your code, so you need to be more specific. What
happens when uncommenting the lines? Is it a runtime error from the
compiler or an error when you try to run the program?

Also your code could improve a lot - e.g. most of the dynamic casts
are not needed, but I am not going to comment further on that.

/Peter
 
P

Pranav

I see no errors in your code, so you need to be more specific. What
happens when uncommenting the lines? Is it a runtime error from the
compiler or an error when you try to run the program?

Also your code could improve a lot - e.g. most of the dynamic casts
are not needed, but I am not going to comment further on that.

/Peter



Base *bp3 = dynamic_cast<Base *> (bp2) ;
Der *dp3 = dynamic_cast<Der *> (bp2) ;

These two lines are giving error in above code.., When you uncomment
them will give you run time error,
 
J

James Kanze

#include <iostream>
#include <conio.h>
using namespace std;

class Base{
public :
Base(){ ib = 123; cb = 'B'; }
int ib;
char cb;
virtual void print(){ cout << "Base Class\n" ;}
~Base(){ }

};

class Der : public Base{
public :
Der():Base(){ id = 456; cd = 'D'; }
int id;
char cd;
void print(){ cout << "Deerived Class\n"; }
~Der(){ }

};

main()
{
Der d1, *dp = new Der;
Base b1, *bp;

Base *bp2 = dynamic_cast<Base *> (dp) ;

bp2->print( );

// Base *bp3 = dynamic_cast<Base *> (bp2) ;
// Der *dp3 = dynamic_cast<Der *> (bp2) ;

getche();
return 0;

}
COMMENTED LINES are generating RUN TIME ERROR

With what compiler? Once I corrected the obvious errors in the
code (inexistant include file, no return type for main, no such
function getche), it worked for me. With three different
compilers. (I suppose that there could be something in
<conio.h> which causes dynamic_cast not to work, but I'd be
surprised.)

Note that the first dynamic_cast is actually a static_cast (and
in fact an implicit conversion), and the second is a no-op.
 
P

Pranav

With what compiler? Once I corrected the obvious errors in the
code (inexistant include file, no return type for main, no such
function getche), it worked for me. With three different
compilers. (I suppose that there could be something in
<conio.h> which causes dynamic_cast not to work, but I'd be
surprised.)

Note that the first dynamic_cast is actually a static_cast (and
in fact an implicit conversion), and the second is a no-op.

--
James Kanze (GABI Software) email:[email protected]
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

MS Visual Studio 6.0..,
 
N

Nick Keighley

MS Visual Studio 6.0

I don't bother with that modern stuff... :)

This code:

//*****************************

#include <iostream>

using namespace std;

class Base{
public :
Base(){ ib = 123; cb = 'B'; }
int ib;
char cb;
virtual void print(){ cout << "Base Class\n";}
~Base(){ }
};

class Der : public Base{
public :
Der():Base(){ id = 456; cd = 'D'; }
int id;
char cd;
void print(){ cout << "Deerived Class\n"; }
~Der(){ }
};

int main()
{
Der d1, *dp = new Der;
Base b1, *bp;

Base *bp2 = dynamic_cast<Base *> (dp);

bp2->print( );

Base *bp3 = dynamic_cast<Base *> (bp2);
Der *dp3 = dynamic_cast<Der *> (bp2);

return 0;
}
//*****************************


compiled with one warning on Visual C++ 5.0
(that's *even* crapper than your compiler)
warning C4101: 'bp' : unreferenced local variable


It gave no runtime errors and produced this output:
Deerived Class


I enabled RTTI and disabled pre-compiled headers in VCC
(it won't compile without RTTI)

I suggest you are not posting your actual code
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top