|
-
Dec 30th, 2001, 03:05 AM
#1
Thread Starter
Addicted Member
Another 'Why & Why Not' Question
Code mentioned in a book fr showing how 2 do Assignment Op. O/Loading.
#include<iostream.h>
class alpha
{
int data;
public:
alpha(){}
alpha(int d)
{
data=d;
}
void display()
{
cout<<data;
}
alpha operator =(alpha& a)
{
data=a.data;
cout<<"Assignment op. invoked";
return alpha(data);
}
};
void main()
{
alpha a1(37);
alpha a2;
a2=a1; //invokes O/Loaded Ass. Op.
cout<<"\n a2";a2.display();
alpha a3=a2; // Does not invoke O/Loaded Ass. Op.
cout<<"\na3=";a3.display();
}
What I wanna know here is tt in the Operator O/Loading bit
alpha operator =(alpha& a)
{
data=a.data;
cout<<"Assignment op. invoked";
return alpha(data);
}
the guy has specified alpha as a return type...I mean why shud it be alpha...y cant it b just a void return type.....after all...we're only going 2 b doing
alpha a3=a2
so y isnt the following sufficient
void operator =(alpha& a)
{
data=a.data;
cout<<"Assignment op. invoked";
}
-
Dec 30th, 2001, 03:10 AM
#2
Thread Starter
Addicted Member
correction
Y do all this when we're only going 2 b doing
a3=a2
-
Dec 30th, 2001, 07:07 AM
#3
transcendental analytic
In fact it should be alpha& not alpha. As all the operators actually returns something you can construct expressions that does more than one assignment, for instance:
int a,b,c;
a=b=c=0;
instead of
a=0;
b=0;
c=0;
reason why you would need it to be a reference instead of a copy, is for those cases where you pass assigned values:
Dosomething(a=10);
It's nothing you absolutely have to do, it's just that it's more convenient if the operators act like they should
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Dec 30th, 2001, 08:04 AM
#4
Monday Morning Lunatic
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Dec 30th, 2001, 10:06 AM
#5
Thread Starter
Addicted Member
Not Quite Old by Indian Standards......I'm in India now....homeland...frgot 2 change the Location in Profile..its actually TurboC++ by robert Lafore....
Y ?....nowadays do they only write #include<iostream>...tts y ?
-
Dec 30th, 2001, 10:10 AM
#6
Monday Morning Lunatic
Uh...tts?
The new iostream library fits in better with the rest of the standard library because it's all template-based. In practice it shouldn't make much difference to the actual code you write, but it makes it easier to implement for the library writers.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|