Results 1 to 6 of 6

Thread: Another 'Why & Why Not' Question

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2001
    Location
    India
    Posts
    203

    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";
    }

  2. #2

    Thread Starter
    Addicted Member
    Join Date
    Aug 2001
    Location
    India
    Posts
    203

    correction

    Y do all this when we're only going 2 b doing
    a3=a2

  3. #3
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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.

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    #include<iostream.h>
    How old is your book?
    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

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Aug 2001
    Location
    India
    Posts
    203
    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 ?

  6. #6
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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
  •  



Click Here to Expand Forum to Full Width