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 6192249
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:54:41+00:00 2026-05-24T02:54:41+00:00

I tried asking before but I wasn’t very clear so I’m re-asking it. I

  • 0

I tried asking before but I wasn’t very clear so I’m re-asking it.

I want to have a variable that depends on the value of another variable, like b in this example:

int main(){
    int a;
    dependent int b=a+1; //I'm just making this up
    a=3;
    cout << b; //prints 4
    a=4;
    cout << b; //prints 5
}

Of course, this does not exist in C++, but this is what I want.

So instead I tried making a function:

int main(){
    int a;
    int b(){ return a+1; } //error
    a=3;
    cout << b(); //would print 4 if C++ allowed nested functions
    a=4;
    cout << b(); //would print 5 if C++ allowed nested functions
}

The above doesn’t work because C++ doesn’t allow nested functions.

I can only make functions outside of main(), like this:

int b(){
    return a+1; //doesn't work because a is not in scope
}

int main(){
    int a;
    a=3;
    cout << b();
    a=4;
    cout << b();
}

But this does not work because a is not in the same scope as b(), so I would have to pass a as a parameter and I don’t want to do that.

Are there any tricks to get something similar to a dependent variable working in C++?

  • 1 1 Answer
  • 2 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-24T02:54:42+00:00Added an answer on May 24, 2026 at 2:54 am

    What you need is a closure. If you can use C++ 0x features, you are in luck. Otherwise, you can define one manually:

    #include <iostream>
    using namespace std;
    struct B
    {
        const int & a;
    
        B(const int & a) : a(a) {}
    
        // variable syntax (Sean Farell's idea)
        operator int () const { return a + 1; }
    
        // function syntax
        int operator () () const { return a + 1; }
    };
    int main()
    {
        int a;
        B b(a);
        a = 3;
        cout << b << '\n'; // variable syntax
        a = 4;
        cout << b() << '\n'; // function syntax
    }
    

    You can also define B inside main, but some compilers would not like it.

    The C++ 0x lambda syntax looks like this:

    auto b = [&]() { return a + 1; }
    

    The [&] means that the lambda captures local variables by reference.

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

Sidebar

Related Questions

I have tried refraining from asking for help, but I have had enough! I
I tried to test this myself before asking on the forum but my simple
So I have been here before asking about this issue but no one has
I've tried asking this before on the OpenNLP sourceforge page, but it is still
i have asked this question before but no answer was there. so asking again.
I tried asking this before but didnt really get an answer so Ill try
I tried to search a bit before asking but I really had no idea
I've tried to really look this one up before asking a question but it
I'm sorry to keep asking stupid questions, but I have tried to research this
This error is driving me crazy . I tried freenode #emberjs before asking here.

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.