By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,804 Members | 1,634 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,804 IT Pros & Developers. It's quick & easy.

Default functions implemented by compiler

P: n/a
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 '06 #1
Share this Question
Share on Google+
22 Replies


P: n/a

su****@gmail.com 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

Jun 29 '06 #2

P: n/a
"Salt_Peter" <pj*****@yahoo.com> writes:
su****@gmail.com 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

Jun 29 '06 #3

P: n/a
"Marco Wahl" <ma********@gmail.com> schrieb im Newsbeitrag
news:11**********************@x69g2000cwx.googlegr oups.com...
"Salt_Peter" <pj*****@yahoo.com> writes:
su****@gmail.com 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

Jun 29 '06 #4

P: n/a
Hi
"Salt_Peter" <pj*****@yahoo.com> writes:
su****@gmail.com 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
Jun 29 '06 #5

P: n/a
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.

Jun 29 '06 #6

P: n/a

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.

Jun 29 '06 #7

P: n/a
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.
Jun 29 '06 #8

P: n/a
Thanks for that.

-- Murali Krishna

Jun 29 '06 #9

P: n/a
* 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?
Jun 29 '06 #10

P: n/a
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.

Jun 29 '06 #11

P: n/a
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.

Jun 29 '06 #12

P: n/a
* 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?
Jun 29 '06 #13

P: n/a
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
Jun 29 '06 #14

P: n/a
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
Jun 29 '06 #15

P: n/a
su****@gmail.com 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::operator=(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
Jun 29 '06 #16

P: n/a
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/com...0c0fb2b18d6f68

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.

Jun 30 '06 #17

P: n/a
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
Jun 30 '06 #18

P: n/a
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

Jul 1 '06 #19

P: n/a
#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.

Jul 3 '06 #20

P: n/a
Sekhar posted:

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

(I'm going to use "malloc" in this example just to try to make it easier
to understand.)
Here is a normal use of "new":
Type *p = new Type;
This does two things:

(1) It allocates memory.
(2) It constructs an object at that location in memory.
Here is a special use of "new" called "placement new":
Type *p = malloc( sizeof(Type) ); /* This only allocates memory */
::new(p) Type; /* This constructs an object at the address
specified by "p" */

For info on "placement new", try this:

http://www.parashift.com/c++-faq-lit...html#faq-11.10

--

Frederick Gotham
Jul 3 '06 #21

P: n/a
In article <jG*******************@news.indigo.ie>, fg*******@SPAM.com
says...

[ ... ]
Type *p = malloc( sizeof(Type) ); /* This only allocates memory */
While perhaps reasonable for demo code, for anybody being really
pedantic, this doesn't allocate memory either -- it just causes a
compiler error. Of course, malloc returns a 'void *' and unlike in C,
you can't assign that to a 'Type *' without a cast:

Type *p = static_cast<Type *>malloc(sizeof(Type));

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 3 '06 #22

P: n/a
Jerry Coffin posted:
Of course, malloc returns a 'void *' and unlike in C,
you can't assign that to a 'Type *' without a cast:

Type *p = static_cast<Type *>malloc(sizeof(Type));

Of course, you're correct.

99% of the time when I'm using "malloc", I'm writing C code... and so force
of habit made me neglect to cast.
--

Frederick Gotham
Jul 3 '06 #23

This discussion thread is closed

Replies have been disabled for this discussion.