CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: new operator

  1. #1
    Join Date
    Jul 2002
    Posts
    56

    new operator

    Hey im just a begginer at this language and im having a real difficult time. Im not sure what this means. I was asked to create like six forms which i did but now it says do this. im kinda lost please help.

    You MUST create all the forms using the new operator. For example,


    int main()
    {
    form *[5];
    return 0;
    }

  2. #2
    Join Date
    Mar 2002
    Location
    California
    Posts
    1,582
    form[0] = new form();
    form[1] = new form();
    ...
    form[5] = new form();


    or (better) ...

    for( int i = 0; i < 5; ++i )
    {
    form[i] = new form();
    }

    Jeff

  3. #3
    Join Date
    Jun 2001
    Location
    Switzerland
    Posts
    4,443
    Originally posted by jfaust
    form[0] = new form();
    form[1] = new form();
    ...
    form[5] = new form();


    or (better) ...

    for( int i = 0; i < 5; ++i )
    {
    form[i] = new form();
    }

    Jeff
    That probably was a typo, but if form is a type name
    Code:
    form[i] = new form();
    won't compile. I think you meant
    Code:
    const int forms = 5
    form *form_[forms];
    for( int i = 0; i < forms; ++i )
    {
       form_[i] = new form();
    }
    Gabriel, CodeGuru moderator

    Forever trusting who we are
    And nothing else matters
    - Metallica

    Learn about the advantages of std::vector.

  4. #4
    Join Date
    Mar 2002
    Location
    California
    Posts
    1,582
    Right. I was attempting to keep the same "form" as the OP (pun intended). Thanks for catching that and avoiding further confusion.

    Jeff

  5. #5
    Join Date
    Jul 2002
    Posts
    56

    still lost

    im still in the dark.
    I have a base class form and have to create osap, school, bank and apply forms. Could someone explain how i would use the new operator please.

  6. #6
    Join Date
    Jun 2002
    Location
    Letchworth, UK
    Posts
    1,020
    I assume osap, school, bank and apply forms are derived from form, so you'd possibly have something like
    Code:
    class form
    {
    public:
        virtual ~form() {}
    };
    class osap: public form {}
    class school: public form {}
    class bank: public form {}
    class apply: public form {}
    
    main ()
    {
        // To create pointers to the different forms
        form* aform[4];
        int i = -1;
       
        aform[++i] = new osap;
        aform[++i] = new school;
        aform[++i] = new bank;
        aform[++i] = new apply;
        ...
    
    
        // When you want to delete them
        for (i = 0; i < 4; i++)
            delete aform[i];
    }
    The declaration creates pointers. Each pointer has to be assigned an instance using the new operator.
    Succinct is verbose for terse

  7. #7
    Join Date
    Jul 2002
    Posts
    56

    k

    one question , what does the virtual ~form do??

  8. #8
    Join Date
    Mar 2002
    Location
    California
    Posts
    1,582
    When you call delete on the base class object, it will call the correct derived class destructor. Any class acting as a base class should always have a virtual destructor.

    Jeff

  9. #9
    Join Date
    Jun 2002
    Location
    Letchworth, UK
    Posts
    1,020
    It is the destructor. When aform[i] is deleted, it calls the destructor of the derived object: not just the form destructor. The easiest way to see this is to put couts in the destructors.
    Succinct is verbose for terse

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured