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
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Default constructors...

Default constructors...

Scheduled Pinned Locked Moved C / C++ / MFC
c++
5 Posts 5 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.
  • P Offline
    P Offline
    p_1960
    wrote on last edited by
    #1

    Hi, May i know what are default constructors generated in c++ when an object is created... and what happens if i declare my own constructor without declaring the default constructor.. pls correct me if im wrong..

    _ A 2 Replies Last reply
    0
    • P p_1960

      Hi, May i know what are default constructors generated in c++ when an object is created... and what happens if i declare my own constructor without declaring the default constructor.. pls correct me if im wrong..

      _ Offline
      _ Offline
      _Superman_
      wrote on last edited by
      #2

      By default C++ generates a default constructor and a copy constructor for a class. If you define any of your own constructors, no other constructor will be generated by the compiler. The auto generated default constructor will initialize all class data members to 0. The auto generated copy constructor will do a bit by bit copy of all data members from one instance to the other.

      «_Superman_» I love work. It gives me something to do between weekends.

      A M 2 Replies Last reply
      0
      • P p_1960

        Hi, May i know what are default constructors generated in c++ when an object is created... and what happens if i declare my own constructor without declaring the default constructor.. pls correct me if im wrong..

        A Offline
        A Offline
        Adam Roderick J
        wrote on last edited by
        #3

        In addition to above points from superman, i will say always declare and define your own default constructors when your class is having pointer members. Along with default constructor, make your own copy constructor and operator = in that case. It is always good to write your own default constructor so that you can initialize your member variable in default constructor.

        Величие не Бога может быть недооценена.

        1 Reply Last reply
        0
        • _ _Superman_

          By default C++ generates a default constructor and a copy constructor for a class. If you define any of your own constructors, no other constructor will be generated by the compiler. The auto generated default constructor will initialize all class data members to 0. The auto generated copy constructor will do a bit by bit copy of all data members from one instance to the other.

          «_Superman_» I love work. It gives me something to do between weekends.

          A Offline
          A Offline
          amatecki
          wrote on last edited by
          #4

          «_Superman_» wrote:

          The auto generated default constructor will initialize all class data members to 0.

          Are You sure? I think it may be behavior of specific compiler You use.

          «_Superman_» wrote:

          The auto generated copy constructor will do a bit by bit copy of all data members from one instance to the other.

          It's true rather for members of built-in types. For members of user defined types, straightway, is used method of constructing member objects with the help of their constructors.

          1 Reply Last reply
          0
          • _ _Superman_

            By default C++ generates a default constructor and a copy constructor for a class. If you define any of your own constructors, no other constructor will be generated by the compiler. The auto generated default constructor will initialize all class data members to 0. The auto generated copy constructor will do a bit by bit copy of all data members from one instance to the other.

            «_Superman_» I love work. It gives me something to do between weekends.

            M Offline
            M Offline
            Michael Dunn
            wrote on last edited by
            #5

            «_Superman_» wrote:

            The auto generated default constructor will initialize all class data members to 0.

            This is incorrect. If your class has built-in data types (int, char, etc.), the default behavior is to leave them uninitialized, just like local variables.

            --Mike-- Dunder-Mifflin, this is Pam.

            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