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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:22:04+00:00 2026-06-17T12:22:04+00:00

Short version of question I seek advice as to whether to use ./*this versus

  • 0

Short version of question

I seek advice as to whether to use ./*this versus ->/this,
i.e. C++ (*this).chained().methods() versus this->chained()->methods().

By the way, at the moment most of the pages I have seen recommend
[[C++ (*this).chained().methods()]].

I was just wondering, because you can’t do

My_Class object.chained().methods();

(By the way, I have not tested the examples in this first section. I provide tested examples in the second section.)

You must do

My_Class object;
object.chained().methods();

which is an annoying extra line

Or you can do

 My_Class object = My_Class().object.chained().methods();

which requires a value copy – not acceptable if the constructor has side effects, like registering the object instance – like so many Knobs libraries do

Or you can do

 My_Class* object_ptr = *(new My_Class).object.chained().methods();

which works, but requires that annoying *(ptr)

Or you can do

 My_Class* object_ptr = (new My_Class)->object.chained()->methods();

which is a teensy bit better.

I suppose you can do

My_Class& object_ref(My_Class().chained().methods());

and I am not sure what I think about that.

By the way, I do NOT need debugging help here.
I code stuff like this up all the time
I provide the examples only for clarity.

I am seeking style advice, because there are several ways to code it,
and I have used different libraries that do it in opposite ways.

And mixing them is ugly:

  My_Object_with_Setters* object_ptr2 = &((new My_Object_with_Setters)->set_R1(1).set_P1(2)->set_R1(3))

 My_Object().method_returning_ptr()->method_returning_ref();

Maybe it’s not that bad…. but it sure can be confusing.

When I run into code that uses two different libraries using mixed .chained()->methods()
I sometimes wish for the ability to have postfix address-of and dereference operators

My_Object* mptr = My_Object() .method_returning_ptr() -> method_returning_ref ->&

More complete Examples

Setter Functions

I most often use this idiom with setter functions

class My_Object_with_Setters {
public:
  static int count;
  int value;
public:
  My_Object_with_Setters() {
    ++count;
    value = 0;
  }
public:
  std::ostream& print_to_stream(std::ostream& ostr) const {
    ostr << "(" << this->count << "," << this->value << ")";
    return ostr;
  }
  friend std::ostream&
  operator<< (
    std::ostream& ostr,
    const My_Object_with_Setters& obj ) {
    return obj.print_to_stream(ostr);
  }

public:
  My_Object_with_Setters& set_R1(int val) {
    this->value = val;
    std::cout << "set_R1: " << *this << "\n";
    return *this;
  }
  My_Object_with_Setters& set_R2(int val) {
    this->value = val;
    std::cout << "set_R2: " << *this << "\n";
    return *this;
  }
public:
  My_Object_with_Setters* set_P1(int val) {
    this->value = val;
    std::cout << "set_P1: " << *this << "\n";
    return this;
  }
  My_Object_with_Setters* set_P2(int val) {
    this->value = val;
    std::cout << "set_P2: " << *this << "\n";
    return this;
  }
public:
  My_Object_with_Setters set_V1(int val) {
    this->value = val;
    std::cout << "set_V1: " << *this << "\n";
    My_Object_with_Setters retval;
    retval = *this;     // kluge to force new object
    return retval;
  }
  My_Object_with_Setters set_V2(int val) {
    this->value = val;
    std::cout << "set_V2: " << *this << "\n";
    My_Object_with_Setters retval;
    retval = *this;     // kluge to force new object
    return retval;
  }
};

int My_Object_with_Setters::count = 0;  // clas static, distinguishes instances

void test_My_Object_with_Setters()
{
  std::cout << "cascading ref, ref, copy, copy, ref, ref\n";
  My_Object_with_Setters object;
  object.set_R1(1).set_R2(2).set_V1(11).set_V2(12).set_R1(101).set_R2(102);

  std::cout << "cascading ptr, ptr, ptr, ptr\n";
  My_Object_with_Setters* object_ptr = (new My_Object_with_Setters)->set_P1(1)->set_P2(2)->set_P1(11)->set_P2(12);

  std::cout << "cascading &address-of, ptr, ptr\n";
  (&object)->set_P1(1)->set_P2(2);

  std::cout << "cascading new ptr ref ptr ref\n";
  My_Object_with_Setters* object_ptr2 = &(*(new My_Object_with_Setters)->set_R1(1).set_P1(2)).set_R1(3);

}

Test output:

cascading ref, ref, copy, copy, ref, ref
set_R1: (1,1)
set_R2: (1,2)
set_V1: (1,11)
set_V2: (2,12)
set_R1: (3,101)
set_R2: (3,102)
cascading ptr, ptr, ptr, ptr
set_P1: (4,1)
set_P2: (4,2)
set_P1: (4,11)
set_P2: (4,12)
cascading &address-of, ptr, ptr
set_P1: (4,1)
set_P2: (4,2)
cascading new ptr ref ptr ref
set_R1: (5,1)
set_P1: (5,2)
set_R1: (5,3)

Generic Example

class My_Object {
public:
  static int count;
public:
  My_Object() {
    ++count;
  }
public:
  My_Object& method1_returning_ref_to_current_object() {
    std::cout << count << ": method1_returning_ref_to_current_object\n";
    return *this;
  }
  My_Object& method2_returning_ref_to_current_object() {
    std::cout << count << ": method2_returning_ref_to_current_object\n";
    return *this;
  }
public:
  My_Object* method1_returning_ptr_to_current_object() {
    std::cout << count << ": method1_returning_ptr_to_current_object\n";
    return this;
  }
  My_Object* method2_returning_ptr_to_current_object() {
    std::cout << count << ": method2_returning_ptr_to_current_object\n";
    return this;
  }
public:
  My_Object method1_returning_value_copy_of_current_object() {
    std::cout << count << ": method1_returning_value_copy_of_current_object\n";
    My_Object retval;
    return retval;
  }
  My_Object method2_returning_value_copy_of_current_object() {
    std::cout << count << ": method2_returning_value_copy_of_current_object\n";
    My_Object retval;
    return *this;
  }
};

int My_Object::count = 0;   // clas static, distinguishes instances

void test_My_Object()
{
  std::cout << "cascading ref, ref, copy, copy, ref, ref\n";
  My_Object object;
  object
   .method1_returning_ref_to_current_object()
   .method2_returning_ref_to_current_object()
   .method1_returning_value_copy_of_current_object()
   .method2_returning_value_copy_of_current_object()
   .method1_returning_ref_to_current_object()
   .method2_returning_ref_to_current_object()
   ;

  std::cout << "cascading ptr, ptr, ptr, ptr\n";
  My_Object* object_ptr = new My_Object;
  object_ptr
   ->method1_returning_ptr_to_current_object()
   ->method2_returning_ptr_to_current_object()
   ->method1_returning_ptr_to_current_object()
   ->method2_returning_ptr_to_current_object()
   ;

  std::cout << "cascading &address-of, ptr, ptr\n";
  (&object)
   ->method1_returning_ptr_to_current_object()
   ->method2_returning_ptr_to_current_object()
   ;

  std::cout << "cascading new ptr ref ptr ref\n";
  My_Object* object_ptr2
   = (&(*(new My_Object)
    ->method1_returning_ptr_to_current_object())
    .method2_returning_ref_to_current_object())
   ;

}

Test output

cascading ref, ref, copy, copy, ref, ref
1: method1_returning_ref_to_current_object
1: method2_returning_ref_to_current_object
1: method1_returning_value_copy_of_current_object
2: method2_returning_value_copy_of_current_object
3: method1_returning_ref_to_current_object
3: method2_returning_ref_to_current_object
cascading ptr, ptr, ptr, ptr
4: method1_returning_ptr_to_current_object
4: method2_returning_ptr_to_current_object
4: method1_returning_ptr_to_current_object
4: method2_returning_ptr_to_current_object
cascading &address-of, ptr, ptr
4: method1_returning_ptr_to_current_object
4: method2_returning_ptr_to_current_object
cascading new ptr ref ptr ref
5: method1_returning_ptr_to_current_object
5: method2_returning_ref_to_current_object

By the way, I do NOT need debugging help here. I provide the examples only for clarity.

I am seeking style advice.

  • 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-17T12:22:05+00:00Added an answer on June 17, 2026 at 12:22 pm

    Everyone has their own style; as you say, it only really gets annoying when you start mixing them.

    Personally, I only return a pointer from a function if it might be 0; this is never 0, so I would always return *this (i.e. a reference) and thus chain with ..

    For what it’s worth, I also try really hard to make the default constructor cheap, partly because there are so many cases in which it turns out to be convenient to first default construct and then assign.

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

Sidebar

Related Questions

The short version of the question - why can't I do this? I'm restricted
This is basically a rails 3 version of this question . Short of parsing
The short version of this question: Does a pointer to the first data member
To short version of this question is that I want to accomplish something along
Super-short version: I solved this problem when I was nearly finished writing the question.
Question in short: How tot do this in MVC.NET? Question in long version: Im
The short version of this question is: How can I take data that only
Inspired by this question . Short version: Why can't the compiler figure out the
Short version question : Is there navigator.mozIsLocallyAvailable equivalent function that works on all browsers,
SHORT VERSION OF QUESTION: So basically my question is: How can I set the

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.