Need help with returning and element of a std::list

This is a discussion on Need help with returning and element of a std::list within the C++ Programming forums, part of the General Programming Boards category; I need some help with the logic of a function I have. I simply am trying to return an element ...

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    131

    Need help with returning and element of a std::list

    I need some help with the logic of a function I have. I simply am trying to return an element of a list...

    Code:
    std::list < Font > m_fontList;
    
    Font &Graphics::GetFont( const std::string &fontName )
    {
    	std::list< Font >::iterator listIterator;
    	// Find the font.
    	for( listIterator = m_fontList.begin(); 
            listIterator != m_fontList.end();
            listIterator++ )
    	{
    		if( listIterator->GetFontName() == fontName )
    		{
    			return *listIterator; //1 is this correct
    		}
    	}
    	// 2 What would the default return value be if the element was not found
    }
    1) am I returning the element correctly via the iterator?

    2) What would I do for the default return value?

    Regards

    Chad

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by chadsxe View Post
    1) am I returning the element correctly via the iterator?

    2) What would I do for the default return value?

    Regards

    Chad
    1) Yes
    2)
    Whatever you want. You could have a default font, in case it didn't exist. If you don't want it, you'll have to change the function definition to one of two things:
    - Return a pointer, where a NULL pointer is returned if the font wasn't found.
    - Pass the Font reference as argument and return true if it was found and false if not, eg:
    Code:
    bool Graphics::GetFont( const std::string &fontName, Font &ret )

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    You could also throw an exception if the font you want isn't in the list.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. is it possible to pass a structure element by reference?
    By Cathalo in forum C++ Programming
    Replies: 6
    Last Post: 03-04-2009, 09:01 AM
  2. returning a linked list from a function
    By jlshipman in forum C++ Programming
    Replies: 3
    Last Post: 01-23-2006, 06:05 PM
  3. returning mode value from array
    By dantestwin in forum C++ Programming
    Replies: 9
    Last Post: 07-09-2004, 01:58 AM
  4. Returning address to temp var.
    By Calthun in forum C++ Programming
    Replies: 2
    Last Post: 04-06-2004, 01:42 PM
  5. returning 2D arrays
    By ... in forum C++ Programming
    Replies: 2
    Last Post: 09-02-2003, 12:28 PM

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