passing strings

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • J. Campbell

    passing strings

    Suppose I have an object that is concatinatable (sp) using the '+'
    operator (eg string). I have a function(func)t hat 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
  • Victor Bazarov

    #2
    Re: passing strings

    "J. Campbell" <mango_maniac@y ahoo.com> wrote...[color=blue]
    > Suppose I have an object that is concatinatable (sp) using the '+'
    > operator (eg string). I have a function(func)t hat 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?[/color]

    No, you may pass by reference to a const object. A temporary will
    be created. Make sure your operator + returns an object.
    [color=blue]
    > 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.[/color]

    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.
    [color=blue]
    > 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?[/color]

    See above. HTH.

    Victor


    Comment

    • Dave

      #3
      Re: passing strings


      "J. Campbell" <mango_maniac@y ahoo.com> wrote in message
      news:b97c86e1.0 401161228.7756f c15@posting.goo gle.com...[color=blue]
      > Suppose I have an object that is concatinatable (sp) using the '+'
      > operator (eg string). I have a function(func)t hat 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[/color]

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


      Comment

      • Jonathan Turkanis

        #4
        Re: passing strings

        "J. Campbell" <mango_maniac@y ahoo.com> wrote in message
        news:b97c86e1.0 401161228.7756f c15@posting.goo gle.com...[color=blue]
        > Suppose I have an object that is concatinatable (sp) using the '+'
        > operator (eg string). I have a function(func)t hat will do some work
        > to the object and return an int. For several reasons it is[/color]
        desirable[color=blue]
        > 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>[/color]

        No, you can pass by const reference.
        [color=blue]
        > 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[/color]
        second[color=blue]
        > method defeats the purpose of pass-by-reference, since it creates a
        > new object anyway.
        >[/color]

        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);
        [color=blue]
        > 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.
        >[/color]

        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


        Comment

        • Jacques Labuschagne

          #5
          Re: passing strings

          J. Campbell wrote:[color=blue]
          > Suppose I have an object that is concatinatable (sp) using the '+'
          > operator (eg string). I have a function(func)t hat 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.[/color]

          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

          Comment

          • Joe C

            #6
            Re: passing strings


            "Jonathan Turkanis" <technews@kanga roologic.com> wrote in message
            news:bu9i40$ffg m9$1@ID-216073.news.uni-berlin.de...[color=blue]
            > "J. Campbell" <mango_maniac@y ahoo.com> wrote in message
            > news:b97c86e1.0 401161228.7756f c15@posting.goo gle.com...[color=green]
            > > Suppose I have an object that is concatinatable[/color][/color]

            <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


            Comment

            • Victor Bazarov

              #7
              Re: passing strings

              "Joe C" <jkc8289@bellso uth.net> wrote...[color=blue]
              >
              > "Jonathan Turkanis" <technews@kanga roologic.com> wrote in message
              > news:bu9i40$ffg m9$1@ID-216073.news.uni-berlin.de...[color=green]
              > > "J. Campbell" <mango_maniac@y ahoo.com> wrote in message
              > > news:b97c86e1.0 401161228.7756f c15@posting.goo gle.com...[color=darkred]
              > > > Suppose I have an object that is concatinatable[/color][/color]
              >
              > <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[/color]
              objects,[color=blue]
              > and it appears that in this case, it cannot be avoided unless I pass a
              > reference for each object and don't concatinate.[/color]

              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(st d::string const& s1, std::string const& s2)
              : s1(s1), s2(s2) {}
              char operator [](std::string::s ize_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(ConcatResul t const &pseudoStrin g) {
              return pseudoString.le ngth();
              }

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

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

              HTH

              Victor


              Comment

              Working...