Intializing the method's parameter list

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • seforo
    New Member
    • Nov 2006
    • 60

    Intializing the method's parameter list

    Hi Guys,

    What is the advantage of intializing the parameter list of the method, e.g
    [CODE=c++]
    class ReadFile
    {
    ReadFile(const char* filename = "Mystuff.tx t")
    {
    // Function defination goes here
    }
    // Define the rest of the class here
    }
    [/CODE]

    I am asking this because having that I still have to pass the file name when I instantiate the object of the class ReadFile, otherwise I get an error. However I have seen many people defining their classes like this.
  • ilikepython
    Recognized Expert Contributor
    • Feb 2007
    • 844

    #2
    Originally posted by seforo
    Hi Guys,

    What is the advantage of intializing the parameter list of the method, e.g
    [CODE=cpp]
    class ReadFile
    {
    ReadFile(const char* filename = "Mystuff.tx t")
    {
    // Function defination goes here
    }
    // Define the rest of the class here
    }
    [/CODE]

    I am asking this because having that I still have to pass the file name when I instantiate the object of the class ReadFile, otherwise I get an error. However I have seen many people defining their classes like this.
    That is a default arguement. If you don't pass it, it defaults to the one you gave it in the declaration or prototype. I think yours doesn't work is because you need something like this:
    [code=cpp]
    class ReadFile
    {
    ReadFile(const char filename[20] = "Mystuff.tx t");
    }

    ReadFile::ReadF ile(const char filename[20])
    {
    // you dont have to pass the filename
    }
    [/code]
    The default has to be in the declaration.

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      Originally posted by ilikepython
      I think yours doesn't work is because you need something like this:
      It should work fine. The argument is a const char* and a literal is a const. The onlt difference is that the function cannot use the file name in a non-const manner.

      Comment

      • seforo
        New Member
        • Nov 2006
        • 60

        #4
        Let me be specific; The following piece of code does not compile:
        [CODE=cpp]
        #include <iostream>
        using namespace std;
        class Test
        {
        public:
        Test(char * file = "galice.roo t");
        void Show()
        {
        }
        };

        Test::Test(char * file)
        {
        }

        int main()
        {
        Test teko();
        teko.Show();
        return 0;
        }
        [/CODE]
        Unless I pass the file name galice.root as an arguement to the object of the class Test that I instatiated, I always get the error message:
        request for member `Show' in `teko()', which is of non-aggregate type `Test ()()'

        Comment

        • ilikepython
          Recognized Expert Contributor
          • Feb 2007
          • 844

          #5
          Originally posted by seforo
          Let me be specific; The following piece of code does not compile:
          [CODE=cpp]
          #include <iostream>
          using namespace std;
          class Test
          {
          public:
          Test(char * file = "galice.roo t");
          void Show()
          {
          }
          };

          Test::Test(char * file)
          {
          }

          int main()
          {
          Test teko();
          teko.Show();
          return 0;
          }
          [/CODE]
          Unless I pass the file name galice.root as an arguement to the object of the class Test that I instatiated, I always get the error message:
          request for member `Show' in `teko()', which is of non-aggregate type `Test ()()'
          You need to leave off the parenthesis when creating the object with no arguements.

          Comment

          • seforo
            New Member
            • Nov 2006
            • 60

            #6
            Originally posted by ilikepython
            You need to leave off the parenthesis when creating the object with no arguements.
            Thank you, it works now

            Comment

            Working...