Results 1 to 13 of 13

Thread: Friend functions and VC++

  1. #1

    Thread Starter
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772

    Friend functions and VC++

    I put together this code after looking at some other code here:
    Code:
    class Thing
    {
    private:
        int x;
    public:
        Thing();
        Thing(int);
        void set(int);
        friend Thing operator +(const Thing&, const Thing&);
        friend ostream operator <<(const ostream&, const Thing&);
    };
    I get this error on the bold line:

    c:\cpp\oper\thing.h(12) : fatal error C1001: INTERNAL COMPILER ERROR
    (compiler file 'msc1.cpp', line 1786)
    Please choose the Technical Support command on the Visual C++
    Help menu, or open the Technical Support help file for more information
    Error executing cl.exe.

    What does that mean?
    Alcohol & calculus don't mix.
    Never drink & derive.

  2. #2
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    It means that you are ****ed, because MSVC6 sucks.
    You have to copy in blocks of code until you can track down the error, and then post that line here (along with the rest).
    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.

  3. #3
    Fanatic Member nabeels786's Avatar
    Join Date
    Jul 2001
    Location
    New York
    Posts
    919
    what does friend do? i saw it somewhere too, but didn't understand what it was for/meant
    Visit www.fragblast.com
    Gaming, forums, and a online RPG/Battle system




    (__Flagg) DOT NET? is this a Hindi Dating service?

  4. #4
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    friend is a anti OOP thing it allows access of a class' private members to a global function
    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.

  5. #5
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Originally posted by kedaman
    friend is a anti OOP thing it allows access of a class' private members to a global function
    Nope, it allows access to private or protected members from any named item.

    Frequently used for things like linked lists:
    Code:
    class list {
        class link {
            friend class list;
        };
    
    public:
        void add(...);
    };
    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

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Could it be that you shouldn't specify the return type? The name and the parameters are enough to identify the function, so I think you don't need to specify type.

    friend certainly has it's uses. But you can usually avoid it. e.g. a global + operator can be realized by giving the class a += operator.
    Code:
    class MyBadClass
    {
      int m_i;
      friend operator +(const MyBadClass&, int);
      friend operator +(int, const MyBadClass&);
    };
    
    MyBadClass operator +(int i, const MyBadClass& rbc)
    {
      MyBadClass tmp;
      tmp = rbc;
      tmp.m_i += i;
      return tmp;
    }
    
    class MyGoodClass
    {
    public:
      MyGoodClass& operator +=(int i) { m_i += i; return *this;};
    };
    
    MyGoodClass operator +(int i, const MyGoodClass& rgc)
    {
      MyGoodClass tmp = rgc;
      tmp += i;
      return tmp;
    }
    That's both the bad and the good way to do it.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  7. #7

    Thread Starter
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772
    Originally posted by CornedBee
    Code:
    class MyGoodClass
    {
    public:
      MyGoodClass& operator +=(int i) { m_i += i; return *this;};
    };
    Kinds off the subject, but can you explain this line to me? Why do you have an ampersand in the return type, and why return *this? (Sorry, I'm new at this and can't find a good explanation anywhere.)
    Alcohol & calculus don't mix.
    Never drink & derive.

  8. #8
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Originally posted by parksie
    Nope, it allows access to private or protected members from any named item.

    Frequently used for things like linked lists:
    Code:
    class list {
        class link {
            friend class list;
        };
    
    public:
        void add(...);
    };
    Come on parksie! I know this stuff!

    1. The object in OOP can do two things a) encapsulates contained data and provide access trough accessors and methods, b) delegate other objects trough its accessors.
    2. Everything is an Object, there should not be any global functions, in other words, all functions are bound to their first parameter, their object.

    Hence these paradoxal facilities:
    1. Friend functions and classes
    2. Protected inheritance
    3. Functors

    OOP can't work alone, its too obvious, functional style does.
    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.

  9. #9
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Originally posted by Wynd
    Kinds off the subject, but can you explain this line to me? Why do you have an ampersand in the return type, and why return *this? (Sorry, I'm new at this and can't find a good explanation anywhere.)
    the ampersand before the type means that its a reference to the type, returning a& for a+=b means that you can still use a for further reference, ex: (a+=b)*=c

    *this is dereferenced this, and this points to the object of which function you have called
    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.

  10. #10
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Since there is no int class that you can modify, global + operators are necessary if you want to add a predefined type to your own class and want this type as first parameter.
    Like:
    Code:
    int i = 4;
    MC mc1(2, 3), mc2;
    mc2 = mc1 + i;   // this can be handled inside the class
    mc2 = i + mc2;  // this requires a global operator
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  11. #11
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Yep, but that's faaar from OOP
    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.

  12. #12
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Still I'm happy when I can do "predefined type" + "object" and not only "object" + "predefined type".
    Didn't you complain just some days ago that Java is so fixed on objects?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  13. #13
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    I did, and I didn't insist OOP was good in any ways
    You'll like how SQ handle things
    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.

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