A question of The List Container.

This is a discussion on A question of The List Container. within the C++ Programming forums, part of the General Programming Boards category; I have a question about the list class. The data consists of objects of the type intEntry Here is my ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    77

    A question of The List Container.

    I have a question about the list class. The data consists of objects of the type intEntry

    Here is my header file
    Code:
    class intEntry
    {
    public:
    	intEntry(int v, int c = 1);
    		// initalize the integer value and its count
    
    	int getValue() const;
    		// return value;
    
    	int getCount() const;
    		// return count
    	
    	void increment();
    		// increment count
    
    	friend bool operator< (const intEntry& lhs, const intEntry& rhs);
    	friend bool operator== (const intEntry& lhs, const intEntry& rhs);
    		// compate lhs and rhs using value
    
    	friend ostream& operator<< (ostream& ostr, const intEntry& obj);
    		// output obj in format "value value ... value" (const times)
    
    
    private:
    	int value;
    		// integer value;
    
    	int count;
    		// number of occurrences of value
    
    };
    My question is what those codes for
    Code:
    friend bool operator< (const intEntry& lhs, const intEntry& rhs);
    	friend bool operator== (const intEntry& lhs, const intEntry& rhs);
    		// compate lhs and rhs using value
    
    	friend ostream& operator<< (ostream& ostr, const intEntry& obj);
    		// output obj in format "value value ... value" (const times)
    Why do I need to put a "friend" at the first?
    Also, what is "operator<" ?
    and (const intEntry& lhs) means the address of lhs?

    Last one, What is the operator== for?

    Thanks for helping.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    Quote Originally Posted by joenching
    Why do I need to put a "friend" at the first?
    You need to do this because the member variables are private to the intEntry class. Putting "friend" in front of them means they have access to these private member variables so they can do their job.

    Quote Originally Posted by joenching
    Also, what is "operator<" ?
    It is an overloaded "less-than" operator for comparing items of the intEntry class, for example:

    Code:
    intEntry a, b;
    
    if( a < b )  // Here the "operator<" is being used to compare a with b
    {
        // Do something
    }

    Quote Originally Posted by joenching
    and (const intEntry& lhs) means the address of lhs?
    It is a const-reference argument. Arguments are passed by copy as a default which means a copy of the passed in value is made for the function to work with. Passing by reference means that the actual object is passed into the function instead of a copy.

    Quote Originally Posted by joenching
    Last one, What is the operator== for?
    It is an overloaded equality operator for use in comparing items of the intEntry class:

    Code:
    intEntry a, b;
    ...
    if( a == b )  // Here the "operator==" is used to test for equality
    {
        // They are equal
    }
    else
    {
        // They are not equal
    }
    Your questions have nothing to do with the list container...
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    77
    Oh, it's because i have a list container question ahead

    That's why i want to make sure myself undestand everything before i work in.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. linked list question
    By brb9412 in forum C Programming
    Replies: 16
    Last Post: 01-04-2009, 03:05 PM
  2. Need help sorting a linked list. Beginner
    By scarlet00014 in forum C Programming
    Replies: 1
    Last Post: 09-27-2008, 06:16 PM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. Problem with linked list ADT and incomplete structure
    By prawntoast in forum C Programming
    Replies: 1
    Last Post: 04-30-2005, 01:29 AM
  5. compiler build error
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-30-2003, 09:16 AM

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