Using dynamic memorys in a class

This is a discussion on Using dynamic memorys in a class within the C++ Programming forums, part of the General Programming Boards category; Ok, basicy what im trying to do is use dynamic memory to create a user defined array of a class... ...

  1. #1
    Master n00b Matty_Alan's Avatar
    Join Date
    Jun 2007
    Location
    Bloody Australia Mate!
    Posts
    96

    Using dynamic memorys in a class

    Ok, basicy what im trying to do is use dynamic memory to create a user defined array of a class... heres sorta what i have so far, or atleast the way iv'e tryed to go about it

    defined class in header file
    Code:
    class SPRITE{
         public: 
          int posx, posy;
          int health;
          int sprite_id;
          
    
          }typedef sprite[i];
    and the .cpp file
    Code:
    #include <iostream>
    #include <new
    #include "sprite.h"
    using namespace std;
    
    int main()
    {
    
    int i = 3;               // create a variable
    cin>> i;
    
    sprite *enemy[i];      //Create dynamic memory
    enemy[i] = new sprite;
    
    
    enemy[1].posx = 3;    //define a variable from the class array
    
    cout<< enemy[1].posx; //print resualt
    
        return 0;
    }
    The idea of what im trying to do is create a sprite handler that creates and many or as little sprites as needed for a level without wasting any memory.
    This is the first time iv'e ever user used dynamic memory i'm not even sure if this is the best way o go about this but any advise and help would be appriciated
    cheers
    Matty Alan
    Last edited by Matty_Alan; 03-14-2010 at 07:48 AM.

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,667
    First, you create a normal declaration. A declaration defines a type, so to speak, and an array consists of several instances of that type.
    So:
    Code:
    class Sprite
    {
    ...
    };
    (Btw, do not use upper-case names for types. They are usually reserved for macros.)

    Then, you simply create an array by using a pointer and specifying the number of elements you want:
    Code:
    Sprite* sprites = new Sprite[100]; // Create 100 sprites
    sprites[5].posx = 3; // Use it like so
    delete [] sprites; // Then don't forget to free it
    You can also use a vector for the job:
    Code:
    std::vector<Sprite> sprites(100); // Create 100 sprites
    sprites.at(5).posx = 3; // Use it like so
    // No need to free!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Master n00b Matty_Alan's Avatar
    Join Date
    Jun 2007
    Location
    Bloody Australia Mate!
    Posts
    96
    Thanks for the help it works fine now
    so when you create a class it uses it as a new type? does that work for stuctures also?

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,667
    Yes.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 08:10 AM
  2. Replies: 8
    Last Post: 07-24-2006, 08:14 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 01:30 PM

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