Accessing Lvalue from function



 DEVELOP > c-Plus-Plus > Accessing Lvalue from function

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: ""
Date: 20 Jan 2006 07:58:51 AM
Object: Accessing Lvalue from function
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?
.

User: "Rolf Magnus"

Title: Re: Accessing Lvalue from function 20 Jan 2006 08:06:08 AM
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;
}
.
User: "Victor Bazarov"

Title: Re: Accessing Lvalue from function 20 Jan 2006 08:51:03 AM
Rolf Magnus wrote:

dogpuke@gmail.com 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
.
User: ""

Title: Re: Accessing Lvalue from function 20 Jan 2006 09:23:11 AM
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.
.
User: "Rolf Magnus"

Title: Re: Accessing Lvalue from function 20 Jan 2006 11:05:48 AM
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.
.





  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER