simple class

This is a discussion on simple class within the C++ Programming forums, part of the General Programming Boards category; I believe my first post was unclear as to what i was asking, so anyways i have to implement a ...

  1. #1
    Kyle
    Join Date
    Jan 2005
    Posts
    5

    simple class

    I believe my first post was unclear as to what i was asking, so anyways i have to implement a class named course with 4 private variable 2 being strings(department and Title) 1 being an int(course no.) and another being an array of 3 characters(section no.). I have to write accessors for each of the variable, which is easy but i don't not know how to access the character array since c++ wont allow arrays to be return types. There was a hint saying to return a reference to that array and i have no clue what that means, we have not used pointers yet, so im clueless! Also i need to declare a typedef for the array of three characters and the accessor for the character array (section no.) should return a const refrence to the typedef. If i could only figure out what is exactly trying to be said im sure i'd have no problem implement the class. For the typedef I have

    Code:
    typedef char value_type[3];
    which i dont know if it is correct as well.
    Any help or advice would be great! Thanks again

  2. #2
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    If I remember correctly the name of the array is a constant reference to the array, aka a constant pointer. Thus to return an array just return a pointer of the same type, aka
    Code:
    #include <iostream>
    
    using std::cin;
    using std::cout;
    using std::endl;
    
    int* getArray(void);
    
    int myArr[3] = {0, 1, 2};
    
    int main() {
    
    	int* myPointer;
    
    	myPointer = getArray();
    
    	for(int i = 0; i < 3; i++) {
    		cout << myPointer[i]<< endl;
    	}
    
    	cin.get();
    	return 0;
    }
    int* getArray(void) {
    
    	return myArr;
    }
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  3. #3
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    To be nitpicky:

    >>the name of the array is a constant reference to the array, aka a constant pointer.
    Arrays can often be represented as pointers, but an array is NOT a pointer; and an array name is NOT a 'constant reference to the array'.

    Example:
    Code:
    int array[256];  //Array
    int* pointer = array;  //Pointer that's pretending to be an array
    
    typedef int t[256];  //Typedef for an array of 256 int
    const t& x = array;  //Constant reference to an array of 256 int
    In most cases you can substitute a pointer for an array or vice versa, but there are subtle differences such as use of sizeof() to determine the length of the array - it'll work with an array but not with a pointer. And, of course, you can't use an array to point at another object

    So the solution is, as andyhunter did, you can return the string (character array) as a char* since pointers can act like an arrays anyway. Just be careful, because making any changes to the 'array' that you get from the accessor will also be changed in the actual array; and don't try using sizeof() to get the length of the array either.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  4. #4
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    You are right -- my bad. What I should have said to be more specific is that the name of the array can be used as the constant reference to the first value in the array.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  5. #5
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>the name of the array can be used as the constant reference to the first value in the array.
    The name of the array can be interpreted as a pointer to the 0'th element in the array.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 09:18 PM
  2. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  3. Replies: 3
    Last Post: 10-31-2005, 12:05 PM
  4. Replies: 8
    Last Post: 10-02-2005, 01:27 AM
  5. Errors in a simple hash table class.
    By TheSquid in forum C++ Programming
    Replies: 4
    Last Post: 02-23-2005, 04:49 AM

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