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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:21:04+00:00 2026-06-14T03:21:04+00:00

I want to do method chaining on class Point below. #include <iostream> class Point

  • 0

I want to do method chaining on class Point below.

#include <iostream>

class Point {
  public:
    Point(int x, int y): _x(x), _y(y) {}

    Point& moveX(int x);
    Point& moveY(int y);
    Point& print() const;

  ...
};

...

Point& Point::print() const {
  std::cout << "(" << _x << "," << _y << ")" << std::endl;
  return *this;  // Compile fails
}

I think it makes sense to mark print() as const member function because it just print the internal members. However, I want to do method chaining among both non-const and const function like below.

int main() {
  Point p(1,1);
  p.moveX(10).print().moveY(11); // method chaining
}

So I have to return this as non-const but it fails the compilation because, in my understanding, the members are marked const including this in const member function.

Is there a way to do method chaining in this situation?

  • 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-14T03:21:06+00:00Added an answer on June 14, 2026 at 3:21 am

    The reason why this fails is that inside a const member function, this is really a const Point*, not a Point*. Thus you are trying to initialize a non-const reference from a const pointer. It’s not that the compiler isn’t believing you, you’re just asking for two incompatible things at one time.

    This is one of the very few valid uses of const_cast, in my opinion. Normally, using const_cast is almost always a sign of a design error, or worse a programming error.
    Here, the function is really const and should be const, but there is no reason why you shouldn’t be able to chain something non-const afterwards, so it’s arguably legitimate to do such a thing.

    Do note, however, although the function is strictly const (in respect to the object, not so much in its use of IO functions!), one thing you should consider is that in some (rare) cases, it may result in code that doesn’t do what you want. The compiler is allowed to cache the result of a const function and omit another call to the same const function (since you promised that it won’t change anything). Therefore, it is allowable to optimize some_point.Print().Print(); into some_point.Print(). This is probably not a problem for you (why would you want to print the same values twice), just something to be generally aware of.

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

Sidebar

Related Questions

I want a method like this: public async void DoSomething() { DoSomethingElse(); } But
Possible Duplicate: PHP method chaining? I want to use functions combined, like: select(SELECT *
I want a method within class User < ActiveRecord::Base def global_user_id User.find_by_username(Global_User).id end end
Sample code : public class CA { public CA(string s, List<int> numList) { //
I want to use method chaining in moo tools 1.2. My requirements are as
I want a method of the class: One ( AccessibleWithinSameNamespace ) to be accessible
I want call method path from class Path. I need set some number -
I have the following class and I want to implement chaining methods. I am
I want a method to return an XML Serialized Typed object. Is there an
I want a method that extracts the data from a JSON-object parsed before as

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.