LinuxQuestions.org
Visit Jeremy's Blog.
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 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
{
public:
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
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,386

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


Reply

Tags
c++


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


All times are GMT -5. The time now is 04:19 AM.

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