Scope of an Object


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 10 of 10

Thread: Scope of an Object

  1. #1
    Join Date
    Oct 2004
    Posts
    11

    Scope of an Object

    Hi,

    I have an array of pointers to an object type:

    Object* arr[5];

    To assign each Object ptr in the array, I decided not to create them in
    the free store, but put them in the stack as I know the scope and thus didn't want to call new/delete without a need.

    func( )
    {
    Object* arr[5];
    arr[0] = &Object(); //ln.2
    //..
    arr[4] = NULL;
    func2(arr); //pass this array of pointers
    }

    I was surprised to note that the dtor of the Object is called immediately after line no. 2. I can see that I didn't create a named object. Nevertheless, I wasn't thinking of a temporary object as I was storing its pointer.

    Why can't the compiler consider this equivalent to a named object (somehow) and thus call the dtor similar to a named object i.e, retain the scope of the object till the exit of this func().

    Thanks,
    Sivsu.

  2. #2
    Join Date
    Dec 2003
    Posts
    3,366
    Too much added compiler complexity. The compiler is actually a fairly simple parser, and getting it to 'understand' your intent would slow it down and be undesirable for several reasons. If you are really interested, do a search on compiler and language/grammer theory -- there is likely a site that explains why you don't want to do some things.

  3. #3
    Join Date
    Nov 2003
    Posts
    4,118
    Code:
    arr[0] = &Object(); //ln.2
    This expression creates a temp object of type Object, takes its address and assigns it to arr[0]. Temps are destroyed immediately after the evluation of their expression, which is typically where the ; is seen. Thus, you end up creating a dangling pointer. The compiler can't guess, as jonnin said, that you want a non temp object. If you want such an object, simply create it as a normal automatic object:
    Code:
    void func()
    {
     Object obj;
     arr[0] & obj;
    }
    Better yet, create an array of the objects on the stack:
    Code:
    Object arr[5];
    Danny Kalev

  4. #4
    Join Date
    Oct 2004
    Posts
    11
    Hi Dany, Jonin,

    I initially had in mind something like this:

    Object* arr[5];

    for( int i =0; i < 4; i++ )
    arr[i] = &Object();
    arr[4] = NULL;
    func(arr);

    instead of separately creating one named object for each element of the array.

    Thanks for your ideas.

    Sivsu.

  5. #5
    Join Date
    Dec 2003
    Posts
    3,366
    you should be able to make an array of objects and take pointers to those in a loop, if that helps.

  6. #6
    Join Date
    Nov 2003
    Posts
    4,118
    There is an ugly but safe workaround: use two arrays:
    Code:
    Object* arr[5]= {0}; //initialize all pointers to NULL
    Object alias[4];
    
    for( int i =0; i < 4; i++ )
    arr[i] = &alias[i]
    //arr[4] = NULL; not needed anymore
    func(arr);
    Danny Kalev

  7. #7
    Join Date
    Dec 2003
    Posts
    3,366
    Thats what I said ;)

    Why is it ugly? If its what you want to do, for whatever reason, it looks clean enough to me?

  8. #8
    Join Date
    Nov 2003
    Posts
    4,118
    It's ugly because there's duplication of variables: an array of pointers and an array of objects that are coupled. I would be happier to use a single and cleaner interface, say a vector or a simple array and use the & operator to access its members. I might even use dynamically allocated objects, although it's a pain of a different kind...
    Danny Kalev

  9. #9
    Join Date
    Nov 2003
    Posts
    4,118
    BTW, there IS a way to accomplish this but you will have to give up the array.
    Standard C++ states that when you bind a reference-to-const to a temp, that temp remains alive throughout the lifetime of the *reference*. In other words:
    Code:
    void f()
    {
    const Object & ref=Object();
    func (&ref);
    ref.doSomething(); //use ref to temp object
    } //bound temp is destroyed here
    Remember: the reference must be const. Some outdated compilers will allow you to bind non-const reference to temps but the results in this case are compiler-specific.
    Last edited by Danny; 03-27-2005 at 02:57 PM.
    Danny Kalev

  10. #10
    Join Date
    Oct 2004
    Posts
    11
    This is great !

    Thanks,
    Sivsu.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center