Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Accessing Lvalue from function

Reply
Thread Tools

Accessing Lvalue from function

 
 
dogpuke@gmail.com
Guest
Posts: n/a
 
      01-20-2006
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?

 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      01-20-2006
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;
}

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      01-20-2006
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
 
Reply With Quote
 
dogpuke@gmail.com
Guest
Posts: n/a
 
      01-20-2006
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.

 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      01-20-2006
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Accessing member of non-lvalue union Johannes Schaub (litb) C Programming 1 11-13-2010 11:40 AM
using a method as an lvalue and "invalid lvalue in assignment"compilation error markryde@gmail.com C Programming 11 09-22-2008 10:42 AM
Possibility to define a function that could be used as lvalue giuliano C Programming 13 01-16-2007 08:16 AM
operators requiring lvalue/rvalue operands and resulting in rvalue/lvalue Kavya C Programming 9 10-28-2006 01:45 AM
Perl hangs when returning lvalue closure from another lvalue closure Julian Mehnle Perl Misc 0 07-17-2003 03:13 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57