Re: [std::vector] operator[], the heap and try-carch blocks

From: Anthony Borla (ajborla_at_bigpond.com)
Date: 02/01/05


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



Relevant Pages

  • Re: pointers to elements in containers
    ... The 'key_fields' container is meant to store pointers to the ... so Detail's constructor looks like the ... pointers to elements in 'fields' point to valid ... Is your Detail struct being copied? ...
    (microsoft.public.vc.language)
  • Re: default values - the good, the bad and the ugly
    ... > The thing is that in your tale, they didn't change only a constructor ... In the specific case the someClass is a container of ... geometric curves, 'd' is the c0 continuous tolerance between ... the capacity reserved would be double the last size. ...
    (comp.lang.cpp)
  • Re: Constructor protected
    ... there is nothing to initialize. ... don't write an own constructor, the compiler will generate a default ... > know nothing about their base classes' data members. ...
    (comp.lang.cpp)
  • Re: Use the accessor! Was: Copy Constructor Usage
    ... The purpose of a copy constructor is ... And copying the data members is usually what achieves that. ... If you use accessor functions in a copy c'tor you run the ... >> what useful purpose I can't imagine. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: memcpy and STL vector
    ... what it calls POD types. ... The copy constructor is called in a copy by value parameter passing. ... What's the default behavior for assignment then if assignment is not ... then each of the data members in turn. ...
    (comp.lang.cpp)