Re: Alternatives to STATIC LOCAL variables

Discussion in 'C++' started by Alf P. Steinbach, Aug 22, 2003.

  1. On 22 Aug 2003 14:22:29 -0700, (Bo) wrote:

    >I m writing a runtime graphic shape library similar to the one below.
    >The size of each shape isn't known until run time called by the user
    >of the objectLibrary. Thus I can only allocate memory in functions
    >such as circle() instead of globally.


    That's good.


    >I made the local memory allocation static so it is not destroyed, but
    >a call to circle() again overwrites the previous circle because the
    >memory is static.


    Use operator 'new'.



    >Surely there are ways to overcome this? Thanks.
    >
    >class object
    >{
    >public:
    >vector* data;
    >
    >object() {}


    Whoops, you forgot to initialize 'data' in the constructor.

    >};






    >class objectLibrary {
    >
    >public:
    >
    >static object* circle( double radius, int segment)
    >{
    >/* Due to being static, the memory pointed by answer is stored in the
    >heap
    > and is not destroyed once the function ends. However, if I later
    >call
    > circle() again the previous circle is overwritten because this
    >memory is
    > static.
    >*/


    Generally it's a good idea to use C++ line-comments, '//'.

    That way you can use /* ... */ to comment out code.

    Some novices prefer, however, to use #ifdef ... #endif to comment out code.




    >static object *answer = object();


    That does (should) not compile.

    Post code that compiles.
     
    Alf P. Steinbach, Aug 22, 2003
    #1
    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. John Davison

    lack of C's static local variables

    John Davison, Mar 3, 2004, in forum: Java
    Replies:
    21
    Views:
    1,007
  2. Patrick Hoffmann
    Replies:
    3
    Views:
    2,921
    Christian Jan├čen
    Aug 8, 2003
  3. Michael
    Replies:
    2
    Views:
    467
    Michael
    Oct 16, 2004
  4. Martin Wells

    Static inline functions with static local variables

    Martin Wells, Oct 6, 2007, in forum: C Programming
    Replies:
    10
    Views:
    726
    Army1987
    Oct 8, 2007
  5. Sullivan WxPyQtKinter
    Replies:
    10
    Views:
    707
    Antoon Pardon
    Nov 8, 2007
Loading...

Share This Page