Thread: redundant function call

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    153

    redundant function call

    Hey everyone,
    I was doing some programming exercises and I came across an interesting little problem. So, I'm overloading a class member function named DrawShape()...there's one version which takes no parameters and one that takes a height and width. The height and width version does all the work while the one without parameters simply calls the one that takes parameters and just inserts the values of the member variables. So I do this, and it works fine...I was just curious if this would be acceptable, having one overloaded function work simply by calling the other version. Isnt that redundant? Just something that was on my mind, maybe we can start a dialogue on this...ok thanks for your time...-Chap

  2. #2
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    This is just my opinion:

    For optimization purposes, I would just get rid of DrawShape( ) and keep DrawShape(arg1, arg2)
    Last edited by The Brain; 01-05-2005 at 06:18 PM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    Note that there is a way to get the best of both worlds in this case, and just provide default paramiters
    Code:
    drawShape(int x=640,int y=480);
    
    foo.drawShape(); // calls drawshape(640,480);
    foo.drawShape(1024); // calls drawshape(1024,480);
    Having a function that calls another function with little more changed than a paramiter is generally called "syntatic sugar". Personally, particularly if the call can be inlined, I think its fine. Often sugar consists of operators that call named methods.
    Code:
    template<class T>
    class foo {
    ...
    public:    
        foo<T> & append(const T &):
        foo<T> & operator += (const T &rhs) {return append(rhs);}
    };
    
    template<class T> inline
    foo<T> operator +(foo<T> lhs, const T &rhs) {return lhs += rhs;}
    
    template<class T> inline
    foo<T> operator +(const T &lhs, foo<T> rhs) {return rhs + lhs;}
    
    foo<int> a = 3 + b;  // becomes a = foo<int>(b).append(3);
    Note that the compiler is frequently smart enough to avoid function call overhead, but not always, a = 3+b could make up to 4 copies of b and 4 stack frames, so sugar is not always without cost. I generally like to give the compiler a crack at it though. It's much easyer to make an slow program that's easy to work with much faster, then a kinda fast, but confusing program even a little bit faster.

  4. #4
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    If it fills in the parameters with member variables, I don't believe default arguments will work - though I've never tried, to be truthful. Perhaps it would work with references or const references, though. Alternately, if there is some 'invalid' value that you can use to signal that you want the parameters filled in with default values:
    Code:
    void MyClass::myFunc(int val1 = -1, int val2 = -1) //assuming -1 is invalid
    {
       if(val1 == -1)
          val1 = classMem1;
       if(val2 == -1)
          val2 = classMem2;
    
       //do stuff
    }
    Otherwise, if you simply inline your 'sugar' function, there would probably be no overhead, unless the compiler decides to ignore your 'inline' keyword (as it is entitled to do). You can inline a couple ways, the first being my personal preference:
    -Define the function within the class, not in a separate .cpp file (or outside the class declaration).
    -Declare the function with the keyword inline

    Hope this helps!
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    153
    thanks a lot for your posts guys...good insight into my little dilemma, greatly appreciated -Chap

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 07:18 AM
  3. temperature sensors
    By danko in forum C Programming
    Replies: 22
    Last Post: 07-10-2007, 07:26 PM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM