Smart resource manager

Discussion in 'C++' started by Alan Johnson, May 22, 2004.

  1. Alan Johnson

    Alan Johnson Guest

    I am trying to create an automatic resource manager that works very much
    the same as std::auto_ptr. However, I have a compile error I cannot
    figure out. The following program demonstrates the problem. Pretend,
    in each case, that -1 means "no resource", and any other number is a
    handle to a valid resource. The class releases the resource in the
    destructor (similar to a std::auto_ptr calling delete), so any time a
    copy of the class is made, ownership of the resource should be transferred.

    class A
    {
    int s ;
    public :
    A() : s(-1)
    { }

    A(int s) : s(s)
    { }

    A(A &a) : s(a.s)
    {
    a.s = -1 ;
    }

    ~A() throw()
    {
    // Do something to release resource.
    s = -1 ;
    }

    A &operator=(A &a) throw()
    {
    s = a.s ;
    a.s = -1 ;
    return *this ;
    }
    } ;

    A f()
    {
    A a(5) ;
    return a ;
    }

    int main()
    {
    A a ;
    a = f() ;
    return 0 ;
    }


    Compling gives the following problem:
    $ g++ -W -Wall -ansi -pedantic test.cpp
    test.cpp: In function `int main()':
    test.cpp:41: error: no match for 'operator=' in 'a = f()()'
    test.cpp:25: error: candidates are: A& A::eek:perator=(A&)

    That is, the assignment fails, for some reason I cannot determine. Any
    insight would be appreciated.

    Thanks,
    Alan
    Alan Johnson, May 22, 2004
    #1
    1. Advertising

  2. Alan Johnson

    Chris Guest

    > A(int s) : s(s)

    dont use "s" as your paramter name, use something other then the name of
    your data member
    likely, this is your problem.

    -Chris
    Chris, May 22, 2004
    #2
    1. Advertising

  3. Alan Johnson

    Rolf Magnus Guest

    Alan Johnson wrote:

    > I am trying to create an automatic resource manager that works very
    > much
    > the same as std::auto_ptr. However, I have a compile error I cannot
    > figure out. The following program demonstrates the problem. Pretend,
    > in each case, that -1 means "no resource", and any other number is a
    > handle to a valid resource. The class releases the resource in the
    > destructor (similar to a std::auto_ptr calling delete), so any time a
    > copy of the class is made, ownership of the resource should be
    > transferred.
    >
    > class A
    > {
    > int s ;
    > public :
    > A() : s(-1)
    > { }
    >
    > A(int s) : s(s)
    > { }
    >
    > A(A &a) : s(a.s)
    > {
    > a.s = -1 ;
    > }
    >
    > ~A() throw()
    > {
    > // Do something to release resource.
    > s = -1 ;
    > }
    >
    > A &operator=(A &a) throw()
    > {
    > s = a.s ;
    > a.s = -1 ;
    > return *this ;
    > }
    > } ;
    >
    > A f()
    > {
    > A a(5) ;
    > return a ;
    > }
    >
    > int main()
    > {
    > A a ;
    > a = f() ;
    > return 0 ;
    > }
    >
    >
    > Compling gives the following problem:
    > $ g++ -W -Wall -ansi -pedantic test.cpp
    > test.cpp: In function `int main()':
    > test.cpp:41: error: no match for 'operator=' in 'a = f()()'
    > test.cpp:25: error: candidates are: A& A::eek:perator=(A&)
    >
    > That is, the assignment fails, for some reason I cannot determine.
    > Any insight would be appreciated.


    Your programm is not const-correct. f() returns a temporary, and in C++,
    you can't bind a non-const reference to a temporary. therefore, no
    suitable operator= is found. Since your operator= actually needs to
    modify the s member of the right hand side, you'd have to use the
    mutable keyword for your member. Try this:

    class A
    {
    mutable int s ;
    public :
    A() : s(-1)
    { }

    A(int s) : s(s)
    { }

    A(const A &a) : s(a.s)
    {
    a.s = -1 ;
    }

    ~A() throw()
    {
    // Do something to release resource.
    s = -1 ;
    }

    A &operator=(const A &a) throw()
    {
    s = a.s ;
    a.s = -1 ;
    return *this ;
    }
    } ;
    Rolf Magnus, May 22, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JezB

    Resource Manager Question

    JezB, May 21, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    304
  2. Dirc Khan-Evans
    Replies:
    1
    Views:
    886
    Karl Seguin
    Oct 17, 2005
  3. MotoK
    Replies:
    59
    Views:
    1,792
    Keith Thompson
    Sep 15, 2006
  4. padma
    Replies:
    0
    Views:
    338
    padma
    Oct 3, 2007
  5. Metalone
    Replies:
    0
    Views:
    335
    Metalone
    Jan 6, 2010
Loading...

Share This Page