Thread: Default argument/parameter to be another previous one

  1. #1
    Registered User
    Join Date
    Nov 2019
    Posts
    6

    Default argument/parameter to be another previous one

    How to have a function default argument/parameter to be another previous argument value ?

    let's make the below illustration only really work

    Code:
    char* function(char* a, int b, char** c = &a){
    
       // if invoked only with two arg, c is assigned automatically with reference of a
       //...
    }
    How to solve it? thanks much.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,557
    Instead of a default argument, add a two-parameter overload that forwards to the three-parameter version:
    Code:
    char* function(char* a, int b, char** c) {
        // ...
    }
    
    char* function(char* a, int b) {
        return function(a, b, &a);
    }
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why default argument for the last parameter is mandatory ?
    By techie_san778 in forum C++ Programming
    Replies: 6
    Last Post: 12-01-2014, 02:52 PM
  2. Default value for function parameter
    By Justin H in forum C++ Programming
    Replies: 2
    Last Post: 12-19-2012, 01:08 AM
  3. Redefinition of default parameter
    By 843 in forum C++ Programming
    Replies: 7
    Last Post: 03-14-2011, 02:07 AM
  4. Default parameter
    By g4j31a5 in forum C++ Programming
    Replies: 3
    Last Post: 01-05-2007, 11:59 AM
  5. default parameter
    By laasunde in forum C++ Programming
    Replies: 8
    Last Post: 11-02-2003, 10:55 PM

Tags for this Thread