Thread: Thanks again....

  1. #16
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Yeah if you want to change the parameter inside the function and not alter the value in the calling function then you would pass by value. If you don't change the parameter inside the function and want to insure that the value in the calling function remains the same the call by const reference.


    Jim

  2. #17
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by jimblumberg View Post
    Yeah if you want to change the parameter inside the function and not alter the value in the calling function then you would pass by value. If you don't change the parameter inside the function and want to insure that the value in the calling function remains the same the call by const reference.


    Jim
    Interesting... if you note I do change the value of Folder in the GetFiles() member... That's why I passed by value... to protect Folder in the constructor, which likely will get changed to something more complex as I improve the Class. The original value is preserved in Path.

    I thought I was protecting the value-integrity of the strings... was that a mistake?

  3. #18
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    what do those main parameters mean?
    int argv,char* argc[]
    You ended that sentence with a preposition...Bastard!

  4. #19
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Since you are changing Folder inside your function.

    If you don't want the value changed in the calling function then you should pass by value.

    If you want to also change the variable in the calling function then pass by reference.

    If you don't want to allow any changes to Folder in the function then pass by const reference.

    The reason for passing by reference or const reference is to avoid the call to the copy constructor.

    The reasons for calling by value, or reference are the same as any POD variable. Pass by value when changing the value in the function is not to alter the original, and pass by reference when changes should change the original.

    The use of const reference is to avoid the copy constructor when possible.

    In your GetFiles() member function pass by value is probably correct. In your DirList() member function pass by const reference is probably correct.

    Jim

  5. #20
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Quote Originally Posted by Eman View Post
    what do those main parameters mean?
    int argv,char* argc[]
    You can name the parameters to main anything you like, although as stated in an earlier post, the common usage is int argc, char **argv.

    Jim

  6. #21
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    yeah, but what does it take, a number and an array of pointers..
    what values can you pass?
    and how can it be passed. Since the program calls main, not the developer
    You ended that sentence with a preposition...Bastard!

  7. #22
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Eman View Post
    what do those main parameters mean?
    int argv,char* argc[]
    They are arguments passed in on the command line when the program is run...

    If I type: DirToFile -R test.txt "c:\Program Files\"

    argv will = 4, meaning there are 4 items on the command line
    argc[] is an array of string pointers with one pointer to each of the items
    argc[0] will be the program's file name
    argc[1] will be the -R
    argc[2] points to test.txt
    argc[3] will be "c:\Program Files" ... quoted because of the space.

    These are variables you can use inside your program. Console programs would be useless without them.

    (NOTE ... as Salem pointed out I misnamed them... the convention is to use argc for the first parameter and argv for the second one... However you could name them fred and barney for all it really matters.)

  8. #23
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Please see this link that explains the arguments to main far better than I can: Arguments to main



    Jim

  9. #24
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by jimblumberg View Post
    Since you are changing Folder inside your function.
    If you don't want the value changed in the calling function then you should pass by value.
    If you want to also change the variable in the calling function then pass by reference.
    If you don't want to allow any changes to Folder in the function then pass by const reference.
    The reason for passing by reference or const reference is to avoid the call to the copy constructor.
    The reasons for calling by value, or reference are the same as any POD variable. Pass by value when changing the value in the function is not to alter the original, and pass by reference when changes should change the original.
    The use of const reference is to avoid the copy constructor when possible.
    In your GetFiles() member function pass by value is probably correct. In your DirList() member function pass by const reference is probably correct.

    Jim
    Hey... now that makes sense. I had read the explanation in "Thinking in C++" and frankly the guy had my head spinning... Thanks.

  10. #25
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Eman View Post
    yeah, but what does it take, a number and an array of pointers..
    what values can you pass?
    and how can it be passed. Since the program calls main, not the developer
    Ok the int parameter is the number of parameters... limited to the range of an int, and that's a lot.

    The char* array values are strings...

    The important thing to know is that on entry the command line is tokenized (probably using strtok() ) on spaces... so if you have any strings with spaces in them you have to put them in quotes so it will load the whole thing on a single pointer, then you're going to have to remove the quotes inside your software PathUnquoteStrings() is real handy for that... it's part of Windows shlwapi.lib and shlwapi.h.

    Windows limits it's ascii command line to 260 characters, including the program name. In Unicode the limit is something like 32k ... other OSs have different rules.

  11. #26
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    The important thing to know is that on entry the command line is tokenized (probably using strtok() ) on spaces... so if you have any strings with spaces in them you have to put them in quotes so it will load the whole thing on a single pointer, then you're going to have to remove the quotes inside your software
    I'm not sure about Windows but on Linux the quotes should be removed by the command processor.

    Also note that argc == number of parameters. and argv are the parameters, with argv[0] being the name of the program.

    Jim

  12. #27
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by jimblumberg View Post
    I'm not sure about Windows but on Linux the quotes should be removed by the command processor.

    Also note that argc == number of parameters. and argv are the parameters, with argv[0] being the name of the program.

    Jim
    In windows you get to dequote it yourself. There are actually cases where you want a pathname quoted anyway... such as when you're handing it directly to ShellExecute().

  13. #28
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    When compiling with MinGW on Windows, the quotes are removed. Visual Studio uses a different runtime library that interacts with the command line in a different way and I have no idea if that strips quotes or not.

  14. #29
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by tabstop View Post
    When compiling with MinGW on Windows, the quotes are removed. Visual Studio uses a different runtime library that interacts with the command line in a different way and I have no idea if that strips quotes or not.
    It could be compiler dependent... but, in this case, what's the harm? It's a one time waste of a couple of milliseconds to play it safe...

  15. #30
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Thanks..one question
    how is
    char** equivalent to char *argv[]
    char *argv[] makes sense an array of pointers to strings..
    char** is a pointer to a pointer to a string. So how can it point to each string in the parameter if used in place of char *argv[].

    Is it Linux you're using. DirToFile and not DirToFile.exe? And I thought commandline windows ignored space.
    You ended that sentence with a preposition...Bastard!

Popular pages Recent additions subscribe to a feed