RSS Forums RSS
Please support our C++ advertiser: Programming Forums
Views: 969 | Replies: 2
Reply
Join Date: Jun 2006
Location: Bangalore, India
Posts: 88
Reputation: dilip.mathews is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 3
dilip.mathews's Avatar
dilip.mathews dilip.mathews is offline Offline
Junior Poster in Training

Help Constructors

  #1  
Jul 26th, 2006
#include <iostream>
using namespace std;
class Exercise 
{
public:
 int a;
    Exercise(){cout<<"constructor\n";}
 Exercise(const Exercise& x)
 {
  cout<<"copy constructor\n";
  a = x.a;
 }
 
 Exercise& operator= (Exercise &x)
 {
  a = x.a;
  cout<<"assignment operaor\n";
        return *this;
 }
};
Exercise fun(Exercise& );
int main(void)
{
 Exercise y;
 Exercise z;
 z = fun(y);
 return 0;
}
Exercise fun(Exercise& z)
{
 return z;
}

The above piece of code was compiled using both VC++ and gcc compiler. VC gave me the intended output, but on gcc i got some error.

constructor.cpp: In function `int main()':
constructor.cpp:30: initialization of non-const reference type `class Exercise &
'
constructor.cpp:30: from rvalue of type `Exercise'
constructor.cpp:17: in passing argument 1 of `Exercise::operator =(Exercise &)'

What is this error ?????
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2006
Location: Bangalore, India
Posts: 88
Reputation: dilip.mathews is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 3
dilip.mathews's Avatar
dilip.mathews dilip.mathews is offline Offline
Junior Poster in Training

Re: Constructors

  #2  
Jul 26th, 2006
Caught the error ....
Missed const in Exercise& operator= (const Exercise &x);

But again why VC++ was not throwing error.
Reply With Quote  
Join Date: Jun 2005
Location: Tokyo, Japan
Posts: 1,481
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Rep Power: 8
Solved Threads: 102
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: Constructors

  #3  
Jul 26th, 2006
I don't think there is a reason for GCC to give an error because of the missing const. The assignment operator of Class C can have any parameter of C, C&, const C&, volatile C&, or const volatile C&..

It ought to be something else. But I dont know what. Maybe GCC is guarding against self-assignment (which is possible if const is not used) while VC++ is not.
バルサミコ酢やっぱいらへんで
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 1:48 am.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC