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

  • Home
  • SEARCH
  • 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 7087067
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:37:41+00:00 2026-05-28T07:37:41+00:00

I’m working on an small C++ example, to learn C++. Somebody wrote the main

  • 0

I’m working on an small C++ example, to learn C++. Somebody wrote the main function already which looks like this:

int main()
{
    map2d p1(1.,3.);       
    const map2d p2(0.,0.);

    p1.x();            // testing the access to member variable
    p2.x();            // testing the access to member variable

    p1.x() = 3.;       // changing the member variable

    return 0;
}

Okay. I created a class with the name map2d. Which works so far up to the point where the member variable has to be changed p1.x() = 3.;. My question is, how to do it? My class looks like this this:

class map2d
{
    private: 
        double xp, yp;
    public:
        map2d (double xnew, double ynew): xp(xnew), yp(ynew) {}

        double x() const { return xp; }   // here is my problem
};

I was thinking to return a reference with &, but this didn’t worked out. I used something something like:

double& x() const { return xp; }

Did I make something wrong? Do you have any idea how to do it?

  • 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-28T07:37:42+00:00Added an answer on May 28, 2026 at 7:37 am

    You can’t return a modifiable l-value reference to a non-mutable class member variable from a method that is labeled as being const. By labeling a method as const, you are telling the compiler that it doesn’t change any data members that aren’t labeled as mutable, nor does it call any class methods that can change the state of non-mutable class members.

    You basically need two overloaded versions of your accessor function:

    double x() const { return xp; }  //constant version
    double& x() { return xp; }       //non-constant version
    

    Which version of the overloaded function will be chosen depends on the context of the call to the class method. This is based on whether the class instance for which the method is being called from is a constant or mutable class reference. For instance:

    void func_a(const map2d& map)
    {
        double x = map.x();  //calls const version
        /* map.x() = 5;  <== calls const version but you'll get a compiler error
                             for returning a non-l-value on left-hand-side of 
                             assignment operator */
    }
    
    void func_b(map2d& map)
    {
        map.x() = 5;        //calls non-const version
        double x = map.x()  //still calls non-const version
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
I am trying to understand how to use SyndicationItem to display feed which is
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

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.