LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 10-17-2003, 04:14 PM   #1
jhorvath
Member
 
Registered: Sep 2002
Location: OH, USA
Distribution: 2.6.16-1.2096_FC5 #1
Posts: 245

Rep: Reputation: 30
not calling copy constructor on function return


hi...

i'm taking this into to c++ course on codewarrioru.com ...and there's an example on there that is trying to show that a copy constructor is called upon returning an object from a function.

the problem i am having , is that , this doesn't seem to be the case on my machine ..but a few people i talked to in that forum said it *is* definately supposed to be called..

take this code as an example (it's from the lesson actually) ::

Code:
#include <iostream> 

using namespace std; 

class samp_class { 
    public: 
        samp_class() {cout << "Normal constructor called.\n";} 
        samp_class(const samp_class &ob1) {cout << "Copy constructor called.\n";} 
};

samp_class funct() { 
    samp_class ob2; 

    return ob2; 
} 

int main() { 
    samp_class x; 
    x = funct(); 

    return 0; 
}
my output from this program consists of ::

Normal constructor called.
Normal constructor called.

...and thats it ..i never see 'Copy constructor called.' when the object is copied from funct() and assigned to x

...i'm using gcc-3.2.2 ..not sure what version of libstd++ i got on here ...`locate libstd++` returns numerous solibs

...anyone has any ideas on this that would be cool...

thanks,
 
Old 10-17-2003, 05:07 PM   #2
ToniT
Senior Member
 
Registered: Oct 2003
Location: Zurich, Switzerland
Distribution: Debian/unstable
Posts: 1,357

Rep: Reputation: 47
There is an error in your lecturers code. The code is in fact calling the assignment operator.

Try compiling and running this one to figure out/illustrate the difference
Code:
#include <iostream> 

using namespace std; 

class samp_class { 
    public: 
        samp_class() {cout << "Normal constructor called.\n";} 
        samp_class(const samp_class &ob1) {cout << "Copy constructor called.\n";} 
        samp_class operator=(const samp_class &ob1) { cout << "Assignment operator called.\n"; }
};

samp_class funct() { 
    samp_class ob2; 

    return ob2; 
} 

void func2(samp_class foo) {
}

int main() { 
    samp_class x;
    cout << "Calling func..\n";
    x=funct();
    cout << "Calling func2..\n";
    func2(x);
    cout << "Done.\n";
    return 0; 
}
 
Old 10-17-2003, 05:26 PM   #3
jhorvath
Member
 
Registered: Sep 2002
Location: OH, USA
Distribution: 2.6.16-1.2096_FC5 #1
Posts: 245

Original Poster
Rep: Reputation: 30
..i'm no guru so i cannot say for sure that it isn't *supposed* to be called ..but i can't get it to work for the life of me. i have already changed the example to see what was going on ...like adding a few variables to that class and seeing if everything was infact copied(assigned) to 'x' ..and infact it was, ...i don't know what to do about this copy constructor then. it does make sense that the function return would create a copy to be used outside the function and the assignment operator would 'copy' that copied object into 'x'... but i'm not sure ...if someone can elaborate a little on this, i would be most grateful :)

thanks,

Last edited by jhorvath; 10-20-2003 at 05:48 PM.
 
Old 10-17-2003, 07:58 PM   #4
ToniT
Senior Member
 
Registered: Oct 2003
Location: Zurich, Switzerland
Distribution: Debian/unstable
Posts: 1,357

Rep: Reputation: 47
Let me give an other example.
Code:
#include <iostream> 

using namespace std; 

class samp_class { 
    public:
        samp_class() {cout << "Normal constructor called.\n";}
        samp_class(const samp_class &ob1) {cout << "Copy constructor called.\n";}
        samp_class operator=(const samp_class &ob1) { cout << "Assignment operator called.\n"; }
        samp_class(int number) { cout << "I am constructor too.\n"; }
};

samp_class funct() {
    samp_class ob2;
    return ob2;
}


void func2(samp_class foo) {
  // Here a new object called foo is in use.
}

int main() {
    samp_class x;
    cout << "Calling funct..\n";
    x = funct();
    cout << "Looks like an assignment but is not..\n";
    samp_class y=x; // syntaxically same as "samp_class y(x);"
    cout << "Calling func2..\n";
    func2(x);
    cout << "Doing something bizarre..\n";
    samp_class z=27; //As in previous case, syntaxically same as "samp_class z(27);"
    cout << "Done";
    return 0;
}
What is the difference between the copy constructor and assignment operator then?
It is less about a syntax but more about what has to be done.

Say T is the target object, S is the source. If what you are doing is trying to do is
to have the T contain the contents of S, you are makeing a copy.
Too cases
  1. T doesn't exist. Then T must be constructed and thus a constructor must be called. So, a copy-constructor is used.
  2. T do exist. Then T is allready constructed and what is wanted is to move(or should I say assign) the contents of S to the variables of T. So, an assignment operator is used.
Those are fundamentally different situations where '=' the operator is used in that example.

I would recommend Bruce Eckel: Thinking in C++ for further reading. Especially the chapters "References & the Copy Constructor" and "Operator Overloading" deal with the problems related to object passing.

Last edited by ToniT; 10-17-2003 at 08:03 PM.
 
Old 10-22-2003, 12:26 PM   #5
jhorvath
Member
 
Registered: Sep 2002
Location: OH, USA
Distribution: 2.6.16-1.2096_FC5 #1
Posts: 245

Original Poster
Rep: Reputation: 30
thanks for pointing that out ToniT..

some more questions that maybe you , or someone else might be able to clarify for me <?>

i compiled your code three times (using the first example you gave me)..the first time i got this output:

Normal constructor called.
Calling func..
Normal constructor called.
Assignment operator called.
Calling func2..
Copy constructor called.
Done.

..the second time, i recompiled with '-Wall' and it cried about not having a return statement for the copy assignment op.. so i added 'return *this;' and recompiled with the warnings and it went fine...however my output had changed to this::

Normal constructor called.
Calling func..
Normal constructor called.
Assignment operator called.
Copy constructor called. <-- this was added
Calling func2..
Copy constructor called.
Done.

...now i recompiled with debugging info (-ggdb) and ran it through anjuta-1.1.97 and i noticed something rather odd...

--- taken from my post on codewarrioru.com ---
i ran this through a debugger, using anjuta, and i noticed something peculiar ...something that isn't shown in the output...

when i reach the line 'x=funct();'
it creates 'ob2' using the standard constructor. then it jumps to the closing brace in funct(), and immediatley jumps to the assignment op, then to the copy constructor...what strikes me as odd is that it then jumps back to the assignment op , but it doesn't display any output from *that* jump , it goes right to the 'cout' after the function call is completed and everthing is *copied*??

so theoretically what should've been shown according to what i just said would be ::

Normal constructor called.
Calling func..
Normal constructor called.
Assignment operator called.
Copy constructor called. <-- mine is here though ..hmmm...
Assignment operator called. <-- according to what i witnessed in the debugger ...this should show up again ??
Calling func2..
Copy constructor called.
Done.

does anyone have any thoughts on this

thanks alot,
jeremy
 
Old 10-22-2003, 02:19 PM   #6
ToniT
Senior Member
 
Registered: Oct 2003
Location: Zurich, Switzerland
Distribution: Debian/unstable
Posts: 1,357

Rep: Reputation: 47
You are doing good analysis. By also checking the backtrace of function-calls we get
more information, sample run:
Code:
toni@mario:~/work/tst$ gdb ./a.out 
GNU gdb 6.0-debian
Copyright 2003 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "i386-linux"...
(gdb) break main
Breakpoint 1 at 0x8048657: file copy2.cpp, line 22.
(gdb) run
Starting program: /home/toni/work/tst/a.out 

Breakpoint 1, main () at copy2.cpp:22
22          samp_class x;
(gdb) n
Normal constructor called.
23          cout << "Calling func..\n";
(gdb) n
Calling func..
24          x=funct();
(gdb) step
funct() () at copy2.cpp:13
13          samp_class ob2; 
(gdb) n 
Normal constructor called.
16      } 
(gdb) step
samp_class::operator=(samp_class const&) (this=0xbffffa00, ob1=@0xbffff9e0) at copy2.cpp:9
9               samp_class operator=(const samp_class &ob1) { cout << "Assignment operator called.\n"; return *this; }
(gdb) bt
#0  samp_class::operator=(samp_class const&) (this=0xbffffa00, ob1=@0xbffff9e0) at copy2.cpp:9
#1  0x0804869d in main () at copy2.cpp:24
(gdb) step
Assignment operator called.
samp_class (this=0xbffff9f0, ob1=@0xbffffa00) at copy2.cpp:8
8               samp_class(const samp_class &ob1) {cout << "Copy constructor called.\n";} 
(gdb) bt
#0  samp_class (this=0xbffff9f0, ob1=@0xbffffa00) at copy2.cpp:8
#1  0x080487c5 in samp_class::operator=(samp_class const&) (this=0xbffffa00, ob1=@0xbffff9e0) at copy2.cpp:9
#2  0x0804869d in main () at copy2.cpp:24
(gdb) step
Copy constructor called.
0x080487c5 in samp_class::operator=(samp_class const&) (this=0xbffffa00, ob1=@0xbffff9e0) at copy2.cpp:9
9               samp_class operator=(const samp_class &ob1) { cout << "Assignment operator called.\n"; return *this; }
(gdb) step
main () at copy2.cpp:25
25          cout << "Calling func2..\n";
(gdb) The program is running.  Exit anyway? (y or n) y
toni@mario:~/work/tst$
So, the copy-constructor is called here to make a copy being passed to the
assignment chain (that is a=b=c stylish chaining). So, the line 'x=funct()' has some value
that is not used to anything (usage could be something like '(x=funct()).aMethodOfSamp_class()' or more chaining like 'samp_class y=x=funct()').

And the caller of copy-constructor is the assignment-operator itself; the assignment-operator
is not called twise. The line you see in the debugger is because the copy-constructor returns
and the function has many operations in one line.


Making a copy here is reluctant and can be avoided by writing the assignment-operator
to form
Code:
samp_class &operator=(const samp_class &ob1) { cout << "Assignment operator called.\n"; return *this; }
.
 
Old 10-23-2003, 11:26 AM   #7
jhorvath
Member
 
Registered: Sep 2002
Location: OH, USA
Distribution: 2.6.16-1.2096_FC5 #1
Posts: 245

Original Poster
Rep: Reputation: 30
...i will definately sit down and fully digest the methods you display for backtracing
..it is a good thing to know, thank you.

a user on codewarrioru did notice the missing 'return *this;' and also the missing '&' and after i had added that to my code it seems to behave as expected. it's always the small things (eg ..syntax) that i miss out.

thanks again,
jeremy
 
Old 09-22-2009, 12:43 PM   #8
vivpad
LQ Newbie
 
Registered: Sep 2009
Posts: 3

Rep: Reputation: 0
CodewarriorU C and C++ materials

HI all,



I think CodeWarriorU.com is started in 2000.It had some good materials on c,c++,java etc.Now the website is unavailable.can anyone has the course materials on c and c++ please send me the course notes.Because i have studied on year 2002 .The materials used to be very nice.I am badly in need of the course material for c and c++ in codewarriorU.com.Please help me in this regard



Please reply ASAP



Thanks and Regards

vivek
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
copy constructor causing confusing error message. (gcc c++) qwijibow Programming 6 09-21-2005 08:34 PM
Calling another function from a function using GTK geminigal Programming 4 07-11-2005 03:15 PM
C++, indefinite function arguments and multiple constructor passing @_@ R00ts Programming 2 04-08-2005 03:33 PM
Copy constructor in C++ prenayan Linux - General 3 11-13-2003 02:48 PM
constructor return type macro-linux Programming 2 11-12-2003 01:29 AM


All times are GMT -5. The time now is 02:23 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration