473,386 Members | 1,795 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Default functions implemented by compiler

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
22 2383

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

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
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
Thanks for that.

-- Murali Krishna

Jun 29 '06 #9
* 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
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
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
* 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
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
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
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
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
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
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
#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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Anthony Baxter | last post by:
To go along with the 2.4a3 release, here's an updated version of the decorator PEP. It describes the state of decorators as they are in 2.4a3. PEP: 318 Title: Decorators for Functions and...
13
by: I. Dancay | last post by:
Can someone help me work out this project? I need someone who is a C++ expert to help me work this one out. Here it is below. implement an abstract data > type (ADT) Student as a C++ class....
2
by: Bryan Olson | last post by:
The current Python standard library provides two cryptographic hash functions: MD5 and SHA-1 . The authors of MD5 originally stated: It is conjectured that it is computationally infeasible to...
44
by: bahadir.balban | last post by:
Hi, What's the best way to implement an overloaded function in C? For instance if you want to have generic print function for various structures, my implementation would be with a case...
47
by: Albert | last post by:
So structures are useful to group variables, so you can to refer to a collection as a single entity. Wouldn't it be useful to also have the ability to collect variable and functions? Ask K&R...
4
by: moleskyca1 | last post by:
Hi, In a recent discussion, some of us were in disagreement about the functions the C++ compiler generates. How many functions are generated by the compiler when you declare: class Foo { };...
13
by: JohnQ | last post by:
The implementation of classes with virtual functions is conceptually easy to understand: they use vtables. Which begs the question about POD structs: how are they associated with their member...
39
by: vlsidesign | last post by:
Operators seem similar to functions. They both do something to either arguments or operands, but are different in their syntax. Operators seem to be like built-in C functions. It would seem that...
8
by: Grizlyk | last post by:
To continue some ideas of "C++ Archeology: Limits of OO paradigm", i want to ask about C++ development. There are C++ compilers, that has been written in old good C. Are C or pascal perfect...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.