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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:07:45+00:00 2026-05-28T03:07:45+00:00

I have a function which aims to perform a recursive calculation. If my function

  • 0

I have a function which aims to perform a recursive calculation. If my function is programmed recursively, it takes too long to compute. Therefore, I perform memoization by storing intermediate results in an array.

During the execution of my program, I might call the function with parameters (10,0),(5,5),(2,4) etc. Therefore I have a setup(double x) function which fills the entire array with the correct values. I can then access any of the array values without any further calculations. I only wait until x changes to call setup() again.

I am wondering how I can go about implementing this in c++. It doesn’t make sense to me to use a class, as I would never need to create the associated object. I have implemented the functions fine in a namespace, but I’m still having a problem. Even If I use an unnamed namespace, the array used by my function is visible and can be modified from outside the namespace of the function. If I include the header file of the namespace, that is.

my code:

FunctionWrapper.h

namespace FunctionWrapper{
      namespace{
            double tempArray[10][10];
      }

      void setup(double x);
      void getValues(int n);
}

Main.cpp

#include "FunctionWrapper.h"

int main(){
   FunctionWrapper::tempArray[0][0] = 5; //Works
}
  • 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-28T03:07:45+00:00Added an answer on May 28, 2026 at 3:07 am

    If you do not want tempArray to be nameable in other source files, don’t declare it in the header file. Instead, declare it in an unnamed namespace in FunctionWrapper.cpp. Then, it can only be used directly from within that source file.

    In general, a header file should not use an unnamed namespace, as it can (and often will) cause One Definition Rule violations.

    Note that a better solution to your problem might, in fact, be to create a class that provides this functionality:

    class ValueGetter
    {
    public:
        ValueGetter(double x);
        void GetValues(int n);
    
    private:
        double cache[10][10];
    };
    

    This way, you can create an instance of this type, and all of the state is owned by that instance. There are many benefits to avoiding global state, including increased maintainability and testability.

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

Sidebar

Related Questions

I have function which takes in an parameter of a class called Triple, and
I have a function which takes a variable number of pointers, which I would
I have a function which takes two parameters: string[], double[] How do I return
I have a function which takes in a font (ttf or otf file) and
I have a function which takes two arrays containing the tokens/words of two texts
I have a function which takes a reference to an object: void move(Ball& ball);
I have a function which can vary in the time it takes to finish.
I have a function which takes two strings and gives out the cosine similarity
I have a function which takes as an argument an XDocument object. I need
I have a function which parses one string into two strings. In C# I

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.