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

  • Home
  • SEARCH
  • 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 3317272
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T22:34:06+00:00 2026-05-17T22:34:06+00:00

In C++ methods can have optional arguments, like this: void myFunction (int arg1, int

  • 0

In C++ methods can have optional arguments, like this:

void myFunction (int arg1, int arg2=0);

In this case myFunction can be called with 1 integer or with two. If you omit the second integer, the value 0 is passed instead.

I am now looking for ways to obtain the same functionality, but from the callee’s side. Suppose I have an interface I need to implment (e.g. an observer interface). At this moment it looks like this:

class IObserver
   {
   public:
      virtual bool operator() (OtherClass *instance) = 0;
   };

Until now, the code calling the observers only wanted to pass the instance argument. But, in order to implement some new functionality, I want to add some other arguments, like this:

class IObserver
   {
   public:
      virtual bool operator() (OtherClass *instance, int firstOptional, int secondOptional) = 0;
   };

But, given that these new arguments only makes sense for a small number of very specific observers, I don’t want to add the additional arguments to the other 99% of the observers.

To make the situation more complex, the code to which you give the observer also accepts a lambda expression. So you could write something like this:

addObserver (observee, [](OtherClass *instance)->bool {/*do something here*/ return true;}

The simplicity of the original signature made it nice and easy to pass a lambda expression.
With the additional arguments I am now forced to add these arguments to the lambda expressions even if they don’t make sense for that observer.

I know the problem can be solved by introducing an additional adapter, but I what I’m actually looking for is a way to have real optional arguments from the callee’s side.

Any suggestions on how to solve this in a clean but easy way?

  • 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-17T22:34:06+00:00Added an answer on May 17, 2026 at 10:34 pm

    In this case, I would consider abstract concept of additional parameters, since they are for only some specific implementations. Your interface will become like:

    class IObserver 
       { 
       public: 
          virtual bool operator() (OtherClass *instance, IObserverArgs* args) = 0;
       }; 
    

    And implementation need additional arguments can have their own IObserverArgs implementation. However, this may not be flexible enough towards lambda.

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

Sidebar

Related Questions

Not many are aware of this feature, but Python's functions (and methods) can have
Can a class have a Service Contract attribute and its methods Operation Contract. Or
We have some methods that call File.Copy, File.Delete, File.Exists, etc. How can we test
In Kohana/CodeIgniter, I can have a URL in this form: http://www.name.tld/controller_name/method_name/parameter_1/parameter_2/parameter_3 ... And then
I have a simple extension method on the int type so I can do
I have a purchase page, it can take an optional argument as a gift,
I have a method that can return either a single object or a collection
How can I select the good method (I have in the example below show
I have class method that returns a list of employees that I can iterate
if i have a protected method, can i pass in a parameter where the

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.