Thread: Can a function return an interator to main()

  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
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    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
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    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,895
    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
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    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
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    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
    28,413
    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?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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

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, 07:15 AM
  5. opengl program as win API menu item
    By SAMSAM in forum Game Programming
    Replies: 1
    Last Post: 03-03-2003, 07:48 PM