Accessing Lvalue from function

Discussion in 'C++' started by dogpuke@gmail.com, Jan 20, 2006.

  1. Guest

    I have a class CString. I'm wondering if it's possible to make a global
    function mystr_cat that does this:

    CString s1 = "hello";
    s1 = mystr_cat("another", "string", "here");

    Thus mystr_cat needs to access the "this" part of s1. Or maybe = can be
    overloaded? Or is this type of thing not allowed?
     
    , Jan 20, 2006
    #1
    1. Advertising

  2. Rolf Magnus Guest

    wrote:

    > I have a class CString. I'm wondering if it's possible to make a global
    > function mystr_cat that does this:
    >
    > CString s1 = "hello";
    > s1 = mystr_cat("another", "string", "here");
    >
    > Thus mystr_cat needs to access the "this" part of s1. Or maybe = can be
    > overloaded? Or is this type of thing not allowed?


    That may be possible by overloading operator=, but it is very unintuitive. A
    user of your class would normally expect operator= to completely replace
    the contents of your string. Why not simply:

    s1.cat("another", "string", "here");

    ? Then you can simply make it a member function. Or alternatively, to elide
    the need for lots of overloads for different argument numbers or variable
    argument lists, you could do something like:

    s1.cat("another").cat("string").cat("here");

    by making a function like:

    CString& CString::cat(const char* arg)
    {
    //...append arg to your string
    return *this;
    }
     
    Rolf Magnus, Jan 20, 2006
    #2
    1. Advertising

  3. Rolf Magnus wrote:
    > wrote:
    >
    >
    >>I have a class CString. I'm wondering if it's possible to make a global
    >>function mystr_cat that does this:
    >>
    >>CString s1 = "hello";
    >>s1 = mystr_cat("another", "string", "here");
    >>
    >>Thus mystr_cat needs to access the "this" part of s1. Or maybe = can be
    >>overloaded? Or is this type of thing not allowed?

    >
    >
    > That may be possible by overloading operator=,


    It is not possible to define the operator= as a non-member. So, if the
    class 'CString' (looks very much like MFC's CString) is closed to you
    (IOW, you can't make changes to it), then overloading operator= is not
    an option.

    > but it is very unintuitive. A
    > user of your class would normally expect operator= to completely replace
    > the contents of your string. Why not simply:
    >
    > s1.cat("another", "string", "here");
    >
    > ? Then you can simply make it a member function. Or alternatively, to elide
    > the need for lots of overloads for different argument numbers or variable
    > argument lists, you could do something like:
    >
    > s1.cat("another").cat("string").cat("here");
    >
    > by making a function like:
    >
    > CString& CString::cat(const char* arg)
    > {
    > //...append arg to your string
    > return *this;
    > }
    >


    Again, this all assumes CString is open for making changes.

    Generally speaking, if the class is closed, you have the option to pass
    the object you need to change into the function by reference or simply
    return an object of that class from the function and hope that compiler
    generates effective code for copying.

    V
     
    Victor Bazarov, Jan 20, 2006
    #3
  4. Guest

    CString is my own class from scratch. I should have (and will) change
    the name to something else, such as MYString.

    If a member function is the only way, then that's fine. I'm interested
    in doing it the way I originally asked, even if not seemingly
    intuitive, not just to accomplish this one thing, but to understand
    what is possible. That functionality can help in other unrelated areas.

    My attempts to get there by overloading the = operator with a global
    function failed.
     
    , Jan 20, 2006
    #4
  5. Rolf Magnus Guest

    wrote:

    > CString is my own class from scratch. I should have (and will) change
    > the name to something else, such as MYString.
    >
    > If a member function is the only way, then that's fine. I'm interested
    > in doing it the way I originally asked, even if not seemingly
    > intuitive, not just to accomplish this one thing, but to understand
    > what is possible. That functionality can help in other unrelated areas.
    >
    > My attempts to get there by overloading the = operator with a global
    > function failed.


    Ok. You can do it in such a way that your original syntax can work. Let
    mystr_cat return a proxy object that contains the arguments already
    concatenated. Then overload an operator= for your string type that gets
    this proxy object as argument. In the operator=, you can append the
    contents of the proxy to your string.
     
    Rolf Magnus, Jan 20, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kavya
    Replies:
    9
    Views:
    541
    Dik T. Winter
    Oct 28, 2006
  2. giuliano
    Replies:
    13
    Views:
    427
    CBFalconer
    Jan 16, 2007
  3. Replies:
    11
    Views:
    716
    James Kuyper
    Sep 22, 2008
  4. Johannes Schaub (litb)

    Accessing member of non-lvalue union

    Johannes Schaub (litb), Nov 13, 2010, in forum: C Programming
    Replies:
    1
    Views:
    334
    Ben Bacarisse
    Nov 13, 2010
  5. Julian Mehnle
    Replies:
    0
    Views:
    259
    Julian Mehnle
    Jul 17, 2003
Loading...

Share This Page