Calling a function from a stored object

This is a discussion on Calling a function from a stored object within the C++ Programming forums, part of the General Programming Boards category; I have just managed to get my head (mostly) around the factory pattern but at the moment I am stuck ...

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    10

    Calling a function from a stored object

    I have just managed to get my head (mostly) around the factory pattern but at the moment I am stuck trying to access created objects.

    I store them in an STL vector:
    Code:
    Vector.push_back (Factory::BuildObject());
    As far as I can tell, this is a reasonable and functioning way to do this.

    How do I call my object's functions, or even find out in which 'position' of the vector, a certain object is held?

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,435
    Why wouldn't you know where they are? If you store them in an array, you obviously have to keep track of the index!
    That said, you can get the most recently pushed back object with the back() function.
    Otherwise, use the index operator or get member function:
    Vector[i].my_member_function()
    or
    Vector.get(i).my_member_function()
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    10
    Thank you, I was really over complicating things.

    Cheers

  4. #4
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    Vector may not be the best choice for this. Removing from a vector invalidates all iterators past the point of removal. So if you are using the vector indices as some sort of unique identifier for your factoried objects you will be in for a surprise when you attempt to remove an item from the vector.

  5. #5
    Registered User
    Join Date
    Jan 2010
    Posts
    10
    Oh, okay, thanks for the tip. This is my first time using STL, any chance you could suggest a better container to use?

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,435
    If you want to associate something with an id, say, a string, then std::map might be a better choice. Otherwise you may consider std::list.
    Both will not incur huge penalties when removing things in the middle of the list, but std::list will be better optimized for removals and insertions everywhere. Although it cannot associate something with an id.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Jan 2010
    Posts
    10
    std::map sounds like the one for me. Thanks guys!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. my short sightedness........ help....
    By gemini_shooter in forum C++ Programming
    Replies: 11
    Last Post: 02-03-2006, 10:34 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM

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