LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-27-2009, 09:22 AM   #1
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Need C++ help with overloading definition of a value


The title is probably misleading, but here is what I am trying to do... and of course it doesn't compile. Any idea why I cannot get a TokenString to auto-convert to an std::string or const char* that can be used for string assignment?

Code:
#include <string>
#include <cstdlib>

class TokenString
{
  public:
    TokenString(const std::string& str) : m_str(str) {}

    operator std::string() const  { return m_str; }
    operator int() const          { return atoi(m_str.c_str()); }
    operator unsigned int() const { return atoi(m_str.c_str()); }
    operator double() const       { return strtod(m_str.c_str(), 0); }
    operator const char*() const  { return m_str.c_str(); }

  private:
    std::string m_str;
};

int main()
{
  TokenString tk("foo");

  // This is ok
  std::string str1 = tk;

  // This is ok
  std::string str2;
  str2 = "foo";

  // This is NOT ok
  std::string str3;
  str3 = tk;           <--- Will not compile!
}
 
Old 02-27-2009, 09:33 AM   #2
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
It's an ambiguous overload. std::string has assignment operator overloads for const std::string &, const char *, and char, and can't decide which combination of conversion operators and assignment operators to use.

I suggest you do what some boost code does: create a template as() member function which is specialized for each kind of conversion. Or for that matter, use std::string rather than a special type and boost::lexical_cast to convert to whatever.
 
Old 02-27-2009, 09:40 AM   #3
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Original Poster
Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Thanks for the suggestion, however I do not have Boost available on my MontaVista system.

The only way I have found to "resolve" the problem is to wrap the token string into an std::string.
Code:
...

  // Now this is ok
  std::string str3;
  str3 = std::string(tk);
 
  


Reply

Tags
c++



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ overloading question Jack_chen Programming 7 07-30-2007 12:42 AM
overloading vs polymorphism lbdgwgt Programming 1 03-06-2007 02:30 PM
overloading a template allomeen Programming 1 01-26-2006 06:59 PM
c++ [] operator overloading exodist Programming 12 04-17-2004 03:06 PM
<< overloading lazyboy0001 Programming 2 04-10-2004 07:25 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:30 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration