Overloading Array Objects for use with Classes

This is a discussion on Overloading Array Objects for use with Classes within the C++ Programming forums, part of the General Programming Boards category; I'm trying to find a tutorial or some direction on overloading array objects for use with a class. An example ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    16

    Question Overloading Array Objects for use with Classes

    I'm trying to find a tutorial or some direction on overloading array objects for use with a class. An example of a program that does this would really help since the book I'm using doesn't provide any help here, but simply lists the syntax for a non-constant array as "Type& operator[](int index);" and a constant array as "const Type& operator[](int index) const;" with Type being the data type of the array elements. I'm trying to instantiate objects for my CollegeCourse class by creating an array of objects. I need to create three CollegeCourse objects containing information such as "ENG101", 'A', and 3 (credits). I learn best by example, but like I said, the book I'm referencing is severely lacking in examples. I'm having a hard time finding class examples at all on the internet. Can anyone help? Hope I explained this alright. Thanks!

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    If you're trying to initialize classes (you are trying that, right?), then you need to create your custom constructors.
    Syntax is
    Code:
    ClassName(argument_list); // Declaration
    ClassName::ClassName(argument_list) { } // Definition
    Note that constructors have no return type.
    Though I'm not sure how you're trying to create those objects. And I don't see why you need to overlord operator [].

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    An array will work with a class already. The code you sited looks like part of a user defined array class, rather than built-in arrays.

    Do you have your CollegeCourse class written? Can you show the "array" class you're using?

    For arrays and most array classes, you can create the objects in the array automatically, but you cannot initialize them with different data when they are created (although this might change in future C++ versions). So to create three objects with different information, you'll have to create the three in the array and change the information, or create them one at a time and add them to the array.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. Array of pointers to point objects
    By totalfreeloader in forum C++ Programming
    Replies: 6
    Last Post: 11-27-2003, 08:26 AM
  3. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 11:09 AM
  4. for loop to create an array of objects
    By Shadow12345 in forum C++ Programming
    Replies: 9
    Last Post: 05-03-2002, 06:26 PM
  5. 2d Array access by other classes
    By deaths_seraphim in forum C++ Programming
    Replies: 1
    Last Post: 10-02-2001, 08:05 AM

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