Default functions implemented by compiler

Discussion in 'C++' started by sujilc@gmail.com, Jun 29, 2006.

  1. Guest

    This question seems to be silly. Can ony one figure out the default
    functions implemented by compiler when we decalre a class like

    class A
    {

    }

    According to me this declaration will define default functions like

    1. Default Constructor
    2. Default Destrucor
    3. Copy Constructor

    Is there any other functions i am missing?
     
    , Jun 29, 2006
    #1
    1. Advertising

  2. Salt_Peter Guest

    wrote:
    > This question seems to be silly. Can ony one figure out the default
    > functions implemented by compiler when we decalre a class like
    >
    > class A
    > {
    >
    > }


    class A
    {
    };

    >
    > According to me this declaration will define default functions like
    >
    > 1. Default Constructor
    > 2. Default Destrucor
    > 3. Copy Constructor
    >
    > Is there any other functions i am missing?


    No
     
    Salt_Peter, Jun 29, 2006
    #2
    1. Advertising

  3. Marco Wahl Guest

    "Salt_Peter" <> writes:
    > wrote:
    >> This question seems to be silly. Can ony one figure out the default
    >> functions implemented by compiler when we decalre a class like

    >
    > class A
    > {
    > };
    >
    >> According to me this declaration will define default functions like
    >> 1. Default Constructor
    >> 2. Default Destrucor
    >> 3. Copy Constructor
    >>
    >> Is there any other functions i am missing?

    >
    > No


    I disagree!

    Scott Meyers writes in Effective C++ chapter 5 that
    also the copy assignment operator may be generated.

    Note that the listed functions are generated only when needed.


    Best wishes
     
    Marco Wahl, Jun 29, 2006
    #3
  4. Heinz Ozwirk Guest

    "Marco Wahl" <> schrieb im Newsbeitrag
    news:...
    > "Salt_Peter" <> writes:
    >> wrote:
    >>> This question seems to be silly. Can ony one figure out the default
    >>> functions implemented by compiler when we decalre a class like

    >>
    >> class A
    >> {
    >> };
    >>
    >>> According to me this declaration will define default functions like
    >>> 1. Default Constructor
    >>> 2. Default Destrucor
    >>> 3. Copy Constructor
    >>>
    >>> Is there any other functions i am missing?

    >>
    >> No

    >
    > I disagree!
    >
    > Scott Meyers writes in Effective C++ chapter 5 that
    > also the copy assignment operator may be generated.
    >
    > Note that the listed functions are generated only when needed.


    It should also be mentioned that there is no "default destructor". There
    only is a default implementation. A "default constructor" is any
    constructor, which can be called without any arguments. It is called
    "default" because it is used when no other constructor is explicitly
    specified when an object is created, not because its implementation is
    provided by the compiler.

    Heinz
     
    Heinz Ozwirk, Jun 29, 2006
    #4
  5. Rolf Magnus Guest

    Marco wrote:

    >> Scott Meyers writes in Effective C++ chapter 5 that
    >> also the copy assignment operator may be generated.
    >>

    >
    > That's right. An operator== function is always implemented by the compiler
    > if none exists.


    ITYM "operator="

    > This is always a bit-by-bit copy.


    No, it isn't. It's a memberwise copy.
     
    Rolf Magnus, Jun 29, 2006
    #5
  6. Marco Guest

    Hi

    > "Salt_Peter" <> writes:
    >> wrote:
    >>> This question seems to be silly. Can ony one figure out the default
    >>> functions implemented by compiler when we decalre a class like

    >>
    >> class A
    >> {
    >> };
    >>
    >>> According to me this declaration will define default functions like
    >>> 1. Default Constructor
    >>> 2. Default Destrucor
    >>> 3. Copy Constructor
    >>>
    >>> Is there any other functions i am missing?

    >>
    >> No

    >
    > I disagree!
    >
    > Scott Meyers writes in Effective C++ chapter 5 that
    > also the copy assignment operator may be generated.
    >


    That's right. An operator== function is always implemented by the compiler
    if none exists. This is always a bit-by-bit copy. Else this wouldn't be
    possible:

    void fun()
    {
    A a;
    A b;
    a = b;
    }

    Ciao,
    Marco
     
    Marco, Jun 29, 2006
    #6
  7. Rolf Magnus wrote:
    > Marco wrote:
    >
    > >> Scott Meyers writes in Effective C++ chapter 5 that
    > >> also the copy assignment operator may be generated.
    > >>

    > >
    > > That's right. An operator== function is always implemented by the compiler
    > > if none exists.

    >
    > ITYM "operator="
    >
    > > This is always a bit-by-bit copy.

    >
    > No, it isn't. It's a memberwise copy.


    yes I agree Rolf. I have a query.
    In an interview, they asked my friend about shallow and deep copy.
    he could not explain because we never heard about that.

    After that some one said..

    deep copy happens through default constructor
    and shallow copy is thru copy constructor. I think I did not match them
    correctly.
    plz correct which is deep and shallow.

    and also said..

    Copy constructor is dangerous. While programming with pointers, it may
    lead to exception.
    and shallow (copy constructor) will not have that problem.

    Plz tell about this.

    -- Murali Krishna.
     
    Murali Krishna, Jun 29, 2006
    #7
  8. Rolf Magnus Guest

    Murali Krishna wrote:

    >
    > Rolf Magnus wrote:
    >> Marco wrote:
    >>
    >> >> Scott Meyers writes in Effective C++ chapter 5 that
    >> >> also the copy assignment operator may be generated.
    >> >>
    >> >
    >> > That's right. An operator== function is always implemented by the
    >> > compiler if none exists.

    >>
    >> ITYM "operator="
    >>
    >> > This is always a bit-by-bit copy.

    >>
    >> No, it isn't. It's a memberwise copy.

    >
    > yes I agree Rolf. I have a query.
    > In an interview, they asked my friend about shallow and deep copy.
    > he could not explain because we never heard about that.
    >
    > After that some one said..
    >
    > deep copy happens through default constructor
    > and shallow copy is thru copy constructor. I think I did not match them
    > correctly.
    > plz correct which is deep and shallow.


    You are right that this definition is not correct.
    A deep copy is a copy where the whole content of the object is copied, while
    a shallow copy doesn't copy the data itself, but e.g. just its address, so
    that the original object and the copy share their data.
    The compiler-generated copy constructor does a deep copy. If you write your
    own, you can do anything you want. The default constructor has nothing to
    do with it.

    > and also said..
    >
    > Copy constructor is dangerous. While programming with pointers, it may
    > lead to exception.


    Well, if you have several objects that have a pointer to the same data, you
    have to ensure that it's not deleted multiple times or used after being
    deleted. However, failure to handle it correctly most often leads to
    undefined behavior. An exception might be thrown, but typically isn't.

    > and shallow (copy constructor) will not have that problem.


    Actually, it's just the shallow copy that has that problem, because it
    usually means that after the copy operation, two objects have a pointer to
    the same data. A deep copy is unproblematic in that respect, but might be
    costly for large amounts of data.
     
    Rolf Magnus, Jun 29, 2006
    #8
  9. Thanks for that.

    -- Murali Krishna
     
    Murali Krishna, Jun 29, 2006
    #9
  10. * Rolf Magnus:
    > The compiler-generated copy constructor does a deep copy.


    You mean, a shallow copy.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Jun 29, 2006
    #10
  11. Rolf Magnus Guest

    Alf P. Steinbach wrote:

    > * Rolf Magnus:
    >> The compiler-generated copy constructor does a deep copy.

    >
    > You mean, a shallow copy.


    I acutally didn't, but I guess it depends on the point of view. The
    compiler-generated copy constructor will just copy all the members. If a
    member is a pointer, it won't copy what that pointer points to. However, if
    you have such a case, you usually need a user-defined constructor to either
    do a deep or shallow copy. If you don't have pointer members, the
    compiler-generated one may be sufficent. Then it will copy the whole object
    content.

    I guess in the end, it's best to separate the concept of constructors from
    the concept of deep/shallow copy. One is part of the language, the other is
    rather a design concept.
     
    Rolf Magnus, Jun 29, 2006
    #11
  12. Sekhar Guest

    Alf P. Steinbach wrote:
    > * Rolf Magnus:
    > > The compiler-generated copy constructor does a deep copy.

    >
    > You mean, a shallow copy.
    >
    > --
    > A: Because it messes up the order in which people normally read text.
    > Q: Why is it such a bad thing?
    > A: Top-posting.
    > Q: What is the most annoying thing on usenet and in e-mail?


    Both shallow copy and deep copy copies all of the member values and the
    difference lies in the case of fields which are dynamically allocated.A
    shallow copy will only copy the pointer for dynamically allocated
    memory. ie for dynamically allocated fields both the both points to the
    same location.
    A deep copy copies all fields, and makes copies of dynamically
    allocated memory pointed to by the fields.

    The default copy constructor and assignment operator make shallow
    copies whereas for making a deep copy we have to write copy constructor
    and overload the assignment operator.
     
    Sekhar, Jun 29, 2006
    #12
  13. * Sekhar:
    > [quoting signature]


    Please don't quote signatures. Even in Google there should be some way
    to automatically remove signatures. I recommend using a real
    newsreader... ;-)

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Jun 29, 2006
    #13
  14. Murali Krishna posted:


    > In an interview, they asked my friend about shallow and deep copy.



    Pre-requisites:

    (1) An original object which to copy.
    (2) A suitably aligned memory buffer.


    #include <string>

    int main()
    {
    std::string original;

    unsigned char *p_buf = new unsigned char[sizeof(std::string)];

    /* Copying goes here */


    /* Time for clean-up */

    delete [] p_buf;
    }


    Here's a shallow copy:


    #include <string>
    #include <cstring>

    int main()
    {
    std::string original;

    unsigned char *p_buf = new unsigned char[sizeof(std::string)];

    /* Copying goes here */

    std::memcpy(p_buf, &original, sizeof original);


    /* Time for clean-up */

    delete [] p_buf;
    }


    And here's a deep copy:


    #include <string>

    int main()
    {
    std::string original;

    unsigned char *p_buf = new unsigned char[sizeof(std::string)];

    /* Copying goes here */

    new(p_buf) std::string(original);


    /* Time for clean-up */

    typedef std::string string;

    reinterpret_cast<std::string*>(p_buf)->~string();

    delete [] p_buf;
    }



    --

    Frederick Gotham
     
    Frederick Gotham, Jun 29, 2006
    #14
  15. Default User Guest

    Alf P. Steinbach wrote:

    > * Sekhar:
    > > [quoting signature]

    >
    > Please don't quote signatures. Even in Google there should be some
    > way to automatically remove signatures. I recommend using a real
    > newsreader... ;-)


    Whether there should be or not, there isn't. However, manual snippage
    works just fine.




    Brian
     
    Default User, Jun 29, 2006
    #15
  16. wrote:

    > This question seems to be silly. Can ony one figure out the default
    > functions implemented by compiler when we decalre a class like
    >
    > class A
    > {
    >
    > }


    They are not called "default" functions. They are called "implicitly
    declared/implicitly defined" functions. The term "default" in this
    context means something completely different from what you think it
    means (see other replies).

    And the term "implemented" is also unclear. They are not "implemented".
    They are "declared" and, if necessary, "defined" implicitly by the compiler.

    For a class 'A' defined as above, the compiler will implicitly _declare_
    default constructor 'A::A()', copy constructor 'A::A(const A&)', copy
    assignment operator 'A& A::eek:perator=(const A&)' and destructor 'A::~A()'.

    The compiler will implicitly _define_ the aforementioned functions only
    if they are used in the program. They are not used in your program. More
    precisely, I don't see any program at all, therefore there's no way to
    say whether they will be defined or not.

    > According to me this declaration will define default functions like
    >
    > 1. Default Constructor
    > 2. Default Destrucor
    > 3. Copy Constructor
    >
    > Is there any other functions i am missing?


    No. See above.

    --
    Best regards,
    Andrey Tarasevich
     
    Andrey Tarasevich, Jun 30, 2006
    #16
  17. Andrey Tarasevich wrote:
    > They are not called "default" functions. They are called "implicitly
    > declared/implicitly defined" functions.


    OK there are function declared/implemented by default or what ever..
    We get them just after creating a class. I think it is the same case in
    java.

    > More precisely, I don't see any program at all, therefore there's no way to
    > say whether they will be defined or not.


    as in other thread.. (plz dont correct me if it is not a thread)
    http://groups.google.co.in/group/co...cb4f3405d69/ba0c0fb2b18d6f68#ba0c0fb2b18d6f68

    Tom Widmer wrote:
    > void Foo_f(Foo* this)
    > {
    > printf("%d\n", this->i);
    > }


    Foo is a class

    > Conceptually, the object you are calling the member function on is
    > passed as a hidden parameter to the function, named "this".


    > So see that C++ classes and member functions are really just "syntatic
    > sugar" for C structs and normal functions


    that's excellent understanding.

    OK my query is..

    If that is the case for 'this', what is it for other default/implicitly
    implemented functions?

    will a default constructor be generated by compiler with C code?
    and the same for destructor and '=' operator?

    what is the implementation for those functions?

    Plz expalin.
     
    Murali Krishna, Jun 30, 2006
    #17
  18. Murali Krishna wrote:
    >> They are not called "default" functions. They are called "implicitly
    >> declared/implicitly defined" functions.

    >
    > OK there are function declared/implemented by default or what ever..
    > We get them just after creating a class. I think it is the same case in
    > java.


    It would be "what ever" if "default" was just an English word. But in
    this context it is not just a word. In C++ terminology it is a term with
    concrete meaning.

    > Tom Widmer wrote:
    >> Conceptually, the object you are calling the member function on is
    >> passed as a hidden parameter to the function, named "this".

    > ...
    > If that is the case for 'this', what is it for other default/implicitly
    > implemented functions?


    Hmm... You wording is unclear. 'this' is not a function. As Tom
    explained, 'this' is a name of an implicit parameter present in every
    non-static member function of a class. All non-static member functions
    have it. Whether they are implicitly defined or user-defined makes no
    difference.

    > will a default constructor be generated by compiler with C code?
    > and the same for destructor and '=' operator?


    C code? What exactly do you mean by "C code" in this case? There's no
    such thing as "classes" or "member functions" in C language.

    > what is the implementation for those functions?


    They just recursively perform the same operations on the subobjects of
    the entire object (bases and data members). For example, copy
    constructor will try to copy-initialize all subobjects of the object.

    --
    Best regards,
    Andrey Tarasevich
     
    Andrey Tarasevich, Jun 30, 2006
    #18
  19. Hi Andrey,

    Thanks for your answers.

    Andrey Tarasevich wrote:
    > It would be "what ever" if "default" was just an English word. But in
    > this context it is not just a word. In C++ terminology it is a term with
    > concrete meaning.


    I agree that they are not 'default' but we are used to that.

    > Hmm... You wording is unclear. 'this' is not a function. As Tom
    > explained, 'this' is a name of an implicit parameter present in every
    > non-static member function of a class. All non-static member functions
    > have it. Whether they are implicitly defined or user-defined makes no
    > difference.
    > ..


    Ok I came to know about one more point. Thanks for that.

    > > will a default constructor be generated by compiler with C code?
    > > and the same for destructor and '=' operator?

    >
    > C code? What exactly do you mean by "C code" in this case? There's no
    > such thing as "classes" or "member functions" in C language.


    I read in that thread..

    > earlier versions of C++ compilers first converted C++ code into C code.


    I dont think it will happen in present C++ compilers.
    I just wanted to understand how the compiler implements those functions
    after just declaring a class.

    Thanks any way.

    -- Murali Krishna
     
    Murali Krishna, Jul 1, 2006
    #19
  20. Sekhar Guest

    > #include <string>

    > #include <string>
    >
    > int main()
    > {
    > std::string original;
    >
    > unsigned char *p_buf = new unsigned char[sizeof(std::string)];
    >
    > /* Copying goes here */
    >
    > new(p_buf) std::string(original);


    Thanks for making concepts more clear. This code snippet gives me
    more thoughts on new operator. Can you just breif on how new operator
    works on the above statement.
     
    Sekhar, Jul 3, 2006
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Vijai Kalyan
    Replies:
    1
    Views:
    324
    Howard Hinnant
    Mar 21, 2006
  2. arun
    Replies:
    2
    Views:
    314
    n2xssvv g02gfr12930
    Jun 12, 2006
  3. arun
    Replies:
    0
    Views:
    322
  4. arun
    Replies:
    2
    Views:
    577
    benben
    Jun 13, 2006
  5. Replies:
    1
    Views:
    340
    Robert Bauck Hamar
    Jul 6, 2007
Loading...

Share This Page