Like Tree1Likes
  • 1 Post By King Mir

Class function return type

This is a discussion on Class function return type within the C++ Programming forums, part of the General Programming Boards category; Code: vector<sf::Text> notepad::retrieveButtons(){ } As you can see, I'm trying to let the computer know that the notepad class has ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    30

    Class function return type

    Code:
    vector<sf::Text> notepad::retrieveButtons(){
    
    }
    As you can see, I'm trying to let the computer know that the notepad class has a function retrieveButtons, and that the function will return a vector of type sf::Text.

    Is this possible? Or will I have to use pointers?
    Last edited by Justin H; 12-04-2012 at 06:20 PM.

  2. #2
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    407
    From what I have read you can return a vector no problem, but the larger the vector the more inefficient it becomes. There is also the possibility of just passing a vector to the function by reference. If I am recalling an article I read recently if you pass by reference you will only have one vector in memory, but if you return a vector from the function back to the original vector that you will use outside the function you are possibly doubling the memory usage for the same function.

    There is a long discussion on the subject here as well.

    how to return std::vector from function? - C / C++
    Last edited by Lesshardtofind; 12-04-2012 at 06:55 PM.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,437
    This is typically not necessary.
    The compiler can perform NRVO, thus preventing any copy at all.
    Even if the compiler did have to do a copy, the move constructor will kick in, and the copy will typically be very cheap - two move operations. So don't worry about this. Return it as normal.
    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.

  4. #4
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    407
    The compiler can perform NRVO, thus preventing any copy at all.
    Thanks for that. I was not aware of this probably because I used an outdated compiler for quite to long. If the function accepted a vector class parameter would it still create a copy in memory for that case? Or are there optimization features for this as well?

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,437
    If you take a reference, then obviously no copy of the vector itself will be made (granted that you work with the vector gotten as a reference, and not a temporary copy).
    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.

  6. #6
    Registered User
    Join Date
    Oct 2012
    Posts
    30
    Quote Originally Posted by Elysia View Post
    This is typically not necessary.
    The compiler can perform NRVO, thus preventing any copy at all.
    Even if the compiler did have to do a copy, the move constructor will kick in, and the copy will typically be very cheap - two move operations. So don't worry about this. Return it as normal.
    How would I return it normally is my main question.

    Code:
    void notepad::retrieveButtons(){
        return &buttons;
    }
    As you can see, I'm not too sure how to return the buttons vector (declared in the class as: vector<sf::Text> buttons;); as a void I can't return anything, and I can't set the return type to vector or vector<sf::Text>.

    Any hints?
    Last edited by Justin H; 12-05-2012 at 03:29 PM.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,437
    Well, it would be something like

    Code:
    const std::vector<sf::Text>& notepad::RetrieveButtons()
    {
        return buttons;
    }
    if buttons is a member variable, or

    Code:
    std::vector<sf::Text> notepad::RetrieveButtons()
    {
    	sf::Text blah(...); // Initialize and do stuff with blah
    	return blah;
    }
    if your buttons is a local variable.

    If this doesn't work, then what is the error?
    It is possible that the object type (sf::Text) cannot be copied (and not moved), and if that is the case, the later may not work.
    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.

  8. #8
    Registered User
    Join Date
    Oct 2012
    Posts
    30
    Quote Originally Posted by Elysia View Post
    Well, it would be something like

    Code:
    const std::vector<sf::Text>& notepad::RetrieveButtons()
    {
        return buttons;
    }
    if buttons is a member variable, or

    Code:
    std::vector<sf::Text> notepad::RetrieveButtons()
    {
        sf::Text blah(...); // Initialize and do stuff with blah
        return blah;
    }
    if your buttons is a local variable.

    If this doesn't work, then what is the error?
    It is possible that the object type (sf::Text) cannot be copied (and not moved), and if that is the case, the later may not work.
    The first code you provided worked fine without any errors.

    However, may I ask why you moved the address symbol (&)? (from the return statement to the function type information)

    Thanks.
    Last edited by Justin H; 12-05-2012 at 04:06 PM.

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,437
    Quote Originally Posted by Justin H View Post
    However, may I ask why you moved the address symbol (&)? (from the return statement to the function type information)
    It's a reference. It's not the address symbol anymore.
    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.

  10. #10
    Registered User
    Join Date
    Apr 2006
    Posts
    2,021
    Quote Originally Posted by Justin H View Post
    However, may I ask why you moved the address symbol (&)? (from the return statement to the function type information)
    It's used to indicate that the return type is a reference. The code is returning a constant reference to the Text vector, instread of a copy. It can be used just like a copy, but it more efficent in case. It's not a pointer type, so you don't need to use the & operator to create it.

    Reference types are also useful for passing parameters to functions.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  11. #11
    Registered User
    Join Date
    Oct 2012
    Posts
    30
    Aha, thanks. This is a nice little feature.

    However, does this act just like a pointer? It's just not technically labeled as one?..

  12. #12
    Registered User
    Join Date
    Apr 2006
    Posts
    2,021
    It acts as a pointer in that if the data refered by a refence is be changed, and this change can be seen using the reference, and non-const references can make changes to the original.

    However, syntatically, it is used just like a copy.
    Justin H likes this.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  13. #13
    Registered User
    Join Date
    Oct 2012
    Posts
    30
    Quote Originally Posted by King Mir View Post
    It acts as a pointer in that if the data refered by a refence is be changed, and this change can be seen using the reference, and non-const references can make changes to the original.

    However, syntatically, it is used just like a copy.
    Ah, makes complete sense.

    Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 11-17-2011, 11:13 PM
  2. Function return type
    By nedim in forum C++ Programming
    Replies: 2
    Last Post: 09-27-2009, 12:58 PM
  3. function with no return type
    By abhi_86 in forum C Programming
    Replies: 12
    Last Post: 11-25-2006, 10:48 AM
  4. Replies: 6
    Last Post: 04-09-2006, 04:32 PM
  5. accessor function return type ptr to map
    By curlious in forum C++ Programming
    Replies: 2
    Last Post: 09-28-2003, 02:50 PM

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