linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Shriramana Sharma <samjnaa@gmail.com>
To: Linux C Programming List <linux-c-programming@vger.kernel.org>
Subject: operator for automatic type conversion not allowed as non-member
Date: Mon, 16 Jul 2007 11:35:09 +0530	[thread overview]
Message-ID: <469B0A95.4040108@gmail.com> (raw)

In my program, I would like QString-s (from Qt) to be automatically 
converted to std::string-s. The Qt people could have done this by 
providing an operator std::string () inside class QString but they 
didn't so I tried to do this using a global operator.

operator std::string (const QString & qs) { return qs.toStdString() ; }

but I got:

error: ‘operator std::string(const QString&)’ must be a nonstatic member 
function

Whereas if I try:

const QString operator+ (const std::string & ss, const QString & qs) {
	return QString::fromStdString(ss) + qs ;
}

it works. So what is special about operator othertype that it is not 
allowed to be a non-member?

Similarly operator= is not allowed to be a non-member. So I cannot do:

const std::string & operator= ( std::string & ss, const QString & qs ) {
	ss = qs.toStdString() ;
	return ss ;
}

which is actually meaningful. Of course, since QString has toStdString, 
I can always use that wherever I need to get a std::string from QString, 
but operator overloading is a matter of convenience and I would like to 
know if there is a strong reason that I cannot use operator othertype 
and operator= as a non-member. It would enable me to define convenience 
operators between objects of third-party (which includes the standard 
library for me) types.

Shriramana Sharma.

-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

             reply	other threads:[~2007-07-16  6:05 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-07-16  6:05 Shriramana Sharma [this message]
2007-07-16 15:54 ` operator for automatic type conversion not allowed as non-member Glynn Clements

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=469B0A95.4040108@gmail.com \
    --to=samjnaa@gmail.com \
    --cc=linux-c-programming@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).