CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 1 of 1
  1. #1
    Join Date
    Feb 2005
    "The Capital"

    C++ General: What members of a class are implicitly defined?

    Q: What members of a class are implicitly defined?

    A: There are quite a few things that you get when you make a class (if they aren't explicitly defined). The important thing to note is that they are not actually created/written by the compiler unless it's sure that you need them. Let's go ahead with an example empty class named 'MyClass'. Below are those members alongwith the constructs that force them into existence:
    • Default (zero-argument) constructor

      Whenever an object of the class is created unless there are any parameterized constructors defined.
      MyClass ObjectOfMyClass;   // Object creation
    • Copy constructor

      Whenever copy construction is initiated.
      MyClass ObjectOfMyClass;
      MyClass AnotherObjectOfMyClass = ObjectOfMyClass;   // Copy construction
    • Assignment operator

      Whenever the assignment operator '=' is used with objects of the class.
      MyClass ObjectOfMyClass;
      MyClass AnotherObjectOfMyClass;
      // some more code manipulating the objects
      ObjectOfMyClass = AnotherObjectOfMyClass;   // Assignment operator
    • Destructor

      If there is an explicitly/implicitly defined constructor (nothing to do with the keyword 'explicit') and an object is created. That would mean if you went through some code as stated in the first point, a destructor would be generated and called whenever the object is destroyed.

    • A pair of 'address-of' operators

      There are two versions: a 'const' one and a 'non-const' one. When the '&' ('address-of') operator is used on a const object, first version is generated and the later one associates itself with a non-const object with similar usage.
      MyClass ObjectOfMyClass;
      const MyClass ConstObjectOfMyClass;
      MyClass* ptr = &ObjectOfMyClass   // Causes non-const version creation
      ptr = &ConstObjectOfMyClass   // Causes const version creation

    If there are all the above mentioned operations in a program, the empty 'MyClass' would be somewhat equivalent to:
    class MyClass
        MyClass() {}
        MyClass(const MyClass& rhs) {}
        MyClass& operator=(const MyClass& rhs) {}
        MyClass* operator&() { return this; }
        const MyClass* operator&() const { return this; }
        ~MyClass() {}
    These will appear in all cases where you don't explicitly define them but are forcing their calls in the above mentioned ways. So just be aware.

    Note: If you define the copy constructor for your class, you don't get the default constructor.

    For more details you may refer to Scott Meyers - 'Effective C++ (2nd Edition)', Item 45 - "Know what functions C++ silently writes and calls".

    Last edited by Andreas Masur; September 25th, 2005 at 11:39 AM.

Posting Permissions

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

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.