473,407 Members | 2,314 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,407 software developers and data experts.

pointers for derived and base class

hey all i have problem here
suppose i made class base and i derived drv that has function set data
in the base class
when i want to have array of pointers for both of them
base* ba[10];
drv* dr[10];
then i want to do this
for(int i=0;i<10;i++)
*(ba+i)=new base;
for(i=0;i<10;i++)
*(dr+i)=new drv //it will compile but in excuting the program it say
unhandled exception and terminate the program
can someone tell me why ?

Jul 23 '05 #1
4 1447
Hisham wrote:
hey all i have problem here
suppose i made class base and i derived drv that has function set data
in the base class
You should consider writing C++ instead of English in a situation
like this. (a) It is less open to interpretation and (b) many of us
here have English as a second language and C++ as our first which
makes C++ much easier to understand than English. Let me illustrate.
What I understood from your second sentence (and do put . at the end
of the sentence please) is

class base { public: void set_data(); };
class drv : public base {};
when i want to have array of pointers for both of them
base* ba[10];
drv* dr[10];
then i want to do this
for(int i=0;i<10;i++)
*(ba+i)=new base;
for(i=0;i<10;i++)
*(dr+i)=new drv //it will compile but in excuting the program it say
unhandled exception and terminate the program
can someone tell me why ?


The code as posted can't be compiled. Please follow the instructions
in the FAQ 5.8 and make another attempt at stating your problem. As
in many other's beginners' posts, you omit some essential parts that
actually [most likely] cause the run-time error you're asking about.

V
Jul 23 '05 #2
Hisham wrote:
hey all i have problem here
suppose i made class base and i derived drv that has function set data
in the base class
when i want to have array of pointers for both of them
base* ba[10];
drv* dr[10];
then i want to do this
for(int i=0;i<10;i++)
*(ba+i)=new base;
More readable would be:

ba[i] = new base;
for(i=0;i<10;i++)
*(dr+i)=new drv //it will compile but in excuting the program it say
unhandled exception and terminate the program
can someone tell me why ?


Please try to make a minimal but complete (i.e. compilable as is) program
that still shows the behavior and then post it here.

Jul 23 '05 #3

"Hisham" <ig*******@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
hey all i have problem here
suppose i made class base and i derived drv that has function set data
in the base class
when i want to have array of pointers for both of them
base* ba[10];
drv* dr[10];
then i want to do this
for(int i=0;i<10;i++)
*(ba+i)=new base;
ba[i] = new base;
for(i=0;i<10;i++)
*(dr+i)=new drv //it will compile but in excuting the program it say
dr[i] = new drv;
unhandled exception and terminate the program
can someone tell me why ?


Be carefull how you delete the array elements, you can't do:
delete [] ba;
delete [] dr;
Since the array is an array of pointers, not objects.

Why not just write:
base* ba[20];
and provide a virtual destructor for your base class? Better yet, use a
std::vector.

Jul 23 '05 #4
Peter Julian wrote:
"Hisham" <ig*******@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
hey all i have problem here
suppose i made class base and i derived drv that has function set data
in the base class
when i want to have array of pointers for both of them
base* ba[10];
drv* dr[10];
then i want to do this
for(int i=0;i<10;i++)
*(ba+i)=new base;

ba[i] = new base;

for(i=0;i<10;i++)
*(dr+i)=new drv //it will compile but in excuting the program it say

dr[i] = new drv;

unhandled exception and terminate the program
can someone tell me why ?

Be carefull how you delete the array elements, you can't do:
delete [] ba;
delete [] dr;
Since the array is an array of pointers, not objects.


The arrays are automatic, they don't need to be deleted. Each object
does, however, need to be deleted, in a loop.
Why not just write:
base* ba[20];
and provide a virtual destructor for your base class? Better yet, use a
std::vector.


Without knowing how the arrays are used it's impossible to suggest
anything, really.

V
Jul 23 '05 #5

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

Similar topics

11
by: Josh Lessard | last post by:
Hi all. I'm maintaining a C++ program and I've come across a nasty piece of code that works, but I just don't understand why. I'm not actually this part of the program, but I really want to know...
7
by: Katie | last post by:
my one base class is a linklist class.now i'd like to inherit it by another class.The problem is that when i use the derived class i can't access the base class's pointers.classic type mismatch.how...
2
by: Dmitri Islentyev | last post by:
Hello everyone! Please consider the following code: class Base { public: Base(int x); virtual int foo() { return 17; } };
1
by: TheOne | last post by:
I have two classes: class OntologyParser { ... protected: virtual void startElement(void *userData, const char *name, const char **atts); virtual void endElement(void *userData, const char...
12
by: ex_ottoyuhr | last post by:
I have a situation more or less as follows, and it's causing me no end of trouble; I'd appreciate anyone's advice on the matter... Given these classes: class BedrockCitizen { ... }; class...
6
by: ivan.leben | last post by:
I want to write a Mesh class using half-edges. This class uses three other classes: Vertex, HalfEdge and Face. These classes should be linked properly in the process of building up the mesh by...
7
by: Steven T. Hatton | last post by:
I have a couple questions about the design pattern presented in the example quoted below. I can appreciate why the destructor is protected, but why is it not virtual? I am forced to assume that I...
3
by: mati-006 | last post by:
Hi, I think the code will be the best way to explain what I mean: #include "arglib/arg_shared.h" class base { public: base() {} virtual ~base() {} };
12
by: bgold | last post by:
Hey. I have a base class (SPRITE), and using this base class I have derived a large number of derived classes (PERSON, BULLET, MISSILE, etc.). Now, at a certain point in my program, I have a pair...
1
by: Rune Allnor | last post by:
Hi all. I am sure this is an oldie, but I can't find a useful suggestion on how to solve it. I have a class hierarchy of classes derived from a base class. I would like to set up a vector of...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
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,...
0
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...
0
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...
0
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,...

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.