Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Default functions implemented by compiler

Reply
Thread Tools

Default functions implemented by compiler

 
 
sujilc@gmail.com
Guest
Posts: n/a
 
      06-29-2006
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?

 
Reply With Quote
 
 
 
 
Salt_Peter
Guest
Posts: n/a
 
      06-29-2006

http://www.velocityreviews.com/forums/(E-Mail Removed) 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

 
Reply With Quote
 
 
 
 
Marco Wahl
Guest
Posts: n/a
 
      06-29-2006
"Salt_Peter" <(E-Mail Removed)> writes:
> (E-Mail Removed) 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

 
Reply With Quote
 
Heinz Ozwirk
Guest
Posts: n/a
 
      06-29-2006
"Marco Wahl" <(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed) oups.com...
> "Salt_Peter" <(E-Mail Removed)> writes:
>> (E-Mail Removed) 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

 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      06-29-2006
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.

 
Reply With Quote
 
Marco
Guest
Posts: n/a
 
      06-29-2006
Hi

> "Salt_Peter" <(E-Mail Removed)> writes:
>> (E-Mail Removed) 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
 
Reply With Quote
 
Murali Krishna
Guest
Posts: n/a
 
      06-29-2006

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.

 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      06-29-2006
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.


 
Reply With Quote
 
Murali Krishna
Guest
Posts: n/a
 
      06-29-2006
Thanks for that.

-- Murali Krishna

 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      06-29-2006
* 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?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
CPP compiler that implemented the export keyword Aaron.Z.Yang@Hotmail.com C++ 1 07-06-2007 01:29 AM
compiler generated destructor vs class implemented destructor arun C++ 2 06-13-2006 05:43 AM
difference between compiler generated destructor and class implemented arun C++ 2 06-12-2006 09:39 PM
difference between compiler generated destructor and class implemented arun C++ 0 06-12-2006 05:34 PM
Why are the min and max values for a particular numeric type implemented as functions? Vijai Kalyan C++ 1 03-21-2006 02:39 PM



Advertisments