By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,429 Members | 1,566 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,429 IT Pros & Developers. It's quick & easy.

Reference varible

P: n/a


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

Nov 9 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
rakoo wrote:

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 );
}
rakesh Kushwaha

--
Karl Heinz Buchegger
kb******@gascad.at
Nov 9 '05 #2

P: n/a
rakoo wrote:


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.

Nov 9 '05 #3

P: n/a
rakoo wrote:

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 );
Nov 9 '05 #4

P: n/a
rakoo wrote:
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.

Nov 9 '05 #5

P: n/a
Kaz Kylheku wrote:
rakoo wrote:
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)


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.

Nov 10 '05 #6

P: n/a
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();

Nov 22 '05 #7

P: n/a
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();

Nov 22 '05 #8

P: n/a
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();

Nov 22 '05 #9

P: n/a
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();

Nov 22 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.