Default functions implemented by compiler

S

sujilc

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?
 
S

Salt_Peter

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
 
M

Marco Wahl

Salt_Peter said:
class A
{
};


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
 
H

Heinz Ozwirk

Marco Wahl said:
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
 
M

Marco

Hi
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
 
M

Murali Krishna

Rolf said:
ITYM "operator="


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.
 
R

Rolf Magnus

Murali said:
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.
 
R

Rolf Magnus

Alf said:
* Rolf Magnus:

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.
 
S

Sekhar

Alf said:
* Rolf Magnus:

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.
 
A

Alf P. Steinbach

* 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... ;-)
 
F

Frederick Gotham

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;
}
 
D

Default User

Alf said:
* 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
 
A

Andrey Tarasevich

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.
 
M

Murali Krishna

Andrey said:
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 said:
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.
 
A

Andrey Tarasevich

Murali said:
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.
...
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.
 
M

Murali Krishna

Hi Andrey,

Thanks for your answers.

Andrey said:
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.
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
 
S

Sekhar

#include said:
#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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top