Thread: Array Help

  1. #1
    Registered User Gnoober's Avatar
    Join Date
    Oct 2002
    Posts
    40

    Question Array Help

    Hey, I'm kind of new at c++ and I have a question about arrays. Can you make any type of object an array? I tried and I don't know if i did it wrong or if my idea is too farfetched or what...

    Example:

    Code:
    class Cpart{
          /*blah blah blah*\
    };
    
    Cpart cow;
    
    Cpart IDtoPart[15];
    
    IDtoPart[0] = cow;
    Like i said, I really need some help here. Anything is greatly appreciated. Thanks!
    - Grady (:

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    What you have should work, but you don't need to assign cow to IDtoPart[0], just initialize IDtoPart[0] since it is a valid object:
    Code:
    class Obj
    {
    public:
      int i;
    };
    
    int main()
    {
      Obj array[10];
    
      array[0].i = 10;
    
      return 0;
    }
    Note that some operations will be more difficult or finicky if you don't have the appropriate constructors defined for the class.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User Gnoober's Avatar
    Join Date
    Oct 2002
    Posts
    40
    Haven't tried it yet but i'm sure it will work. Its not exactly what I wanted but it will work. That will help me out a lot... thanks!
    - Grady (:

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM