Thread: Pointer = new (?)

  1. #1
    Skarr
    Guest

    Pointer = new (?)

    Hi

    I have this problem: To use "object = new class" stuff, the 'object' has to be a pointer. And that makes it an array. I don't want an array. How do I go about this?

    Thanks

  2. #2
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    it makes an array?

    let's say you had a pointer pXYZ
    Code:
    Object* pXYZ = new Object
    now pXYZ points to one valid object

  3. #3
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    Code:
    Object single;  //single object
    Object* pointer;  //pointer to an object, not specified yet
    Object array[ 10 ];  //array of objects
    This is where you are confused. The identifier 'array' is a const pointer to the first member of the array, array[ 0 ]. This does not mean that a pointer always points to an array, it simply points to an address in memory. If we make 'pointer' point to single we have a pointer to a single object, like so:
    Code:
    pointer = &single;
    Couldn't think of anything interesting, cool or funny - sorry.

  4. #4
    elad
    Guest
    int * first = new int;
    first is a pointer to a single int

    int * second = new int[6];
    second is a pointer to the first element of an array of 6 ints. This is essentially the same description as the name of an array. The name of an array acts as a constant pointer to the first element of the arry. second can be used anywhere the name of an array can be used.

    const SIZE = 8;
    int third[SIZE];

    void display(int * array, int size)
    {
    for(i = 0; i < size; i++)
    cout << array[i] << endl;
    }

    display(second, 6);
    display(third, SIZE);

  5. #5
    Skarr
    Guest
    Okay, thanks...

    That means I should use pointers whenever I can? You know, to save memory?

  6. #6
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    >> That means I should use pointers whenever I can? You know, to save memory?

    Only use pointers if they are necessary. If the use of pointers in a program would drastically increase it's efficiency, then by all means, use them.

  7. #7
    Skarr
    Guest
    But then why not use them?

  8. #8
    Unregistered
    Guest
    Originally posted by endo
    Code:
    Object single;  //single object
    Object* pointer;  //pointer to an object, not specified yet
    Object array[ 10 ];  //array of objects
    This is where you are confused. The identifier 'array' is a const pointer to the first member of the array, array[ 0 ]. This does not mean that a pointer always points to an array, it simply points to an address in memory. If we make 'pointer' point to single we have a pointer to a single object, like so:
    Code:
    pointer = &single;
    pointer = &single ... why the reference?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 03:47 PM
  3. Parameter passing with pointer to pointer
    By notsure in forum C++ Programming
    Replies: 15
    Last Post: 08-12-2006, 07:12 AM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM