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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:25:56+00:00 2026-05-25T19:25:56+00:00

Suppose I have a c++ class with a private variable, x. For it’s setter,

  • 0

Suppose I have a c++ class with a private variable, x. For it’s setter, is there any difference using this? Is there the potential for unwanted / unexpected behavior is I don’t use this?

Setter:

void setX(double input)
{
   x = input;
}

Setter using this:

void setX(double x)
{
   this->x = 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-25T19:25:56+00:00Added an answer on May 25, 2026 at 7:25 pm

    Because of two-phase lookup for class templates, this might be required to

    1. explicitly state you mean a member
    2. because of lookup rules, the compiler might assume another entity that is visible there is meant

    (see the first example) Both are equivalent.

    In a non-template situation, you usually avoid using this; the generated code will be the same, thus there won’t be any difference (see the second example).
    The reason for this is that the compiler will try to lookup every symbol it sees. The first place to lookup is in class-scope (except for block and function scope). If it finds a symbol of that name there, it will emit this implicitly. In reality, member functions are just ordinary functions with an invisible parameter.

    class Foo {
        void foo () { x = 0; }
        void bar () const { std::cout << x; }
        void frob();
        int x;
    };
    void Foo::frob() {}
    

    Is actually transformed into

    class Foo {        
        int x;
    };
    inline void foo (Foo *const this) { this->x = 0; }
    inline void bar (Foo const * const this) { std::cout << this->x; }
    void frob (Foo * const this) {}
    

    by the compiler.


    Behavioral example for this in templates:

    #include <iostream>
    
    void foo() {
        std::cout << "::foo()\n";
    }
    
    template <typename>
    struct Base {
        void foo() const { std::cout << "Base<T>::foo()\n"; }
    };
    
    template <typename T>
    struct Derived_using_this : Base<Derived_using_this<T> > {
        void bar() const { this->foo(); }
    };
    
    template <typename T>
    struct Derived_not_using_this : Base<Derived_not_using_this<T> > {
        void bar() const { foo(); }
    };
    
    
    int main () {
        Derived_not_using_this<void>().bar();
        Derived_using_this<void>().bar();
    }
    

    Output:

    ::foo()
    Base<T>::foo()
    

    Example assembly for non-template:

    With this:

    pushq   %rbp
    movq    %rsp, %rbp
    subq    $16, %rsp
    movq    %rdi, -8(%rbp)
    movq    -8(%rbp), %rax
    movl    (%rax), %eax
    movl    %eax, %esi
    movl    $.LC0, %edi
    movl    $0, %eax
    call    printf
    leave
    ret
    

    Without this:

    pushq   %rbp
    movq    %rsp, %rbp
    subq    $16, %rsp
    movq    %rdi, -8(%rbp)
    movq    -8(%rbp), %rax
    movl    (%rax), %eax
    movl    %eax, %esi
    movl    $.LC0, %edi
    movl    $0, %eax
    call    printf
    leave
    ret
    

    Verify yourself:

    test.cc:

    #include <stdio.h> // Don't use this. I just did so for nicer assembly.
    
    class Foo {
    public:
        Foo () : i(0) {}
    
        void with_this() const { printf ("%d", this->i); }
        void without_this() const { printf ("%d", i); }
    private:
        int i;
    };
    
    
    int main () {
        Foo f;
        f.with_this();
        f.without_this();
    }
    

    Run g++ -S test.cc. You’ll see a file named test.s, there you can search for the function names.

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

Sidebar

Related Questions

Suppose I have a class Foo , with a private variable bar_ containing some
Suppose I have this: class Validator { private $db; public checkIfUsernameAlreadyExists($username) { if (!$this->db)
Suppose I have this class: public class DispatcherService<T> { private static Action<T> Dispatcher; public
Suppose I have a class public class MyClass { private Set<String> set = new
Suppose I have a class module clsMyClass with an object as a member variable.
I suppose that this is an interesting code example. We have a class --
Suppose I have a class: class test { public: void print(); private: int x;
suppose I have the following class: class MyInteger { private: int n_; public: MyInteger(int
Suppose, I have a junit test class: class MyComponentTest { private void test(File file)
Suppose I have a class with some attributes. How is it best (in 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.