rakoo said:
is there is any ways i pass default value to reference variable
Yes.
suct as fun(const int &i=8) as i can't do fun ( int &i=8)
.... and you seem to know what that is, so what is the question?
and modify variable i ,inside that funtion.
Ah, you want to pass an integer by a non-const reference so you can
modify it. But, you also want to be able to call the function with no
arguments and work on a default value.
The trick is to realize that default arguments are equivalent ot
multiple functions that are overloaded. For instance, this function
with a default argument:
void foo(int x = 3) { }
is equivalent to these two functions:
void foo(int x) { }
void foo() { foo(3); }
Do you understand? If you call foo(30) then the first overload is
called and x takes on the value 30. If you call foo() then the second
overload is called, and calls the first one with value 3. So
effectively, 3 is the default value.
But by splitting the function into multiple ones, you have more
flexibility, which allows you to solve your problem like this:
void fun(int &modify_me) { modify_me = 42; }
void fun() { int throw_me_away = 8; fun(throw_me_away); }
These two functions together give you the behavior you want. The
throw_me_away variable inside fun() is a non-temporary object that can
be bound to a non-const reference.