Re: [std::vector] operator[], the heap and try-carch blocks
From: Anthony Borla (ajborla_at_bigpond.com)
Date: 02/01/05
- Next message: Robert W Hand: "Re: [std::vector] operator[], the heap and try-carch blocks"
- Previous message: BobR: "Re: Resource editor and cygwin ?"
- In reply to: Val: "[std::vector] operator[], the heap and try-carch blocks"
- Next in thread: Robert W Hand: "Re: [std::vector] operator[], the heap and try-carch blocks"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Tue, 01 Feb 2005 01:34:20 GMT
"Val" <valmont_programming@hotmail.com> wrote in message
news:41feb2d1$0$44070$5fc3050@dreader2.news.tiscali.nl...
> 1) Suppose I do this...
> vector<int>* a = new vector<int>;
> ...then how do I acces operator[] ?
> This doesn't work anymore: a[0].
>
How about:
(*a)[0];
> 2) Is there a good reason to allocate a vector on the heap
> anyway?
>
Is there a reason to dynamically allocate [or not allocate] *any* item at
all ? It depends on what you wish to do with it.
>
> 2b) I don't have to delete vectors that are created dynamically
> this way right?
>
For every 'new' there must be a 'delete'; for ever 'new type[...]' there
must be a 'delete []'. I don't know of any exceptions to this guideline.
With containers you need to ensure:
* Elements are correctly destroyed. Often this is a
non-issue because this happens when the container
itself is destroyed. However, if the container contains
pointers these must be explicity 'delete'd
* Container is automatically destroyed at the end of
its scope, but if it was dynamically allocated, it must
be 'delete'd
>
> 3) Suppose I have two classes:
>
> class OtherClass
> {
> OtherClass() {}
> OtherClass(in a, int b)
> {
> if(a = = b)
> {
> throw AErrorObject("ERROR: numbers are equal");
> }
> }
> };
>
> class SomeClass
> {
> public:
> SomeClass : OC(new OC(2,2)
> {}
> private:
> OtherClass* OC;
> };
>
> I want to try-catch the exception in the intializer list already.
> How do I do that? Or is it impossible?
> Ive tried...
>
> SomeClass::SomeClass()
> try : OC(new OC(2,2)
> catch(AErrorObject& AEO) {
> cout<<AEO.what()<<endl;
> }
>
> ...like one finds on the internet but that doesn't work.
> What *the* way to do it? Uhm, AErrorObject is a just
> a simple error class with a constructor and the member
> what(). Nothing close to fancy.
>
This is an example of a function try block; they are a relatively new
facility, and not all compilers support them.
You should be able to get away without their use by setting data members to
valid values [for pointers, 0 / NULL] in initialiser lists, then perform any
complicated resource acquisition [like 'new'] in the constructor body. You
would then either:
[1] Catch any exceptions [e.g. bad_alloc] in the constructor
body, thereby allowing you to complete object construction
[an 'empty' or 'null, but valid, object would result]
[2] Or allow such exceptions to propagate upward [assuming a
suitable] handler exists, which may then terminate the program
or possibly retry the object creation process
The basic idea behind object construction is that it entirely succeeds, or
it entirely fails, and that where there is object construction failure, all
resources needed to construct data members / subobjects will have been
appropriately freed. If each object / data member correctly performs it's
own cleanup [in the constructor on failure, and in its destructor at end of
lifetime] then this is easy to achieve.
I hope this helps.
Anthony Borla
- Next message: Robert W Hand: "Re: [std::vector] operator[], the heap and try-carch blocks"
- Previous message: BobR: "Re: Resource editor and cygwin ?"
- In reply to: Val: "[std::vector] operator[], the heap and try-carch blocks"
- Next in thread: Robert W Hand: "Re: [std::vector] operator[], the heap and try-carch blocks"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|