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 8682559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:48:34+00:00 2026-06-12T21:48:34+00:00

Why do the following two usecases behave differently? An example for a built-in type:

  • 0

Why do the following two usecases behave differently?

An example for a built-in type:

class Test
{
    operator const char*() {
        return "operator const char*() called";
    }

    operator char*() {
        return const_cast<char*> ("operator char*() called");
        // a really ugly hack for demonstration purposes
    }
};

Test test;
const char *c_ch = test;  // -> operator const char*() called
char *ch = test;          // -> operator char*() called

Okay, everything works as it should. No, let’s try it with a user-defined type:

class ColorWrapper
{
    operator const Color() {
        cout << "operator const Color() called" << endl;
        return Color(10, 20, 30);
    }

    operator Color() {
        cout << "operator Color() called" << endl;
        return Color(11, 22, 33);
    }
};

ColorWrapper cw;
const Color c_col = cw;
Color col = cw;

The situation looks identical; but now, GCC starts complaining:

error: conversion from 'ColorWrapper' to 'const Color' is ambiguous
error: conversion from 'ColorWrapper' to 'Color' is ambiguous

Am I missing something? What I’m trying to achieve here is to prevent the user from changing the color directly. If they want to change it, they must do it via the wrapper class.

  • 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-06-12T21:48:35+00:00Added an answer on June 12, 2026 at 9:48 pm

    The thing is, that your two examples are not equivalent. The first returns two different types from which the compiler can easily choose, a pointer to mutable data and a pointer to constant data. It would be different, was it a pointer vs a constant pointer (which would mean you cannot change where the returned pointer points, in contrast to not changing the pointed to data):

    class Test
    {
        operator char * const();
        operator char *();
    };
    

    which would be equivalent to your second example and introduce the same ambiguity, since the compiler cannot decide which one to choose, as both return the same type, one const-qualified and the other not.

    This could be solved by overloading based on the constness of the object:

    class ColorWrapper
    {
        operator const Color() const;
        operator Color();
    };
    

    But this won’t buy you anything here, since you return a new object by value anyway, so you can always return the const version, and also the appropriate version would be chosen based on the source object’s constness and not the destination obejct’s constness (thus call the non-const version in both cases), which doesn’t seem what you want.

    So you can overload based on the constness of the source object. But you cannot, as you want to now, overload simply based on the constness of the destination object.

    This all reduces down to thinking about the difference between an object and the data the object may inderectly refer to, which in practice presents itself as the difference between a constant pointer and a pointer to constant data.

    What I’m trying to achieve here is to prevent the user from changing
    the color directly

    Then there’s absolutely no need for any overloading. Just return the const version. The user cannot change the returned color directly, he can only copy it and change the copy.

    cw.set(red);    //nope, return color is constant
    Color col = cw; //copy returned color into col
    col.set(green); //fine, since we're not working on the returned color, but on col
    
    const Color c_col = cw; //use this if you don't want c_col to be modified, 
                            //doesn't have anything to do with returned color, though
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have defined the following two functions test <- function(t) { return( (0.5*eta^2/theta)*(1-exp(-2*theta*t)) )
The following two functions behave differently when given an empty string: guardMatch l@(x:xs) |
Consider following two examples: Example 1: <xs:import namespace=http://example.com/ns schemaLocation=test.xsd/> Example2: <sample:Data Test=true xsi:schemaLocation=http://example.com/test.xsd> How
I have the following two files: file1.c int main(){ foo(); return 0; } file2.c
Following two cases seem to work: public class A { private class B {
I have the following two observables System.Net.WebRequest req = System.Net.HttpWebRequest.Create(http://test.com/data.xml); req.Method = HEAD; var
Consider the following two lines of code if (test ! = null) and if
The following two queries return the same (expected) result when I query my database:
I have the following two classes : class DbQuery: def __init__(self,query): self.query = query
Consider the following two trivial models: class Iq def score #Some Irrelevant Code end

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.