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; ....and if so what is the function defination then to be ? I just want my self written function (sort) ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    34

    Can a function return an interator to main()

    ....and if so what is the function defination then to be ?

    I just want my self written function (sort) which sorts lists to return me a pointer after its execution so that i can perform tasks on the sorted list IN THE FUNCTION MAIN(); (dont ask why...i just want to cause i cant control my urge to do it otherwise which stems out of the fact that i cannot do it as of now)

    so i wondered that since iterators are kind of pointers can i simply return them ?

    Thanks all..
    I am in love with c++...Hence i broke off with my girl

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,249
    Quote Originally Posted by roalme00 View Post
    ....and if so what is the function defination then to be ?

    I just want my self written function (sort) which sorts lists to return me a pointer after its execution so that i can perform tasks on the sorted list IN THE FUNCTION MAIN(); (dont ask why...i just want to cause i cant control my urge to do it otherwise which stems out of the fact that i cannot do it as of now)

    so i wondered that since iterators are kind of pointers can i simply return them ?

    Thanks all..
    main() will only accept iterators on Tuesdays, Thursdays, dates ending with '6', and days where more than three rainbows occur in Alabama.

    More seriously, why do you think main() is different from any other code? There's no restriction on what you can do inside main().

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    34
    LOL...i think its safe to assume that the answer is blowing in the wind and the answer is NO
    I am in love with c++...Hence i broke off with my girl

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,249
    Quote Originally Posted by roalme00 View Post
    LOL...i think its safe to assume that the answer is blowing in the wind and the answer is NO
    Actually the answer is YES. Of course you can return an iterator to main(). Just do it how you'd do it elsewhere.

  5. #5
    Registered User
    Join Date
    Jan 2006
    Posts
    34
    Quote Originally Posted by brewbuck View Post
    More seriously, why do you think main() is different from any other code? There's no restriction on what you can do inside main().
    i know but then i was planning onto writting special functions for each tasks that i would be performing on the sorted list.Hence......thanks though for ruining my dream of wanting to return an iterator to main()

    What...the answer is yes you say....what would be the function declaration of such a function returning an iterator ?

    eg:

    (what should be here) IAM_A_Function_Returning_ITERATOR( int, int)
    I am in love with c++...Hence i broke off with my girl

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    What the hell are you talking about?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,249
    Quote Originally Posted by roalme00 View Post
    What...the answer is yes you say....what would be the function declaration of such a function returning an iterator ?

    eg:

    (what should be here) IAM_A_Function_Returning_ITERATOR( int, int)
    Suppose for instance you were returning a std::vector<int> iterator... You'd return:

    Code:
    std::vector<int>::iterator some_func(...)
    {
        // do stuff
        return some_iterator;
    }

  8. #8
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I would suppose that main is the entry point of your program and it should get called only once during program execution. That is, you don't ever call main yourself.

    Whatever you mean by interator to main, you are probably not supposed to use it in any way, so there's no point returning it.

    Perhaps post your real problem but keep in mind that calling main on something in a linked list is not a good idea.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  9. #9
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,249
    Quote Originally Posted by anon View Post
    I would suppose that main is the entry point of your program and it should get called only once during program execution. That is, you don't ever call main yourself.
    I think the actual question was simply how to return an iterator.

    Sometimes the posts here are like those word problems you solve in elementary school, where half of the information is irrelevant or misleading

  10. #10
    Registered User
    Join Date
    Jan 2006
    Posts
    34
    grrrrrrrr....my bad...ok heres an example code ..i am not attempting to call main() as brewbuck guessed..my bad if the post is confusing..infact I am in elementary school as brewbuck guessed right yet again..

    Code:
    #include<iostream>
    #include<list>
    
    using namespace std;
    
    class RandomClass
    {
    	private:
    		int xyz;
    	public:
    		void Make_A_List(RandomClass *);
    };
    
    int main()
    {
    	RandomClass temp;
    	temp.Make_A_List(&temp);
    
    	return 0;
    }
    
    void RandomClass::Make_A_List(RandomClass *p)
    {
    	list<RandomClass> ListOfInts;
    	list<RandomClass>::iterator it;
    	int input;
    
    	cout << "Enter the integer : "<< endl;
    	cin >>input;
    	p->xyz=input;
    	ListOfInts.push_back(*p);
    
    	for(it= ListOfInts.begin();it!=ListOfInts.end();it++)
    		cout<<it->xyz<<endl;	
    }
    now what if i want to add another line at the end of Make_A_List function...
    Code:
    return it ; // "it" is the iterator
    in that case i need to get rid of the void preceding the function prototype...but what takes the place of void then....that is my question.
    I am in love with c++...Hence i broke off with my girl

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,914
    Well, for one thing, you would be returning an iterator to a local list that is destroyed at the end of the function. That is a Bad Thing.

    What problem are you trying to solve?
    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

  12. #12
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,249
    Quote Originally Posted by roalme00 View Post
    grrrrrrrr....my bad...ok heres an example code ..i am not attempting to call main() as brewbuck guessed..my bad if the post is confusing..infact I am in elementary school as brewbuck guessed right yet again..
    If that's true I retract any snarkiness I may have displayed. Cool!

  13. #13
    Registered User
    Join Date
    Jan 2006
    Posts
    34
    ohh lord...so true...your post laserlight dawns on me like an epiphany..the list will be destroyed as soon as control hops out of the function...
    well i am not trying to solve any particular problem...i just had this question if iterators can be returned.Brewbuck did say one can but i havent quite gotten it it..thanks to my noob brain

    So in the above example if i were to declare the list as global by making the follwing change to the above code
    Code:
    class RandomClass
    {
    	private:
    		int xyz;
    	public:
    		void Make_A_List(RandomClass *);
    }temp ;
    can i then simply return the iterator as mentioned in the previous post and if yes then what is the function prototype of the Make_A_List function
    I am in love with c++...Hence i broke off with my girl

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,914
    That does not make the list global, but makes a global RandomClass object. You probably do not even need globals at all, but perhaps the list should be passed by reference.
    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

  15. #15
    Registered User
    Join Date
    Jan 2006
    Posts
    34
    ok thanks....let me put an end to this as of now...i think i need some more reading about STL...will do that first and if i still have got the same question left unanswered then i will post here again....



    thanks all
    I am in love with c++...Hence i broke off with my girl

Page 1 of 2 12 LastLast
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