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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:05:49+00:00 2026-05-26T16:05:49+00:00

According to Bjarne Stroustrup, references were introduced into C++ to support operator overloading: References

  • 0

According to Bjarne Stroustrup, references were introduced into C++ to support operator overloading:

References were introduced primarily to support operator overloading.

C passes every function argument by value, and where passing an object by value would be inefficient or inappropriate the user can pass a pointer. This strategy doesn’t work where operator overloading is used. In that case, notational convenience is essential because users cannot be expected to insert address-of operators if the objects are large. For example:

a = b - c;

is acceptable (that is, conventional) notation, but

a = &b - &c;

is not. Anyway, &b - &c already has a meaning in C, and I didn’t want to change that.

Having both pointers and references in the language is a constant source of confusion for C++ novices.

Couldn’t Bjarne have solved this problem by introducing a special language rule that allowed object arguments to decay into pointers if a user-defined operator function exists that takes such pointers?

The declaration and usage of subtraction would then have looked like:

Foo operator-(const Foo* x, const Foo* y);
a = b - c;

Was such a solution ever proposed/considered? Would there be any serious downsides to it?

Yes I know, references provide other advantages due to their restrictions, but that’s not the point.

Interestingly, the assignment operator of C with classes seems to have worked exactly like that:

Changing the meaning of assignment for objects of a class […] is done by declaring a class member function called operator=. For example:

class x {
public:
    int a;
    class y * p;
    void operator = (class x *);
};
  • 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-26T16:05:49+00:00Added an answer on May 26, 2026 at 4:05 pm

    I don’t see how this solves the problem: operator- called on pointers already has a meaning.

    You’d be defining an operator- for Foo* arguments, but that already exists.

    Then you’d need some contrived semantics that “when called explicitly with pointers, the pointer arithmetic operator is called. When called with object lvalues, they decay into pointers, and then the overloaded version is called”. Which, to be honest, seems far more contrived, and much less intuitive than just adding references.

    And then inside the operator-, I get the arguments as pointers, and then I’d better make sure to dereference those if I need to call another (or the same) operator from there. Otherwise I’d accidentally end up performing pointer arithmetics.

    You’re proposing an implicit conversion which has different semantics than doing the conversion yourself. That is:

    Foo foo;
    bar(foo); 
    

    performs an implicit conversion from Foo to Foo*, and then a function with the signature void bar(Foo*) is called.

    But this code would do something completely different:

    Foo foo;
    bar(&foo);
    

    that would also convert to a Foo*, and it would also call a function with the signature void bar(Foo*), but it would potentially be a different function. (In the case of operator-, for example, one would be the user-overloaded operator, and the other would be the standard pointer arithmetic one.

    And then consider template code. In general, implicit conversions make templates really painful because the type passed in might not be the type you want to operate on.

    Of course, there are more practical problems too:

    References enable optimizations that aren’t possible with pointers. (The compiler can assume they’re never null, and it may be better able to do aliasing analysis)

    Moreover, code would fail silently if no matching operator- can be found. Instead of telling me that, the compiler would implicitly start doing pointer arithmetics. Not a deal-breaker, but I really really prefer errors to be caught early where possible.

    And one of the goals with C++ was to make it generic: to avoid having “magic” types or functions. In real-world C++, operators are very much just functions with a different syntax. With this rule, they would have different semantics as well. (What if the operator is called with function syntax? operator+(a, b)?

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

Sidebar

Related Questions

According to Bjarne Stroustrup: if (and only if) you use an initialized member in
According to the feedparser documentation , I can turn an RSS feed into a
According to the MSDN article found at http://msdn.microsoft.com/en-us/library/wyk4d9cy.aspx the floating-point value .1 has no
According to Mark's awesome tutorial page, The map function applies a given function that
Bjarne wrote:- For a type T, T() is the notation for the default value
According to w3schools , the best HTML solution for embedding videos directly into a
according to the CUDA programming guide, the value returned by the texture fetch is
According to http://caniuse.com/script-defer , most browsers support the script tag's defer attribute. I would
according to msdn IStructuralEquatable Defines methods to support the comparison of objects for structural
According to this page on MSDN, the Key Value for Left Control is 162.

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.