Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7559003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T12:31:20+00:00 2026-05-30T12:31:20+00:00

I want to achieve something like this: class C { int m_nVal; public: C(int

  • 0

I want to achieve something like this:

class C
{
    int m_nVal;
public:
    C(int nVal) : m_nVal(nVal){}            

    void foo(int nVal = m_nVal)
    {
         // use nVal, if provided; otherwise use m_nVal
    }
};

C c(1);
c.foo();  // use 1
c.foo(2); // use 2

This is not possible as C++ standard says:

a non-static member shall not be used in a default argument

Options I have are:

(1) Overload foo():

class C
{
    int m_nVal;
public:
    C(int nVal) : m_nVal(nVal){}

    void foo()
    {
        // use m_nVal
    }

    void foo(int nVal)
    {
        // use nVal
    }
};

(2) Use static member:

class C
{
    static int m_nVal;
public:         
    void foo(int nVal = m_nVal)
    {
        // use nVal, if provided; otherwise use m_nVal
    }
};

I don’t want to make m_nVal static member so option 1 seem the only one.

Are there any other ways to achieve this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T12:31:22+00:00Added an answer on May 30, 2026 at 12:31 pm

    There are other alternatives if you are willing to change the interface. You can use boost::optional:

    // untested:
    void foo( boost::optional<int> val = boost::optional<int>() ) {
        int value;
        if ( val ) value = *val;
        else value = m_val;
        // Now use `value` in the function
    }
    

    If you cannot use boost, you can write your own nullable wrapper. You just need to store the type (int) and a flag that determines whether it is set or not.

    The next option is using a pointer to mark that the argument is optional:

    void foo( int *pval = 0 ) {
        int value = (pval? *pval : m_val);
        // use value from here on
    }
    

    But the option with the pointer inhibits the use of rvalues as arguments to the function (i.e. you need a proper variable to call the function, you cannot do foo(1) but rather need to do int x = 1; foo( &x );, which is kind of a pain).

    Finally you can use your approach of offering two overloads, one that takes the argument and one that doesn’t and just forwards to the first:

    void foo( int val ) {
       // actual implementation
    }
    void foo() {
       foo( m_val );
    }
    

    This might actually be the best option…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to achieve something like this in C# 3.5: public void Register<T>() :
What I want to achieve is something like this: public void paint(Graphics g) {
I want to do something like this public class Class1 { public Class1() {
I've trying to achieve something like this: class Base { public: Base(string S) {
I'm trying to achieve something like this public abstract class BaseEvent { public abstract
I want to achieve something like this inside a UIView: I have created a
I want to do something like this: template<typename CType, unsigned int targetDimensions> struct GeneratePointerType
I have a base class like this: class FooBase { public bool Do(int p)
I want to achieve something like the following: UrlBuilder ub = new UrlBuilder(http://www.google.com/search); ub.Parameters.Add(q,request);
I want to achieve a list like this with smarty. <ul> <li> <a>img1</a> <a>img2</a>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.