LinuxQuestions.org
Review your favorite Linux distribution.
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 02-03-2011, 03:35 AM   #1
abhinav.dube15
LQ Newbie
 
Registered: Feb 2011
Posts: 1

Rep: Reputation: 0
C++ constructor/copy constructor issues in g++ 3.4.6


Here is a code snippet:

Code:
class test{

    public:
            test(){
                    cout<<"I am in default constructor ";
            }
            static void func(const  test &obj){
                    cout<<"I am in function ";
            }
    protected:
            test( const test &o){
                    cout<<"I am  in copy constructor ";
            }

};

int main() { 
test::func(test()); 
}
The above code gives following error with g++ 3.4.6(on Red Hat Linux) on compilation: In function int main()': error:test::test(const test&)' is protected
error: within this context

However if you compile with g++ 3.3.2 or g++ 4.4.3 (on Red Hat Linux),it compiles successfully and gives following output:

I am in default constructor I am in function

In the above code,i have passed the temporary object(created by default constructor) to function func by reference.So why the compiler 3.4.6 is calling the copy constructor?

Last edited by XavierP; 02-04-2011 at 01:52 AM.
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 02-04-2011, 12:38 AM   #2
vjackcon
LQ Newbie
 
Registered: Jan 2011
Posts: 3

Rep: Reputation: 0
weird C++ constructor/copy constructor issues in g++

[edited out - plagiarism and not connected to the original question]

Last edited by Mara; 03-14-2011 at 02:35 PM.
 
0 members found this post helpful.
Old 02-04-2011, 04:07 AM   #3
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,539

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by abhinav.dube15 View Post
In the above code,i have passed the temporary object(created by default constructor) to function func by reference.So why the compiler 3.4.6 is calling the copy constructor?
Maybe there's a bug in the older compiler? ... and it was fixed in a later version. Your code is fine, as far as I'm concerned.
 
Old 02-04-2011, 04:11 AM   #4
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,539

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by vjackcon View Post
Any explanations?
1. Never hijack someone else's thread.

2. Never pass back a reference to an object that is declared on the stack, as you have done in fun(). Once that particular function returns, the local variable 's' is destroyed, and thus there is nothing left to reference. With GCC version 4.4.3, the following warning is produced.
Code:
g++ foo.cpp 
foo.cpp: In function X& fun():
foo.cpp:18: warning: reference to local variable s returned
 
Old 02-04-2011, 06:53 PM   #5
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by vjackcon View Post
Code:
X& fun() {
        X s;
        return s;
}
The copy constructor isn't called here. You're merely leaving a pointer to s on the stack.
Quote:
Originally Posted by abhinav.dube15 View Post
The above code gives following error with g++ 3.4.6(on Red Hat Linux) on compilation: In function int main()': error:test::test(const test&)' is protected
error: within this context
As far as I know test() is taken as an rvalue, which basically means it isn't required to be addressable. You can, however, pass an rvalue as a const reference, but the compiler that causes the error might be trying to push an lvalue to the stack constructed by copying test(). It seems like a compiler bug.
Kevin Barry
 
Old 03-11-2011, 11:11 AM   #6
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 9,685

Rep: Reputation: 4324Reputation: 4324Reputation: 4324Reputation: 4324Reputation: 4324Reputation: 4324Reputation: 4324Reputation: 4324Reputation: 4324Reputation: 4324Reputation: 4324
Quote:
Originally Posted by vjackcon View Post
This code calls the copy constructor also. Why does this work?
If anyone's wondering what vjackcon's talking about, the answer is that his post is 100% plagiarized from here:

http://stackoverflow.com/questions/1...or-issues-in-g

His whole reason for posting, of course, was to attach his spam sig.

He has two other plagiarized replies:

http://www.linuxquestions.org/questi...0/#post4250713
http://www.linuxquestions.org/questi...7/#post4240254

Neither of them have anything to do with the discussions in the threads they were posted to.

Last edited by dugan; 03-11-2011 at 11:31 AM.
 
2 members found this post helpful.
Old 03-13-2011, 06:11 AM   #7
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 234Reputation: 234Reputation: 234
Nice catch, dugan. (+7)

How did you do your plagiarizism check?
 
Old 03-13-2011, 08:27 AM   #8
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 9,685

Rep: Reputation: 4324Reputation: 4324Reputation: 4324Reputation: 4324Reputation: 4324Reputation: 4324Reputation: 4324Reputation: 4324Reputation: 4324Reputation: 4324Reputation: 4324
Quote:
Originally Posted by archtoad6 View Post
How did you do your plagiarizism check?
I just pasted the first line or so into Google, and added a quotation mark to each end.

Here's the query for this one:

http://www.google.ca/#&q="This+code+...hat+the+first"

Last edited by dugan; 03-13-2011 at 08:31 AM.
 
  


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 in GCC4.4 AmuthaA Programming 7 04-26-2010 07:26 AM
not calling copy constructor on function return jhorvath Programming 7 09-22-2009 12:43 PM
Homework Help: Copy Constructor on a template class. MicahCarrick Programming 2 01-22-2006 10:43 PM
copy constructor causing confusing error message. (gcc c++) qwijibow Programming 6 09-21-2005 08:34 PM
Copy constructor in C++ prenayan Linux - General 3 11-13-2003 02:48 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:57 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration