473,790 Members | 2,850 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Hi all,

In Delphi the following code will work:
(xx[n-1]as TLibr).XEarth=x x[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,
Jul 19 '05 #1
1 1742
On 30 Jun 2003 02:05:37 -0700, am****@sohu.com (Titan) wrote:
Hi all,

In Delphi the following code will work:
(xx[n-1]as TLibr).XEarth=x x[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<TL ibr&>(*(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
Jul 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
10476
by: Kevin Saff | last post by:
Apparently I'm missing something. Stroustrup (15.3) says of protected access: If is protected, its name can be used only by member functions and friends of the class in which it is declared and by member functions and friends of classes derived from this class. Since private access cares about the calling class rather than the calling object, I assumed the same was true for protected access, but the following code fails in MSVC6:
2
1957
by: Michael Young | last post by:
I've been trying to compile code similar to the example code below, but I keep getting errors indicating that the function 'foo()' is not accessible. At first, I thought this was a bug in the compiler, but I've now tried the code against Borland C++ Builder 6.0, MS VC++ .NET, DevC++ (GCC), and Comeau's on-line compiler, and all give me similar diagnositic messages. I can make the code work if I make the "protected" items "public" in...
9
4996
by: Banaticus Bart | last post by:
I wrote an abstract base class from which I've derived a few other classes. I'd like to create a base class array where each element is an instance of a derived object. I can create a base class pointer which points to an instance of a derived class, but when I pass that base class pointer into a function, it can't access the derived object's public functions. Although, the base class pointer does call the appropriate virtual function...
3
3773
by: Daniel Graifer | last post by:
Why doesn't c++ support virtual data? It supports class data of type pointer to function (that's what virtual functions really are). In terms of implementation, why can't I have other types of data in my vtable? RTTI could be implemented this way instead of as a special case for the compiler. It would also be useful for class specific memory management implementations. -- Daniel A. Graifer
28
3429
by: Act | last post by:
Why is it suggested to not define data members as "protected"? Thanks for help!
5
1469
by: Daniel Aarno | last post by:
Can anyone provide a good explanation to why the following does not work? After all since A is a public base of B objects of type B ISA object of type A. class A { protected: void foo() {} }; class B : public A {
3
1907
by: Samuel Burri | last post by:
Hi there I got a simple problem. As you can see in the posted source, I have two classes, where one is derived from the other. In fact, they also contain virtual functions. In the function doSomething of Derived I'd like to access private members of Base from the pointer I receive. The compiler (gcc 4.0) tells me, that Base::foo is protected and it can't access although Derived itself has access to the specified element. Probably its...
3
1511
by: Mark Foley | last post by:
I am implementing some functionality where there is a single base class 'A' which provides some common functionality There are then two classes derived from 'A', call them 'B' and 'C'. Both 'B' and 'C' define functions specific only to their class. Somethng like... class A {
3
1670
by: jewel87 | last post by:
Hi everyone, i have problems with inheritance implementation. I have a base class Account and 3 derived classes from it: CurrentAccount, SavingAccount and ISAAccount. The Account class is connected with the Employee class and i'm trying to use the derived class functions in the Employee class. In the Employee i have declared a pointer Account **theAccounts; And here is the rest code.
10
4120
by: blangela | last post by:
If I pass a base class object by reference (likely does not make a difference here that it is passed by reference) as a parameter to a derived class member function, the member function is not allowed to access the protected data members of the base object. This surprises me. Can someone explain why this is? I suspect there is a good reason and I am just having a slow day to not come up with it myself. Bob
0
9666
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10200
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10145
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9986
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9021
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6769
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3707
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2909
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.