Can a function return an interator to main()

This is a discussion on Can a function return an interator to main() within the C++ Programming forums, part of the General Programming Boards category; What you need to understand is the scope of variables and how to pass through reference or value depending on ...

  1. #16
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    What you need to understand is the scope of variables and how to pass through reference or value depending on what you need.
    In your example, the function should probably take the vector as argument instead of creating it.
    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.

  2. #17
    Darkness Prevails Dark_Phoenix's Avatar
    Join Date
    Oct 2006
    Location
    Houston, Texas
    Posts
    174
    Code:
    class SomeClass
    {
        public:
        SomeClass() { }
        ~SomeClass() { }
        void Add(int a) { SomeList.push_back(a); }
        std::list<int>::iterator GetFirstElement() { return SomeList.begin(); }
    
        private:
        std::list<int> SomeList;
    }
    
    int main()
    {
        SomeClass Foo;
        Foo.Add(5);
    
        std::list<int>::iterator it = Foo.GetFirstElement();
    
        return 0;
    }
    Using Code::Blocks and Windows XP

    In every hero, there COULD be a villain!

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,647
    Concerning Dark_Phoenix's example - I think that if you want to provide:
    Code:
    std::list<int>::iterator GetFirstElement() { return SomeList.begin(); }
    You should also provide:
    Code:
    std::list<int>::const_iterator GetFirstElement() const { return SomeList.begin(); }
    Incidentally, I note that the terminating semi-colon is missing from the class definition.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #19
    Darkness Prevails Dark_Phoenix's Avatar
    Join Date
    Oct 2006
    Location
    Houston, Texas
    Posts
    174
    Incidentally, I note that the terminating semi-colon is missing from the class definition.
    Hmm, now how did I miss that..? :P
    Using Code::Blocks and Windows XP

    In every hero, there COULD be a villain!

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How can I make this code more elegant?
    By ejohns85 in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2009, 08:55 AM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Another weird error
    By rwmarsh in forum Game Programming
    Replies: 4
    Last Post: 09-24-2006, 10:00 PM
  4. Replies: 4
    Last Post: 11-23-2003, 06:15 AM
  5. opengl program as win API menu item
    By SAMSAM in forum Game Programming
    Replies: 1
    Last Post: 03-03-2003, 06:48 PM

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