Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How to access data or functions of the derived class if it was declared as the base class?

Reply
Thread Tools

How to access data or functions of the derived class if it was declared as the base class?

 
 
Titan
Guest
Posts: n/a
 
      06-30-2003
Hi all,

In Delphi the following code will work:
(xx[n-1]as TLibr).XEarth=xx[1].xm*(-1.0);
where xx is defined as a array of class TBody, and it has n members;
Tlibr is the derived class of Tbody, and Xearth is its private data.

How to write the corresponding code in c++ ?

Thanks,
 
Reply With Quote
 
 
 
 
tom_usenet
Guest
Posts: n/a
 
      06-30-2003
On 30 Jun 2003 02:05:37 -0700, http://www.velocityreviews.com/forums/(E-Mail Removed) (Titan) wrote:

>Hi all,
>
>In Delphi the following code will work:
>(xx[n-1]as TLibr).XEarth=xx[1].xm*(-1.0);
>where xx is defined as a array of class TBody, and it has n members;
>Tlibr is the derived class of Tbody, and Xearth is its private data.
>
>How to write the corresponding code in c++ ?


If Xearth is private, you can't access it. It it isn't private, then,
firstly you'll need an array of TBody* rather than TBody (an array of
TBody only contains TBody elements, and not elements derived from
TBody), and secondly the code should look something like this:

The set up code looks something like this:
TBody* xx[n]; //or TBody** xx = new TBody*[n];
xx[n-1] = new TLibr;


and here's the transcription of your code:
dynamic_cast<TLibr&>(*(xx[n-1])).XEarth = xx[1]->xm * -1.0;

It will throw an exception if xx[n-1] isn't an instance of TLibr.

Tom
 
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
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 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
Member struct declared in base, defined in derived class Ninereeds C++ 2 04-03-2007 03:41 AM
Access DataGrid declared in derived class from base class?? Jonas ASP .Net 1 08-12-2003 04:13 PM



Advertisments