470,841 Members | 1,089 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,841 developers. It's quick & easy.

Can I pass addres of constant reference to a function in c++.

Hi,
Need help in solving issue.

I m having one function:

int alllowReset(TRAN & tranP)
{
if( tranP->name == "PQRS" ||
...
..
)
rerurn TRUE;
else
return FALSE;
}
Now I want to call this function, but in code only const refernce of
TRAN is available.

Is it possible if I will call this function with const reference.
Or will it have some memory issues.

Jun 1 '06 #1
5 1683

Joyti wrote:
Hi,
Need help in solving issue.

I m having one function:

int alllowReset(TRAN & tranP)
{
if( tranP->name == "PQRS" ||
...
..
)
rerurn TRUE;
else
return FALSE;
}
Now I want to call this function, but in code only const refernce of
TRAN is available.

Is it possible if I will call this function with const reference.
Or will it have some memory issues.


You shoud never use
if (tranP->name =="PQRS" ....

But use this instead,
if (!strcmp(tranP->name, "PQRS") || ....

or you can use strncmp(), it depends on you

Jun 1 '06 #2
Joyti wrote:
Hi,
Need help in solving issue.

I m having one function:

int alllowReset(TRAN & tranP)
{
if( tranP->name == "PQRS" ||
...
..
)
rerurn TRUE;
else
return FALSE;
}
Now I want to call this function, but in code only const refernce of
TRAN is available.
usually it is not advisable to pass a const object to a function which
takes a non constant object reference. The idea has to do with the
design.
The function takes a non constant reference that means it can change
the value of the object.
But the object is constant, which means the state of the object should
not change, thats why you have declared it constant.
you need to look at your design once more to be sure whether this is
what you want.
Is it possible if I will call this function with const reference.
Or will it have some memory issues.

Possible solution:
PS: i have taken the liberty to change some constructs just to compile
my code.
function defined :

int allowReset(TRAN & tranP)
{
if( tranP.name.compare("PQRS"))
return true;
return false;
}

calling code from main:
const TRAN tran;
allowReset( const_cast<TRAN&>(tran) );

Regards,
Anon.

Jun 1 '06 #3

Joyti wrote:
Hi,
Need help in solving issue.

I m having one function:

int alllowReset(TRAN & tranP)
{
if( tranP->name == "PQRS" ||
...
..
)
rerurn TRUE;
else
return FALSE;
}
Now I want to call this function, but in code only const refernce of
TRAN is available.
Your code does not make much sense. rerurn TRUE is not valid, and
neither is return FALSE unless you have made silly defines such as
#define TRUE true.
Also the code is only valid if the -> operator is defined for the TRAN
class (or TRAN is yet another silly #define).

That being said, perhaps the problem is that alllllowReset should have
a const reference as input? Something like
bool alllowReset(TRAN const& tranP) looks more sensible to me.

Is it possible if I will call this function with const reference.
Or will it have some memory issues.


Jun 1 '06 #4

Prawit Chaivong wrote:

You shoud never use
if (tranP->name =="PQRS" ....

But use this instead,
if (!strcmp(tranP->name, "PQRS") || ....

or you can use strncmp(), it depends on you


depends on what tranP->name is. (Note that as tranP is a reference,
operator-> must be overloaded. Do you mean tranP.name() ? )

Code is not const-correct if it takes a non-const reference and doesn't
modify it, unless it is a virtual function where some implementations
will commonly modify the referenced object.

Jun 1 '06 #5
Joyti posted:

Now I want to call this function, but in code only const refernce of
TRAN is available.

If the function in question doesn't alter the object, then it should take a
reference to const.

If it does alter the object, and you do something akin to the following:
int main()
{
std::string const str("ajslj");

Func( const_cast<str&>(str) );
}
Then you'll have undefined behaviour.

-Tomás
Jun 1 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Kent | last post: by
23 posts views Thread by Sanjay Kumar | last post: by
14 posts views Thread by Abhi | last post: by
10 posts views Thread by Robert Dailey | last post: by
2 posts views Thread by soukat | last post: by
14 posts views Thread by Siegfried Heintze | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.