Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. About default constructor
QtWS25 Last Chance

About default constructor

Scheduled Pinned Locked Moved C++ Gurus
10 Posts 9 Posters 9.0k Views
  • 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.
  • E Offline
    E Offline
    Edico
    wrote on last edited by
    #1

    @
    class A {
    public:
    A(int x = 0) { v = x; }
    private:
    int v;
    };
    @

    The definition for default constructor: "The constructor that takes no arguments is known as the default constructor."
    Is A(int x = 0); a default constructor?

    1 Reply Last reply
    0
    • H Offline
      H Offline
      HuXiKa
      wrote on last edited by
      #2

      As you just said, “The constructor that takes no arguments is known as the default constructor.”
      The A(int x = 0) constructor takes int x as an argument, so no, it isn't a default constructor. The default constructor would be
      @class A {
      public:
      A();
      ...
      @

      If you can find faults of spelling in the text above, you can keep them.

      1 Reply Last reply
      0
      • EddyE Offline
        EddyE Offline
        Eddy
        wrote on last edited by
        #3

        You can test it yourself. Put a debug in the constructor. Create an instance of A and see if your constructor is called or not.

        Qt Certified Specialist
        www.edalsolutions.be

        1 Reply Last reply
        0
        • L Offline
          L Offline
          lgeyer
          wrote on last edited by
          #4

          bq.
          "ISO/IEC 14882:2003, 12.1.5":http://cs.nyu.edu/courses/summer11/G22.2110-001/documents/c++2003std.pdf A default constructor for a class X is a constructor of class X that can be called without an argument. If there is no user-declared constructor for class X, a default constructor is implicitly declared.

          [quote author="Edico" date="1311779819"]Is A(int x = 0); a default constructor?[/quote]

          Yes.

          1 Reply Last reply
          0
          • G Offline
            G Offline
            giesbert
            wrote on last edited by
            #5

            It is a default constuictor as the argument is optional.

            Nokia Certified Qt Specialist.
            Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

            1 Reply Last reply
            0
            • D Offline
              D Offline
              dangelog
              wrote on last edited by
              #6

              [quote author="Edico" date="1311779819"]The definition for default constructor: "The constructor that takes no arguments is known as the default constructor."[/quote]

              The definition is wrong.

              http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.4

              Software Engineer
              KDAB (UK) Ltd., a KDAB Group company

              1 Reply Last reply
              0
              • C Offline
                C Offline
                Chuck Gao
                wrote on last edited by
                #7

                @A(int x = 0) @
                is a default constructor with the default argument.

                if @A(int x)@ without default argument, it's not a default constructor

                Chuck

                1 Reply Last reply
                0
                • EddyE Offline
                  EddyE Offline
                  Eddy
                  wrote on last edited by
                  #8

                  bq. You can test it yourself. Put a debug in the constructor. Create an instance of A and see if your constructor is called or not.

                  The practical meaning of a default constructor is that if you don't define one yourself the compiler is making one implicitly for you.

                  Have a look at this example :

                  @class A { public:
                  //A(int x = 0) { v = x; cout << " test my constructor";} //just for later use
                  private:
                  int v;
                  }; @

                  So for instance when you use class A in another class B, the default constructor, implicitly created by the compiler, will be called.

                  @class B { public:
                  private:
                  A a;
                  }; @

                  If you uncomment line 2 of class A, you will see the cout message, meaning your constructor is called with no parameter. Thus it's a default constructor. And you as a programmer took control over it.

                  In this example there is no practical need to define one since the result of both will be te same. Of course in real life things aren't that simple ;)

                  Qt Certified Specialist
                  www.edalsolutions.be

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    AlterX
                    wrote on last edited by
                    #9

                    it's just a defination...the default constructor is always one without parameters (included optional parameters)

                    Qt Ambassador
                    Real-time cooperative teams: http://www.softairrealfight.net
                    Free Real-time network platform sdk: https://github.com/AlterX76/Solomon

                    https://codereview.qt-project.org/...

                    1 Reply Last reply
                    0
                    • G Offline
                      G Offline
                      goetz
                      wrote on last edited by
                      #10

                      The C++ standard is very clear about what a default constructor is (section 12.1, number 5):

                      [quote]
                      A default constructor for a class X is a constructor of class X that can be called without an argument. If there is no user-declared constructor for class X, a default constructor is implicitly declared. [...]
                      [/quote]

                      http://www.catb.org/~esr/faqs/smart-questions.html

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved