Declaring New Objects

This is a discussion on Declaring New Objects within the C++ Programming forums, part of the General Programming Boards category; is it possible to create new objects as you see necessary? for example: if (i==1) // for whatever reason new ...

  1. #1
    Unregistered
    Guest

    Arrow Declaring New Objects

    is it possible to create new objects as you see necessary?

    for example:

    if (i==1) // for whatever reason
    new Struct;


    what i am really trying to ask is: can you create a new objects for a class, struct, etc. dynamically, without having to declare it in the driver, so you are not limited in the number of objects that can be in one program?

    if you dont understand just put it in a reply..

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    194
    yes you can.
    using the "new" keyword

    Code:
    int * array = NULL;
    int size;
    cout << "What size do you want array to be?\n";
    cin >> size;
    array = new int [size];
    if( array == NULL)
    {
       cout << "Error, not enough mem to create array" << endl;
    }
    array can now be acessed like a regular array, using brackets
    array[index], and index must be in the range 0..size-1.
    Once you are done with array give the memory back with delete
    Code:
    delete [] array;
    array = NULL;
    new returns a pointer to what ever type you pass it.
    type * new type [size];
    the size inside brackets is optional.
    Code:
    int * CreatedWithNew = NULL;
    CreatedWithNew= new int;
    *CreatedWithNew = 10;
    //blah blah
    delete CreatedWithNew;
    CreatedWithNew = NULL;
    Notice in that call to delete i did not need to use the [] before the var name. That is becuase i only created 1 int. When you pass delete the [] it tells delete to clear all the vars created with new. If you dont pass it the [], it will only clear the first var.

    You should make sure you delete all memory you create with new to avoid memory leaks. A memory leak is when you get memory from new, then never delete it. The operating system then loses that memory for other programs, or future calls to new, and wont get it back until a reboot. New can be used with any data type.

  3. #3
    Unregistered
    Guest
    thanks - that was really helpful


    is there anyway you can do that with an object of a class?

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    194
    Yes, you can use new to create and built in data type OR and class or struct.

    SomeClass * = new SomeClass[10];

  5. #5
    Unregistered
    Guest

    Lightbulb

    so they are referred to by number and not by name?

    thanks again

  6. #6
    Registered User
    Join Date
    Dec 2001
    Posts
    194
    no, that would create 10 copys of SomeClass.

  7. #7
    Registered User
    Join Date
    Dec 2001
    Posts
    421

    Talking

    BTW, you don't have to use the [] if you don't want to create more than one:

    int* ip;
    .
    .
    .

    ip = new int;

    *ip = 10;
    .
    .
    .
    delete ip;

    just thought i'd throw that in
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

  8. #8
    Unregistered
    Guest
    so how would you refer to the last copy of SomeClass?

    SomeClass [9]

    (i assume)

    and would the variables under it be referred to as

    SomeClass [9].x;
    // assuming there is a variable named x
    // somewhere in that class

    sorry these questions are overly newbie

  9. #9
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    int y = (Someclass + 9)->x;
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

  10. #10
    Registered User
    Join Date
    Dec 2001
    Posts
    194
    Originally posted by Uraldor
    In order to understand recursion, one must first understand recursion
    I dont think understanding recursion is so simple. Its more like

    In order to understand recursion one must first understand that in order to understand recursion one must first understand that in order to und......

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 60
    Last Post: 12-20-2005, 10:36 PM
  2. Question about cout an stack object?
    By joenching in forum C++ Programming
    Replies: 8
    Last Post: 05-08-2005, 10:10 PM
  3. pointers in arrays... Declaring objects in a loop.
    By Guest852 in forum C++ Programming
    Replies: 10
    Last Post: 04-05-2005, 06:57 AM
  4. chain of objects within pop framework help needed
    By Davey in forum C++ Programming
    Replies: 0
    Last Post: 04-15-2004, 10:01 AM
  5. array of objects?
    By *~*~*~* in forum C++ Programming
    Replies: 4
    Last Post: 05-31-2003, 05:57 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21