Classes and Global Functions

This is a discussion on Classes and Global Functions within the C++ Programming forums, part of the General Programming Boards category; Hi, I have recently been learning about classes and operator functions. and while working these things into examples i have ...

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    9

    Classes and Global Functions

    Hi, I have recently been learning about classes and operator functions. and while working these things into examples i have come across a problem. I have solved it but i really need an explantion as to why my solution worked.

    I have a multiplication function :

    Code:
    Point mult(int n, const Point &other);          
    
    Point mult(int n, const Point &other) {       
          Point newpoint;
          newpoint.x = other.x * n;
          newpoint.y = other.y * n;
          
          return newpoint;
    }
    which i call in the following operator function:

    Code:
    friend Point operator*(int n, const Point &other) {return mult(n, other);}
    after trying to compile this i got this error :

    29 C:\Dev-Cpp\Exercises\pointclass\main.cpp cannot call member function `Point Point::mult(int, const Point&)' without object

    Not knowing what this means i just mucked around and made the mult function a friend, and it worked. Im just wondering why this worked. By giving the mult function two arguments did i make it global or something? , and making it a friend gave it access to what it needed?

    Plz explain, lol

    Thx in Advance

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Your mult() function is a member of class Point. It needs to be a function that is not a member of class point. A friend function (such as your operator*()) are not usually member functions either.

    I wouldn't usually inline a friend function, but that's a stylistic concern.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    9
    Just a silly question, but i have to ask because i dont understand,

    Why does the mult() function not need to be a member of the class? I thought that the operator function, being a friend of the Point class would have access to the mult() function if it were a member.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,796
    Why does the mult() function not need to be a member of the class?
    In this case, your mult() does not need to use the current object. It is essentially a free function using the class as a namespace, but unlike free functions, it needs an object other than its argument(s) to be invoked.

    I thought that the operator function, being a friend of the Point class would have access to the mult() function if it were a member.
    If Point::mult() were public, it would have access even if it were not a member. However, the point (pun intended, haha) is that it does not need an object of the class other than other. Suppose you defined point like this instead:
    Code:
    Point Point::mult(int n) {
        // Multiply this Point by n.
        return *this;
    }
    Now, instead of passing other as the argument, you would just call other.mult(n);
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    9
    ahh, that has cleared it up a bit. Thanks alot for the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. basic question about global variables
    By radeberger in forum C++ Programming
    Replies: 0
    Last Post: 04-06-2009, 12:54 AM
  2. Declaring classes in global space
    By Rune Hunter in forum C++ Programming
    Replies: 12
    Last Post: 10-12-2005, 02:46 PM
  3. Global Variables, include files and classes
    By sharpstones in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2005, 10:06 AM
  4. global classes
    By earth_angel in forum C++ Programming
    Replies: 2
    Last Post: 07-04-2005, 08:44 PM
  5. Global Classes
    By Death_Wraith in forum C++ Programming
    Replies: 3
    Last Post: 03-12-2004, 06:12 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21