473,387 Members | 1,561 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

passing strings

Suppose I have an object that is concatinatable (sp) using the '+'
operator (eg string). I have a function(func)that will do some work
to the object and return an int. For several reasons it is desirable
to have the ability to call the function like this:
int myvalue = func(obj1 + obj2);

because of this am I limited to passing by value? If I demand a pass
by reference, must my calling procedure look like this,

AnObj temp = obj1 + obj2;
intmyvalue = func(temp);

rather than concatinating inside the function parentheses? This second
method defeats the purpose of pass-by-reference, since it creates a
new object anyway.

I realize that I could make the function declaration look like
int func(AnObj&, AnObj&);
but this is undesirable since the function really only needs one
AnObj.

Any advice?

Joe
Jul 22 '05 #1
6 1392
"J. Campbell" <ma**********@yahoo.com> wrote...
Suppose I have an object that is concatinatable (sp) using the '+'
operator (eg string). I have a function(func)that will do some work
to the object and return an int. For several reasons it is desirable
to have the ability to call the function like this:
int myvalue = func(obj1 + obj2);

because of this am I limited to passing by value?
No, you may pass by reference to a const object. A temporary will
be created. Make sure your operator + returns an object.
If I demand a pass
by reference, must my calling procedure look like this,

AnObj temp = obj1 + obj2;
intmyvalue = func(temp);

rather than concatinating inside the function parentheses? This second
method defeats the purpose of pass-by-reference, since it creates a
new object anyway.
Passing by a reference to const object allows you to avoid creating
an object (the system will do it for you). A reference to non-const
object cannot be used because it cannot be bound to a temporary.
I realize that I could make the function declaration look like
int func(AnObj&, AnObj&);
but this is undesirable since the function really only needs one
AnObj.

Any advice?


See above. HTH.

Victor
Jul 22 '05 #2

"J. Campbell" <ma**********@yahoo.com> wrote in message
news:b9**************************@posting.google.c om...
Suppose I have an object that is concatinatable (sp) using the '+'
operator (eg string). I have a function(func)that will do some work
to the object and return an int. For several reasons it is desirable
to have the ability to call the function like this:
int myvalue = func(obj1 + obj2);

because of this am I limited to passing by value? If I demand a pass
by reference, must my calling procedure look like this,

AnObj temp = obj1 + obj2;
intmyvalue = func(temp);

rather than concatinating inside the function parentheses? This second
method defeats the purpose of pass-by-reference, since it creates a
new object anyway.

I realize that I could make the function declaration look like
int func(AnObj&, AnObj&);
but this is undesirable since the function really only needs one
AnObj.

Any advice?

Joe


Well, I'm not sure exactly what your goal is so I'm not sure I can offer
advice that will help, but I'll offer a couple of observations...

You noted the creation of a second object in this case:

AnObj temp = obj1 + obj2;
intmyvalue = func(temp);
Well, there is a second object created in this case too:

int myvalue = func(obj1 + obj2);

In this case, the object is an unseen temorary, but it is real nonetheless.
For this reason, you could pass only by *const* reference. A non-const
reference may not be bound to a temporary, only a const reference may be.
And of course, if your goal is to have the called function modify its
parameter, you're SOL. If, however, you're not wanting to pass by reference
to have the called function modify its parameter but rather for efficiencies
sake, you could then change the parameter to a const reference and make the
call func(obj1 + obj2);.

So, I'm not sure if this will help but I hope it does...
Jul 22 '05 #3
"J. Campbell" <ma**********@yahoo.com> wrote in message
news:b9**************************@posting.google.c om...
Suppose I have an object that is concatinatable (sp) using the '+'
operator (eg string). I have a function(func)that will do some work
to the object and return an int. For several reasons it is desirable to have the ability to call the function like this:
int myvalue = func(obj1 + obj2);

because of this am I limited to passing by value? <snip>
No, you can pass by const reference.
If I demand a pass
by reference, must my calling procedure look like this,

AnObj temp = obj1 + obj2;
intmyvalue = func(temp);

rather than concatinating inside the function parentheses? This second method defeats the purpose of pass-by-reference, since it creates a
new object anyway.

The expression obj1 + obj2 creates a temporary object even if it is
called using

int myvalue = func(obj1 + obj2);

The problem is that f (as currently formulated) needs a string object
representing the concatenated value, and none exists, so one must be
created somewhere.

If you don't ming modifying obj1, you can write:

int myvalue = func(obj1 += obj2);
I realize that I could make the function declaration look like
int func(AnObj&, AnObj&);
but this is undesirable since the function really only needs one
AnObj.


If f does need actually need to construct a concatenated string, but
can do its job by inspecting obj1 and obj2, then your last suggestion
is probably the way to go.

Jonathan
Jul 22 '05 #4
J. Campbell wrote:
Suppose I have an object that is concatinatable (sp) using the '+'
operator (eg string). I have a function(func)that will do some work
to the object and return an int. For several reasons it is desirable
to have the ability to call the function like this:
int myvalue = func(obj1 + obj2);

because of this am I limited to passing by value? If I demand a pass
by reference, must my calling procedure look like this,

AnObj temp = obj1 + obj2;
intmyvalue = func(temp);

rather than concatinating inside the function parentheses? This second
method defeats the purpose of pass-by-reference, since it creates a
new object anyway.


The expression "ob1 + ob2" should always create a temporary, it
shouldn't modify ob1 or ob2[*]. And as you say, one can't pass a
temporary to a function expecting T&. You may be able to pass
a temporary to a function expecting const T&, but you would
still create a temporary by using operator+.
If you just want to modify ob1 and pass that in, declare an
appropriate += operator, and call func(obj1+=obj2).
[*] The reason obj1+obj2 shouldn't modify its arguments is because
(IMHO) it's important for libraries to have a consitent flavour. It's
important that things work the way a user intuitively expects them to
work.
T i = 1, j = 4;
T k = i + j;
If operator+ modified either i or j you would quickly have users
abandoning your library in disgust because of this wholly unneccessary
quirk which makes it harder to learn.

Jacques

Jul 22 '05 #5

"Jonathan Turkanis" <te******@kangaroologic.com> wrote in message
news:bu************@ID-216073.news.uni-berlin.de...
"J. Campbell" <ma**********@yahoo.com> wrote in message
news:b9**************************@posting.google.c om...
Suppose I have an object that is concatinatable


<snip>

You guys are magic. Thanks for the speedy replies. When I read about
tempories, I didn't have enough basis to understand...so it did't sink
in...I guess this is the reason to continue reading reference material as
your knowledge-base increases, no?

Anyway...my base question was about wanting to avoid passing heavy objects,
and it appears that in this case, it cannot be avoided unless I pass a
reference for each object and don't concatinate.

Thanks very much for the help.
Joe
Jul 22 '05 #6
"Joe C" <jk*****@bellsouth.net> wrote...

"Jonathan Turkanis" <te******@kangaroologic.com> wrote in message
news:bu************@ID-216073.news.uni-berlin.de...
"J. Campbell" <ma**********@yahoo.com> wrote in message
news:b9**************************@posting.google.c om...
Suppose I have an object that is concatinatable

<snip>

You guys are magic. Thanks for the speedy replies. When I read about
tempories, I didn't have enough basis to understand...so it did't sink
in...I guess this is the reason to continue reading reference material as
your knowledge-base increases, no?

Anyway...my base question was about wanting to avoid passing heavy

objects, and it appears that in this case, it cannot be avoided unless I pass a
reference for each object and don't concatinate.


Without knowing what your function does, I'll take your word for it.
Yes, usually addition creates another object from the two involved.
However, you _could_ create such object that serves as a _result_ of
addition and stores the _references_ or pointers to the original two
objects, and does some tricks when asked for the value. I'll give
you an example based on std::string.

#include <string>

class ConcatResult {
std::string const &s1;
std::string const &s2;
public:
ConcatResult(std::string const& s1, std::string const& s2)
: s1(s1), s2(s2) {}
char operator [](std::string::size_type i) const {
if (i < s1.length())
return s1[i];
else
return s2[i - s1.length()];
}

int length() const { return s1.length() + s2.length(); }
operator std::string() const { return s1 + s2; } // lazy
};

int foo(ConcatResult const &pseudoString) {
return pseudoString.length();
}

#include <iostream>
int main() {
std::string s1("abc"), s2("def");

std::cout << "The concatenated string would be "
<< foo(ConcatResult(s1,s2)) << " chars long\n";
}

HTH

Victor
Jul 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: harry | last post by:
I have 2 multi-dim arrays double subTotals = null; String rowTitles = null; I want to pass them to a function that initialises & populates them like so - loadData( rowTitles, subTotals);
2
by: muser | last post by:
How can I pass the parameter " long part_num, into a case statement. Case statement follows the function CheckDigit. i.e. CheckDigit( something, temp1 ); Thank you for your help in advance. ...
4
by: Suddn | last post by:
Help me get my mind around passing string types to a function. I need to have the function modify the string types and get them back. Normaly I would just return the modified string but I need to...
5
by: Jack | last post by:
Hi, I need to pass multple variables in a link in order to go to a asp page with the two varables. The following are the values of the variables using response.write: <%'Response.Write Mypage...
39
by: Mike MacSween | last post by:
Just spent a happy 10 mins trying to understand a function I wrote sometime ago. Then remembered that arguments are passed by reference, by default. Does the fact that this slowed me down...
3
by: Mark | last post by:
Hi From what I understand, you can pass arrays from classic ASP to .NET using interop, but you have to change the type of the.NET parameter to object. This seems to be because classic ASP passes...
0
by: Dave Cullen | last post by:
I have a dll written in VC6 that I need to send arguments to from a call in VB.NET. I'm having trouble passing data arguments between them. So far I've only tried passing strings, and all I get...
61
by: academic | last post by:
When I declare a reference variable I initialize it to Nothing. Now I'm wondering if that best for String variables - is "" better? With Nothing I assume no memory is set aside nor GC'ed But...
6
by: Andy Baker | last post by:
I am attempting to write a .NET wrapper for a C++ DLL file, but am having problems with passing strings as parameters. How should I be writing my C# function call when the C header file is...
4
by: arnuld | last post by:
I am passing an array of struct to a function to print its value. First I am getting Segfaults and weired values. 2nd, is there any elegant way to do this ? /* Learning how to use an array...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.