why are constructors evil ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pbgcs
    New Member
    • Aug 2008
    • 3

    why are constructors evil ?

    I just downloaded chrome source code and saw the following macros

    // A macro to disallow the copy constructor and operator= functions
    // This should be used in the private: declarations for a class
    #define DISALLOW_COPY_A ND_ASSIGN(TypeN ame) \
    TypeName(const TypeName&); \
    void operator=(const TypeName&)

    // An older, deprecated, politically incorrect name for the above.
    #define DISALLOW_EVIL_C ONSTRUCTORS(Typ eName) DISALLOW_COPY_A ND_ASSIGN(TypeN ame)


    DISALLOW_EVIL_C ONSTRUCTORS is used in all the classes in private section .
    I know it is trying to disallow default copy and assignment constructors.

    But can anyone tell me why they are treated as evil ?
    I have searched in google and i see some people really hate constructors
    to as much as to say real object oriented languages dont have constructors .

    Can someone can explain this ?

    Thanks a lot for your help !!
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    One reason you disallow a copy constructor is to prevent other programmers from using objects of your class as function arguments. It forces them to use either a reference or a pointer.

    Unfortunately, you cannot disallow constrcutors or your objects will not be properly initialized. Remember, your constructor is called after the constructors on your data members have been called.

    Likewise, your assignment operator is called only after the assignment operator has been called on all of your data members.

    Now lets assume you have poor design, say with many levels of inheritance. When you need to make a copy of an object at the lowest level, that may involve dozens of other constructor calls before yours ever gets called. Ditto for the assignment operator.

    This is called a deep-copy. It is always safe.

    The counterpart is shallow copy where you only copy the members of your class. That is, you just copy the pointer instead of making a copy of the data the pointer points at. This is almost always a bad idea becuse your object is not a real copy. It still points to the same data pointed at by the original obect. Whichever of these objects deletes the pointer first trashes the other object.

    The cure for this is better design.

    Microsoft's MFC was so poorly designed (over 400 virtual methods in one class!) that the vtables became enormous. That coupled with 9 levels of inheritance caused the developers to disable virtual functions because of memory requirements for all these tables. Intstead, they invented a large macro called a MESSAGE_MAP and you got to hook up your classes by hand. It was now a feature.

    These are all techniques used to make a poor design workable.

    Also do not overlook control. Some programmers must make all of the calls themselves. None of this implicit call stuff. These are mostly C programmers who can't switch to C++. I see them all the time in my C++ classes. Once, when one of these programmers understood that the compiler was making calls not in the source code asked me how to shut it off. I said you couldn't really shut it off. Then I was told that C++ was useless and the programmer got up and left the classroom never to be seen again.

    Read some of Bjarne Stroustrup's interviews. He's run into the same thing.

    As to real object-oriented languages not having constrcutors, remember that C++ is not an object-oriented language. It is a structured language, like the C it is the replacement for, but with "object-oriented" features. In the object oriented world, creating an object also covers automatic initialization. No way to to that in C without calling a function, so in C++ the call of the constructor function was made implicit to mimic a real object feature - but it's still creating a variable and calling a function to initialize it.

    C++ also does not support the Liskow Substitution Principle so it can never by truly object oriented. The most you can do is use a base class pointer or reference to call a derived class method but you cannot actually substitute a derived object for a base object.

    Comment

    • pbgcs
      New Member
      • Aug 2008
      • 3

      #3
      Hi,
      Thanks a lot! Your post is really helpful in improving my understanding. I understood now that it is so as it can be passed only as a reference/pointer .Also we are assuming that once an instance is created we wont be making a copy of it right. Sorry im just messed up but i think i have read about some design pattern ( its been some time i read so i forgot) says having clone() to create an exact replica of the object. Is it the correct way to make another copy instead of a copy constructor ? can you tell me the difference between the clone() and copy constructor ? Is it better way to always avoid copy constructor and assignment operator and use always clone ?

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        Originally posted by weaknessforcats
        Likewise, your assignment operator is called only after the assignment operator has been called on all of your data members.
        Hi weaknessforcats , can you just clarify this for me please?

        You say that when the assignment operator for a class is called the assignment operators for all its data members are called first.

        Is this true for just the default assignment operator or is that also true if you explicitly override the assignment operator?

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by Banfa
          Hi weaknessforcats , can you just clarify this for me please?

          You say that when the assignment operator for a class is called the assignment operators for all its data members are called first.

          Is this true for just the default assignment operator or is that also true if you explicitly override the assignment operator?
          I'm not WFC but nevertheless: only the default assignment operator does this;
          when you user define it, you're free to do whatever you want to do.

          kind regards,

          Jos

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            Originally posted by JosAH
            I'm not WFC but nevertheless: only the default assignment operator does this;
            when you user define it, you're free to do whatever you want to do.
            Yeah. That's what I mean't to say. Sorry I got carried away.

            Comment

            • pbgcs
              New Member
              • Aug 2008
              • 3

              #7
              Originally posted by weaknessforcats
              Yeah. That's what I mean't to say. Sorry I got carried away.
              Hi , can you answer my last question ? Thanks!

              Comment

              • weaknessforcats
                Recognized Expert Expert
                • Mar 2007
                • 9214

                #8
                A clone is typically a deep copy. That is, it does what a copy constructor should do. These things are used when the copy constructor is a)disabled, or b) short-circuited to do a shallow copy or c) private.

                That is, the clone method usually calls the copy constrcutor (which is probably private). A clone method is sometimes called a virtual constrcutor.

                If this is done in an inheritance hierarchy, covariance comes into play.

                Have a look at The C++ Programming Language 3rd Ed. by Bjarne Stroustrup Section 15.6.2.

                Comment

                Working...