Help answer threads with 0 replies.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 07-06-2012, 01:03 AM   #1
Navjot Arora
LQ Newbie
Registered: Apr 2012
Location: Bangalore
Distribution: J ARORA
Posts: 22

Rep: Reputation: Disabled
Reg Copy Constructor

Hi All,

There is small doubt about the below program
class A
cout <<"C'tor!"<<endl;
A(const A& src)
cout <<"Copy C'to!"<<endl;
void display()
cout << "Worked!"<<endl;


int main()
A a = A();
ifstream obj = ifstream("notepad.txt");
return 0;

when we create a object like A a = A(); , according to me, this should create temporary object and then copy C'tor Should be called but this has not happened but if i make the copy C'tor of class A as private (even though it is not called or used), g++ error throws for statement A a = A();

If i create a ifstream object like above then g++ compiler throws an error like below :

"synthesized method ‚std::basic_ifstream<char, std::char_traits<char> >::basic_ifstream(const std::basic_ifstream<char, std::char_traits<char> >&)‚ first required here"

My concern here is if we create a object like A a = A(); why the copy C'tor should be accessible if even it is not called and consequently why for ifstream it is required if Copy C'tor is not called for user defined class (Trying to make it generic)
Old 07-08-2012, 11:39 AM   #2
Senior Member
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,309

Rep: Reputation: 1485Reputation: 1485Reputation: 1485Reputation: 1485Reputation: 1485Reputation: 1485Reputation: 1485Reputation: 1485Reputation: 1485Reputation: 1485
See Copy elision:
copy-initialization is usually equivalent to direct-initialization in terms of performance, but not in semantics; copy-initialization still requires an accessible copy constructor.



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
C++ constructor/copy constructor issues in g++ 3.4.6 abhinav.dube15 Programming 7 03-13-2011 08:27 AM
Copy Constructor in GCC4.4 AmuthaA Programming 7 04-26-2010 07:26 AM
Copy constructor in C++ prenayan Linux - General 3 11-13-2003 02:48 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:48 AM.

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