473,386 Members | 1,668 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

dynamic_cast problem

Hi,

I have below piece of code in which i am dynamically casting a base type to derived type (which is allowed in case base class is polymorphic).
even then i m getting error while dynamic casting.

Please Help.

Thanks,
Harish

#include <iostream>

using namespace std;

//----------------------- class Idatatype ------------------------//
class IDatatype
{
public:
IDatatype(){ cout <<"constructor IDatatype"<<endl; i = 100 ; };
IDatatype( const IDatatype& right){ cout<<"copy constructor IDatatype"<<endl;i = right.i; };
virtual ~IDatatype(){};
virtual IDatatype& copyData(const IDatatype& right) = 0;

int getData() {return i ;}
void setData(int x){ i = x;};
void setData(IDatatype& right){ i = right.getData(); };
private:
int i ;
};

//----------------------- class FlatChargeInfo ------------------------//
class FlatChargeInfo : public IDatatype
{
public:
FlatChargeInfo() {cout<<"constructor FlatChargeInfo"<<endl;};
FlatChargeInfo(const FlatChargeInfo& other) {cout<<"copy constructor FlatChargeInfo"<<endl;
m_oCustomDatatype = other.m_oCustomDatatype; };
virtual ~FlatChargeInfo(){};
IDatatype* getData() {return m_oCustomDatatype; }
virtual IDatatype& copyData(const IDatatype& right){return *m_oCustomDatatype; };
private:
IDatatype* m_oCustomDatatype;
};

//----------------------- class FlatChargeInfoProxy ------------------------//
class FlatChargeInfoProxy : public IDatatype
{
public:
FlatChargeInfoProxy(){ cout<<"constructor FlatChargeInfoProxy"<<endl;
m_pFlatChargeInfo = new FlatChargeInfo();}
FlatChargeInfoProxy(const FlatChargeInfoProxy& other)
: IDatatype( other ) , m_pFlatChargeInfo( other.m_pFlatChargeInfo ){
m_oCustomDatatype = other.m_oCustomDatatype ;
cout<<"copy constructor FlatChargeInfoProxy"<<endl;
};
virtual ~FlatChargeInfoProxy(){};
//virtual void setCode( int newCode){ m_pFlatChargeInfo->setCode(newCode);};
IDatatype* getData(){return (m_oCustomDatatype);};
//void setData(IDatatype* iDataType){m_oCustomDatatype=iDataType;};
FlatChargeInfo* getChargeRecord(){return (m_pFlatChargeInfo);};
virtual IDatatype& copyData(const IDatatype& right);
private:
IDatatype* m_oCustomDatatype;
protected:
FlatChargeInfo * m_pFlatChargeInfo;
};

//## Assignment operator. Copyes data from argument to current datatype
IDatatype& FlatChargeInfoProxy::copyData(const IDatatype& right)
{
const FlatChargeInfoProxy* ptr = (dynamic_cast<const FlatChargeInfoProxy*>(&right));
if( ptr ) {
m_pFlatChargeInfo = ptr->m_pFlatChargeInfo;
// Copy data of customization object
// if (m_oCustomDatatype) {
//m_oCustomDatatype->copyData(*(ptr->m_oCustomDatatype));
// }
}
return *this;
};

//-------------------------------- MAIN -----------------------------------//
main(){
FlatChargeInfoProxy fp,fp2;
FlatChargeInfo f;
fp.copyData(f);
}
May 14 '07 #1
1 964
I got the solution. it was because i did not enabled RTTI in VS.

thanks,
harish
May 14 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

13
by: GianGuz | last post by:
Everyone knows about the complex and cpu-expensive procedures taken by dynamic_cast to find the right function call in a virtual classes hierarchy. The question I would to rise is when...
8
by: Thomas Lorenz | last post by:
Hello, first, I didn't find any reference to my question through googling. dynamic_cast uses RTTI to determine if the cast expression is valid. Invalid casts of pointers give a '0'-result. As...
2
by: yccheok | last post by:
Hello, I have an object called XXX previously derived from CDocument in my MDI project. Later, I create an concrete class called Subject. And I let XXX to have multiple inheritance from Subject...
5
by: tthunder | last post by:
Hi @all, Perhaps some of you know my problem, but I had to start a new thread. The old one started to become very very confusing. Here clean code (which compiles well with my BCB 6.0 compiler)....
22
by: Boris | last post by:
I'm porting code from Windows to UNIX and ran into a problem with dynamic_cast. Imagine a class hierarchy with three levels: class Level2 derives from Level1 which derives from Base. If you look...
5
by: mijobee | last post by:
Hello Everyone, I just wanted to check that I'm using dynamic_cast correctly. I have a hierarchy of objects using pure virtual classes and virtual inheritance to implement interfaces. I ran...
15
by: Grizlyk | last post by:
Hello. Returning to question of manual class type identification, tell me, for ordinary inheritance is C++ garantee that dynamic_cast<Derived*>(Base*) can be implemented similarly to ...
15
by: Bo Yang | last post by:
Hi, I can understand static_cast, reinterpret_cast and const_cast, they all work at compile time. But I can figure out how the C++'s dynamic- cast works? Could you please explain how for me?...
9
by: Rob | last post by:
(I am not the one who defined these classes) class _jobject {}; class _jarray : public _jobject {}; typedef _jobject* jobject; typedef _jarray jarray; int main() {
18
by: Eric | last post by:
Ok...this seems to be treading into some really esoteric areas of c++. I will do my best to explain this issue, but I don't fully understand what is happening myself. I am hoping that the problem...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.