How to overload a class func with an outside func while in a the same class

This is a discussion on How to overload a class func with an outside func while in a the same class within the C++ Programming forums, part of the General Programming Boards category; I have a class that has a method name the same as another method separate from the class and I ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    71

    How to overload a class func with an outside func while in a the same class

    I have a class that has a method name the same as another method separate from the class and I am writing another class method in the class but want to call the outside method instead of the one in the class.

    Is there a way I can overload the class method with the outside one?

    Here is what the situation looks like
    Code:
    class LongInt
    {
         int digits() { /* find total # of digits of this object */
         LongInt divide(const LongInt&); // this method is where I want to use the outside digits(int number) method 
    }
    int digits(int number){ /*finds total digits of number */}
    The problem is when I call digits(number) inside the class, I get compiler error saying it doesn't take 1 arguments.

  2. #2
    Registered User
    Join Date
    Oct 2012
    Posts
    71
    hmm, when I think about it more, what I'm trying to do seems like bad programming practice.

  3. #3
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    It's not really 'overriding' - you can certainly call a function by fully qualifying it. If it's in a namespace, it would be namespace::function(), or in the global namespace it would be ::function().

    You are right, though, this might not pass the smell test - I'm glad you're cultivating that nagging feeling that there might be a better way - that is too rare of a skill sadly
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,243
    Quote Originally Posted by workisnotfun View Post
    The problem is when I call digits(number) inside the class, I get compiler error saying it doesn't take 1 arguments.
    As to explaining that .... within a class member function, the attempt to use the name digits matches LongInt's digits() function first. This is called name hiding in some circles: the name LongInt::digits hides ::digits within context of the LongInt class. That's as far as the compiler goes: it matches the name digits, detects that LongInts::digits() accepts no arguments, and complains about a mismatch since you supplied an argument. The compiler does not (and the standard actually requires that it does not) attempt to match something else visible named digits that accepts a single argument. Hence the need for using the scoping operator, as mentioned by Cat.
    Right 98% of the time, and don't care about the other 3%.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need some help with pi() func!!!!!!
    By oron_e in forum C++ Programming
    Replies: 5
    Last Post: 01-07-2012, 07:56 AM
  2. Cos func.
    By omGeeK in forum C Programming
    Replies: 20
    Last Post: 10-17-2010, 11:12 AM
  3. about non-virtual func. in derived class
    By sawer in forum C++ Programming
    Replies: 7
    Last Post: 03-18-2006, 04:45 PM
  4. Overload == for string class
    By nickname_changed in forum C++ Programming
    Replies: 6
    Last Post: 10-02-2003, 05:07 PM
  5. class design and operation overload
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-03-2001, 09:49 PM

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