Reference varible

R

rakoo

is there is any ways i pass default value to reference variable

suct as fun(const int &i=8) as i can't do fun ( int &i=8)


and modify variable i ,inside that funtion.

rakesh Kushwaha
 
K

Karl Heinz Buchegger

rakoo said:
is there is any ways i pass default value to reference variable

suct as fun(const int &i=8) as i can't do fun ( int &i=8)

and modify variable i ,inside that funtion.

create 2 functions:

void fun( int& i )
{
// original function
}

and the second one, which handles the default parameter

void fun()
{
int default = 8;
fun( default );
}
 
R

Rolf Magnus

rakoo said:
is there is any ways i pass default value to reference variable

suct as fun(const int &i=8) as i can't do fun ( int &i=8)

How would you expect that to work? i is just a reference to a variable
existing somewhere else. 8 is not a variable. It's an integer literal.
and modify variable i ,inside that funtion.

You can't modify it anyway if it's const.
 
K

Kyle

rakoo said:
is there is any ways i pass default value to reference variable

suct as fun(const int &i=8) as i can't do fun ( int &i=8)


and modify variable i ,inside that funtion.

rakesh Kushwaha

perhaps:

const int eight = 8;

void foo( const int& = eight );
 
K

Kaz Kylheku

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.
 
O

Old Wolf

Kaz said:
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

Actually it isn't equivalent. The following code is legal:
void foo(int x = 3) { }
void foo() { abort(); }

Also this code is illegal:
void foo(int x = 3) { }
void (*ptr)() = &foo;

If things were as you said they were, then my first snippet would
be illegal and the second would be legal.
 
R

rakoo

see real problem is like this suppose

fun1( )
{
f(i) ;

}

fun2()
{
f(i);
}

fun3()
{
// let j=1 ;
f(i,j) ; // fun1 and fun2 don't need that parameter so i want
to hide

if( j==1)
j=2;
}

can i put f( int i, int &j=0)

but i can't do that i have to do f(int ,const &j=0)

but then i can modify j into fun3();
 
R

rakoo

see real problem is like this suppose

fun1( )
{
f(i) ;

}

fun2()
{
f(i);
}

fun3()
{
// let j=1 ;
f(i,j) ; // fun1 and fun2 don't need that parameter so i want
to hide

if( j==1)
j=2;
}

can i put f( int i, int &j=0)

but i can't do that i have to do f(int ,const &j=0)

but then i cann't modify j into fun3();
 
R

rakoo

see real problem is like this suppose

fun1( )
{
f(i) ;

}

fun2()
{
f(i);
}

fun3()
{
// let j=1 ;
f(i,j) ; // fun1 and fun2 don't need that parameter so i want
to hide

if( j==1)
j=2;
}

can i put f( int i, int &j=0)

but i can't do that i have to do f(int ,const &j=0)

but then i cann't modify j into fun3();
 
R

rakoo

see real problem is like this suppose

fun1( )
{
f(i) ;

}

fun2()
{
f(i);
}

fun3()
{
// let j=1 ;
f(i,j) ; // fun1 and fun2 don't need that parameter so i want
to hide

if( j==1)
j=2;
}

can i put f( int i, int &j=0)

but i can't do that i have to do f(int ,const &j=0)

but then i cann't modify j into fun3();
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top