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

J

Joyti

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

Prawit Chaivong

Joyti said:
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
 
G

gangs

Joyti said:
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.
 
P

peter koch

Joyti said:
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.
 
E

Earl Purple

Prawit said:
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.
 
T

Tomás

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
 

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
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top