Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 833869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T04:35:11+00:00 2026-05-15T04:35:11+00:00

Recently, I was browsing through my copy of the C++ Pocket Reference from O’Reilly

  • 0

Recently, I was browsing through my copy of the C++ Pocket Reference from O’Reilly Media, and I was surprised when I came across a brief section and example regarding user-defined conversion for user-defined types:

#include <iostream>

class account {

    private:
        double balance;

    public:
        account (double b) { balance = b; }

        operator double (void) { return balance; }
};

int main (void) {

    account acc(100.0);
    double balance = acc;

    std::cout << balance << std::endl;

    return 0;
}

I’ve been programming in C++ for awhile, and this is the first time I’ve ever seen this sort of operator overloading. The book’s description of this subject is somewhat brief, leaving me with a few unanswered questions about this feature:

  • Is this a particularly obscure feature? As I said, I’ve been programming in C++ for awhile and this is the first time I’ve ever come across this. I haven’t had much luck finding more in-depth material regarding this.
  • Is this relatively portable? (I’m compiling on GCC 4.1)
  • Can user-defined conversions to user defined types be done? e.g.

    operator std::string () { /* code */ }

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T04:35:12+00:00Added an answer on May 15, 2026 at 4:35 am

    Is this a particularly obscure feature?

    Yes, conversion operators aren’t used very often. The places I’ve seen them are for user-defined types that can degrade to built-in ones. Things like a fixed-precision number class that supports converting to/from atomic number types.

    Is this relatively portable?

    As far as I know, it is. They’ve been in the standard forever.

    Can user-defined conversions to user defined types be done?

    Yes, that’s one of the features of constructors. A constructor that takes a single argument effectively creates a conversion operator from the argument type to your class’s type. For example, a class like this:

    class Foo {
    public:
        Foo(int n) {
            // do stuff...
        }
    }
    

    Let’s you do:

    Foo f = 123;
    

    If you’ve used std::string before, odds are you’ve used this feature without realizing it. (As an aside, if you want to prevent this behavior, declare any single-argument constructors using explicit.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Recently I found a pretty graph when I was browsing the adminskins at ThemeForest
I'm really new to programming, and creating programs. Recently I was browsing around iTunes
Recently Jeff has posted regarding his trouble with database deadlocks related to reading. Multiversion
Recently I have been investigating the possibilities of caching in ASP.NET. I rolled my
Recently, I started changing some of our applications to support MS SQL Server as
Recently, I've been dealing with an error with accessing MAPI via the .NET framework
Recently our site has been deluged with the resurgence of the Asprox botnet SQL
Recently I had to develop a SharePoint workflow, and I found the experience quite
Recently we got a new server at the office purely for testing purposes. It
Recently, I read an article entitled SATA vs. SCSI reliability . It mostly discusses

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.