473,789 Members | 2,860 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(TRA N & 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 1846

Joyti wrote:
Hi,
Need help in solving issue.

I m having one function:

int alllowReset(TRA N & 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(TRA N & 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.comp are("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(TRA N & 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(TRA N 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
9995
by: Kent | last post by:
Hi! I am working with an obscure graphics library in school as a university C++ programming project. The graphics library has a class kalled Bitmap wich you use to draw graphics (you can "stamp it down" on the screen, double buffering and such is all within the lib). This is how it works now (i think its a bad design): In a class i created to represent "monsters" in a game i have the following
110
9966
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object must be an object instead of
23
6551
by: Sanjay Kumar | last post by:
Folks, I am getting back into C++ after a long time and I have this simple question: How do pyou ass a STL container like say a vector or a map (to and from a function) ? function: vector<string> tokenize(string s){
14
20413
by: Abhi | last post by:
I wrote a function foo(int arr) and its prototype is declared as foo(int arr); I modify the values of the array in the function and the values are getting modified in the main array which is passed also. I understand that this way of passing the array is by value and if the prototype is declared as foo(int *), it is by reference in which case the value if modified in the function will get reflected in the main function as well. I dont...
10
13663
by: Robert Dailey | last post by:
Hi, I noticed in Python all function parameters seem to be passed by reference. This means that when I modify the value of a variable of a function, the value of the variable externally from the function is also modified. Sometimes I wish to work with "copies", in that when I pass in an integer variable into a function, I want the function to be modifying a COPY, not the reference. Is this possible?
2
1610
by: soukat | last post by:
i have declear one structure typedef struct { char name; int age;} INFO another file and create one structure vasrable str1 in main, I want to pass the addres for another function linke fun( &str1); in this function i am read data from a file like void fun ( INFO * str ) READ (HANDLE, &str,sizeof(str1)); that is write or not ? i wnat to print info in main
11
3365
by: venkatagmail | last post by:
I have problem understanding pass by value and pass by reference and want to how how they are or appear in the memory: I had to get my basics right again. I create an array and try all possible ways of passing an array. In the following code, fun1(int a1) - same as fun1(int* a1) - where both are of the type passed by reference. Inside this function, another pointer a1 is created whose address &a1 is different from that of the passed...
14
4838
by: Siegfried Heintze | last post by:
Why does VB.NET V2 force me to pass by value for my set function? When I try to change it to const byref it gives me a syntax error. It seems very inefficient to be passing strings around by value when a reference to a constant string object will do fine (all we are going to do is copy it). In my case, would the byval for the set function cause a superfluous copy? Thanks, Siegfried class person dim m_name as string
9
2063
by: raylopez99 | last post by:
I'm posting this fragment from another thread to frame the issue clearer. How to pass an object to a function/method call in C# that will guarantee not to change the object?* In C++, as seen below, you can use the 'const' keyword in the function / method declaration. But how to do this in C#? *for example: "void Foo() const;"
0
9666
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10410
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10139
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9984
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9020
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6769
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5418
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4093
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2909
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.