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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:08:52+00:00 2026-05-29T11:08:52+00:00

I want to do something like this: int displayAll(Message *m, string &lastIndex, int &NumPrinted

  • 0

I want to do something like this:

int displayAll(Message *m, string &lastIndex, int &NumPrinted = 0 );

It gives me error, cribbing about int to int&.

I tried this too:

int temp =0;

int displayAll(Message *m, string &lastIndex, int &NumPrinted = temp );

Still it gives following error:

error: ISO C++ forbids in-class initialization of non-const static member 'temp'

Even static int temp; gives error.

error: ISO C++ forbids in-class initialization of non-const static member ‘temp’

  • 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-29T11:08:52+00:00Added an answer on May 29, 2026 at 11:08 am

    The problem with the first line of code you mention is that you are trying to pass a reference to a temporary variable

    class Foo {
    
        int displayAll(Message *m, bool &moreElements, string &lastIndex, int &NumPrinted = 0 );
    
    };
    

    The second bit of code complains because you were trying to initialize a class member statically.

    class Foo {
    
        int temp =0;
    
        int displayAll(Message *m, bool &moreElements, string &lastIndex, int &NumPrinted = temp );
    
    };
    

    (I am putting your code inside of a class declaration to be clear about what is happening).

    An easy way out of your problem that does not introduce a static variable is explicit function overloading:

    class Foo {
    
        inline int displayAll(Message *m, bool &moreElements, string &lastIndex) {
            int dummy = 0;
            return displayAll(m, moreElements, lastIndex, dummy);
        }
        int displayAll(Message *m, bool &moreElements, string &lastIndex, int &NumPrinted);
    
    };
    

    There’s a bit of boilerplate, but it achieves what you want.
    Hope this helps.

    EDIT: Some more clarification. The core of the problem stems from the fact that the function must take a reference to some memory that it can modify. If you pass it a temporary variable (temporary as in the C++ meaning of the term, not just the english language term ) (as in your first line of code), it’s illegal C++, since you usually copy a temporary to a value before you use it as an argument to a function:

    void bar( int someNum = 0 ); // think of this as creating a temporary rvalue 0
                                 // and then copying it into the function for use.
    
    // temporary rvalues arise in expressions like
    int v = 5 + 5; // the result of 5 + 5 is stored in a temporary rvalue, and then
                   // copied into v (which is an lvalue in this case).
    

    So we need something that is an “lvalue”, either some global variable somewhere or a temporary local variable ( in the english language sense ) as I gave in my answer. I was about to write a solution using a static variable, but there is a large flaw- since the static variable will be shared by all instances of your class, it will start out 0 and then be different every time you call the method ( since it would have been edted by the previous call). Even worse, in the case of multiple threads, you would be reading/writing to the same place of memory from several processors, so the value will be complete garbage, and you ill rape your processor cores’ caches as each write will invalidate the cache of every other core. It’s ugly, please don’t do it. 😛

    By using my first solution you make the temporary variable very local, without much impact on anything else.

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

Sidebar

Related Questions

hi I want to do something like this: int op(string s1, string s2){ int
I want to do something like this : myYear = record.GetValueOrNull<int?>(myYear), Notice the nullable
I want to do something like this: const MyFirstConstArray: array[0..1] of string = ('Hi',
I got something like this: int, string ------------ 1, 'test1' 1, 'test2' 2, 'test1'
i want something like this the user enter a website link i need check
I want something like this: <msxsl:script language=C#> ??? getNodes() { ... return ... }
I want do something like this: Result = 'MyString' in [string1, string2, string3, string4];
I want do something like this: Button btn1 = new Button(); btn1.Click += new
I want to define something like this in php : $EL = \n<br />\n;
I want to do something like this: create table app_users ( app_user_id smallint(6) not

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.