C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Question related to creating and deleting of objects

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
vsnadagouda@gmail.com
Guest





PostPosted: Wed Aug 17, 2005 3:17 pm    Post subject: Question related to creating and deleting of objects Reply with quote



Hello All,

I am sort of lost while creating and deleting objects. Please take a
look at the small piece of code below:
============================================================================
#include <iostream>

using namespace std;

class Validator
{
public:
Validator ()
{
cout << "In Validator::Validator() " << endl;
}
void display ()
{
cout << "In Validator::display()" << endl;
}
virtual ~Validator ()
{
cout << "In Validator::~Validator() " << endl;
}
};

class Mars: public Validator
{
public:
Mars ()
{
cout << "In Mars::Mars() " << endl;
}
void display ()
{
cout << "In Mars::display()" << endl;
}
~Mars ()
{
cout << "In Mars::~Mars() " << endl;
}
};

class Safe
{
public:
Validator *ptr[2];
// Validator **ptr; // THIS DOESNT WORK
Safe ()
{
cout << "In Safe::Safe() " << endl;
}
void display()
{
cout << "In Safe::display() " << endl;
}
~Safe ()
{
cout << "In Safe::~Safe() " << endl;
}
};

int main ()
{
Safe *safe_ptr = new Safe();
safe_ptr->ptr[0]= new Mars();
safe_ptr->ptr[1]= new Mars();
delete safe_ptr->ptr[0];
delete safe_ptr->ptr[1];
//delete []safe_ptr->ptr; //THIS ALSO DOESNT WORK
delete safe_ptr;
}
============================================================================

I have Safe class, which will have pointers to Validator/Mars objects.
The number of objects of Validator/Mars is decided at the run time.

Above piece of code assumes that there will 2 objects of type
Mars/Validator (by explicitly mentioning Validator *ptr[2] ). But when
I try to make it general like "Validator **ptr" it doesn't work.

Also while deleting I used delete []safe_ptr->ptr to delete all the
objects created, but the destrucor is never called. ut when I call like
delete safe_ptr->ptr[0];
delete safe_ptr->ptr[1];
it works.

Can someone help in how to make this program generic.

Thanks in advance
-Vallabha


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Victor Bazarov
Guest





PostPosted: Wed Aug 17, 2005 5:19 pm    Post subject: Re: Question related to creating and deleting of objects Reply with quote



[email]vsnadagouda (AT) gmail (DOT) com[/email] wrote:

Quote:
Hello All,

I am sort of lost while creating and deleting objects. Please take a
look at the small piece of code below:
============================================================================
#include <iostream

using namespace std;

class Validator
{
public:
Validator ()
{
cout << "In Validator::Validator() " << endl;
}
void display ()
{
cout << "In Validator::display()" << endl;
}
virtual ~Validator ()
{
cout << "In Validator::~Validator() " << endl;
}
};

class Mars: public Validator
{
public:
Mars ()
{
cout << "In Mars::Mars() " << endl;
}
void display ()
{
cout << "In Mars::display()" << endl;
}
~Mars ()
{
cout << "In Mars::~Mars() " << endl;
}
};

class Safe
{
public:
Validator *ptr[2];

These data should be private and it should be 'Safe's responsibility
to maintain (create/destroy) them.

Quote:
// Validator **ptr; // THIS DOESNT WORK
Safe ()
{
cout << "In Safe::Safe() " << endl;
}
void display()
{
cout << "In Safe::display() " << endl;
}
~Safe ()
{
cout << "In Safe::~Safe() " << endl;
}
};

int main ()
{
Safe *safe_ptr = new Safe();
safe_ptr->ptr[0]= new Mars();
safe_ptr->ptr[1]= new Mars();
delete safe_ptr->ptr[0];
delete safe_ptr->ptr[1];
//delete []safe_ptr->ptr; //THIS ALSO DOESNT WORK
delete safe_ptr;
}
============================================================================

I have Safe class, which will have pointers to Validator/Mars objects.
The number of objects of Validator/Mars is decided at the run time.

..... but the maximum is decided at compile time: 2.

Quote:
Above piece of code assumes that there will 2 objects of type
Mars/Validator (by explicitly mentioning Validator *ptr[2] ). But when
I try to make it general like "Validator **ptr" it doesn't work.

What do you mean by "doesn't work"?

Quote:
Also while deleting I used delete []safe_ptr->ptr to delete all the
objects created, but the destrucor is never called.

Since you didn't create the array by using 'new []', you can't expect
'delete[]' to work. It's just undefined behaviour to delete a pointer
that isn't obtained from 'new'.

Quote:
ut when I call like
delete safe_ptr->ptr[0];
delete safe_ptr->ptr[1];
it works.

Of course it does. You allocate each object using 'new' and then you
deallocate it using 'delete'. That's how it's supposed to work.

What book are you reading that doesn't explain how 'new' and 'delete'
work with each other?

V

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
alexey.br@gmail.com
Guest





PostPosted: Wed Aug 17, 2005 11:08 pm    Post subject: Re: Question related to creating and deleting of objects Reply with quote



class Safe
{
public:
Validator **ptr; // NOW THIS DOES WORK

.......
int main()
{
Safe *safe_ptr = new Safe();
typedef Validator* PValidator;
safe_ptr->ptr = new PValidator[2]; // ! allocate memory for array of
pointers or simply use std::vector<PValidator> or even with smart
pointers.
safe_ptr->ptr[0]= new Mars();
safe_ptr->ptr[1]= new Mars();
delete safe_ptr->ptr[0];
delete safe_ptr->ptr[1];
delete[] safe_ptr->ptr; // now u need to delete this
array
delete safe_ptr;
return 0;
}


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
apolly
Guest





PostPosted: Wed Aug 17, 2005 11:10 pm    Post subject: Re: Question related to creating and deleting of objects Reply with quote

As you create objects by new not new[], you must destroy them by delete
but not delete[]. It is also important to remember : NEVER treat arrays
polymorphically. Check More Effective C++ Item3 to find out the detail.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
mlimber
Guest





PostPosted: Wed Aug 17, 2005 11:11 pm    Post subject: Re: Question related to creating and deleting of objects Reply with quote

[email]vsnadagouda (AT) gmail (DOT) com[/email] wrote:
[snip]
Quote:
I have Safe class, which will have pointers to Validator/Mars objects.
The number of objects of Validator/Mars is decided at the run time.
[snip]


See this FAQ and the ones that follow:

http://www.parashift.com/c++-faq-lite/freestore-mgmt.html#faq-16.11

Cheers! --M


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.