CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    May 1999
    Posts
    92

    Default Constructor

    How do you add parameters to a default constructor of a class?


  2. #2
    Join Date
    Apr 1999
    Location
    Beaverton, OR
    Posts
    241

    Re: Default Constructor

    You declare the parameters as you would any other function, except that the constructor may NOT have a return type.

    //In the header file...
    class TestParams
    {
    public:
    TestParams(int a, int b); //Constructor
    .
    .
    .
    };

    In the .cpp file:

    #include "TestParams.h"
    TestParams::TestParams(int a, int b)
    {
    .
    .
    .
    }

    And to use the variable:

    TestParams tp(4, 5);

    Hope that was what you were looking for.


  3. #3
    Join Date
    Apr 1999
    Posts
    383

    Re: Default Constructor

    A default constructor is one that can be called without passing any arguments.

    If you want to add arguments and still have it as the default constructor, each argument must have a default value:

    MyClass(int arg1 = 3, string str1 = "hello");

    Any other user-defined constructor taking arguments will hide the compiler-provided default constructor, which must be explicitly re-introduced if required.

    Dave


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured