Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Incredible functions... may exist ?

Incredible functions... may exist ?

Scheduled Pinned Locked Moved C / C++ / MFC
comquestion
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Rafael Fernandez Lopez
    wrote on last edited by
    #1

    We can create functions that return values like : CString my_function() { CString my_string="whatever"; return my_string; } BUT I NEED A FUNCTION THAT MAY BE ABLE TO RETURN A CStringArray OBJECT OR SOMETHING SIMILAR WITH SOME DATA !! I mean, something like this: CStringArray my_function() { CStringArray my_array; my_array.Add("VALUE1"); my_array.Add("VALUE2"); return my_array; } THANKX !!!!


    Written by: Rafael Fernández López.

    Visit: http://www.maestroprogramador.com

    J 1 Reply Last reply
    0
    • R Rafael Fernandez Lopez

      We can create functions that return values like : CString my_function() { CString my_string="whatever"; return my_string; } BUT I NEED A FUNCTION THAT MAY BE ABLE TO RETURN A CStringArray OBJECT OR SOMETHING SIMILAR WITH SOME DATA !! I mean, something like this: CStringArray my_function() { CStringArray my_array; my_array.Add("VALUE1"); my_array.Add("VALUE2"); return my_array; } THANKX !!!!


      Written by: Rafael Fernández López.

      Visit: http://www.maestroprogramador.com

      J Offline
      J Offline
      Joe Woodbury
      wrote on last edited by
      #2

      If an object has a copy constructor you can do it. It might even be optimized to not be a performance nightmare. For anything but a trivial return value, I prefer passing a reference to the object to be "returned." Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke

      R 1 Reply Last reply
      0
      • J Joe Woodbury

        If an object has a copy constructor you can do it. It might even be optimized to not be a performance nightmare. For anything but a trivial return value, I prefer passing a reference to the object to be "returned." Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke

        R Offline
        R Offline
        Rafael Fernandez Lopez
        wrote on last edited by
        #3

        Could you show me an example please ?? I'm really thanked... ;)


        Written by: Rafael Fernández López.

        Visit: http://www.maestroprogramador.com

        J 1 Reply Last reply
        0
        • R Rafael Fernandez Lopez

          Could you show me an example please ?? I'm really thanked... ;)


          Written by: Rafael Fernández López.

          Visit: http://www.maestroprogramador.com

          J Offline
          J Offline
          Joe Woodbury
          wrote on last edited by
          #4

          void my_function(CStringArray& my_array) { my_array.Add("VALUE1"); my_array.Add("VALUE2"); } Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke

          R 1 Reply Last reply
          0
          • J Joe Woodbury

            void my_function(CStringArray& my_array) { my_array.Add("VALUE1"); my_array.Add("VALUE2"); } Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke

            R Offline
            R Offline
            Rafael Fernandez Lopez
            wrote on last edited by
            #5

            well, and declare my_array as public ?


            Written by: Rafael Fernández López.

            Visit: http://www.maestroprogramador.com

            C J 2 Replies Last reply
            0
            • R Rafael Fernandez Lopez

              well, and declare my_array as public ?


              Written by: Rafael Fernández López.

              Visit: http://www.maestroprogramador.com

              C Offline
              C Offline
              Christian Graus
              wrote on last edited by
              #6

              What do you mean ? You pass the array into the function, but you pass it as a reference ( which means you put a & between the type and the variable name ). That means that any changes to the variable within the function also occur to the object outside the function, it's a bit like a pointer, except you have the object, not it's address. Another point - you should use std::vector over CStringArray unless you have pressing reason to use crappy MFC containers. Christian I have drunk the cool-aid and found it wan and bitter. - Chris Maunder

              1 Reply Last reply
              0
              • R Rafael Fernandez Lopez

                well, and declare my_array as public ?


                Written by: Rafael Fernández López.

                Visit: http://www.maestroprogramador.com

                J Offline
                J Offline
                Joe Woodbury
                wrote on last edited by
                #7

                What Christian said except about std::vector. I like CStringArray. Works just fine and has less weirdness than vector. Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke

                R 1 Reply Last reply
                0
                • J Joe Woodbury

                  What Christian said except about std::vector. I like CStringArray. Works just fine and has less weirdness than vector. Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke

                  R Offline
                  R Offline
                  Rafael Fernandez Lopez
                  wrote on last edited by
                  #8

                  Yes it was said to pass a reference. I mean, to modify a public CStringArray object from a function. That's a good idea !! Thanx for everything !!


                  Written by: Rafael Fernández López.

                  Visit: http://www.maestroprogramador.com

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups