Accessing Lvalue from function

D

dogpuke

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?
 
R

Rolf Magnus

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;
}
 
V

Victor Bazarov

Rolf said:
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
 
D

dogpuke

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

Rolf Magnus

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top