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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:16:39+00:00 2026-06-14T11:16:39+00:00

In c++ I implemented an integer class and I overloaded operator ^ to be

  • 0

In c++ I implemented an integer class and I overloaded operator ^ to be the power function.

integer integer::operator^ (const integer& rhs){
       return integer(pow(this->.i, rhs.i));
}

This is working correctly for two operands.

integer i1, i2, i3 ;   
 i4 = i1 ^ i2 ^ i3;  

The value of i4 is wrong mathematically because associativity required right-to-left. How can I solve this problem? How do I change associativity?

I got reasonable answers and I learn:

-We can't change  associativity or priority of an operator.   
-Good is Not to overload operators to do something conceptually different to 
 the built-in versions  
-Even compiler can't support; it hard to implement!  
  • 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-14T11:16:43+00:00Added an answer on June 14, 2026 at 11:16 am

    You cannot change the associativity or priority of an operator in C++ by overloading it. These rules are hardwired into the language syntax.

    The C++ standard says (§13.5.6, emphasis mine):

    An operator function shall either be a non-static member function or be a non-member function and have
    at least one parameter whose type is a class, a reference to a class, an enumeration, or a reference to an
    enumeration. It is not possible to change the precedence, grouping, or number of operands of operators.
    The meaning of the operators =, (unary) &, and , (comma), predefined for each type, can be changed […]

    Not only is the ^ operator left-associative, but it also has a very low precedence. The correct precedence for a power operator should be higher than the multiplication (so priority 4 or better on this table), but it has priority 10–this means that even additions and subtractions are evaluated before it. 1 + 2 ^ 3 * 4 will be parsed as (1 + 2) ^ (3 * 4), while a mathematically correct power operator should parse as 1 + (2 ^ 3) * 4.

    If the associativity or priority of an operator could be modified, a huge, huge syntactical mess would ensue. My humble opinion is that you should not try to overload the ^ operator to use it as a power operator. I would rather make a power method on the class.

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

Sidebar

Related Questions

I've implemented a plotting class that is currently capable of handling integer values only.
class WrongOverloading{ void something(String [] a){ .. } Integer something(String... aaa){ return 1;} }
In a single-threaded application I use code like this: Interface function GetNextUID : integer;
@Entity public class TestClass implements Serializable{ private Integer id; private Set<String> mySet; @Id @GeneratedValue
I implemented a sparse matrix as List<Map<Integer,Double>> . To get all entries of row
I've have already implemented pagination using the following code: public Paginacao<Anuncio> consultarPaginado(int pagina, Integer
I've implemented this search algorithm for an ordered array of integers. It works fine
I want to implement greatest integer function. [The greatest integer function is a quite
Implemented a generic repository with several Methods. One of those is this: public IEnumerable<T>
I was working on some code recently and decided to work on my operator

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.