quick question concerning "this"

This is a discussion on quick question concerning "this" within the C++ Programming forums, part of the General Programming Boards category; Hi, further in my quest to make the transtition from java to c++. Code anybody tell me the equivalent c++ ...

  1. #1
    HIM
    HIM is offline
    Registered User
    Join Date
    Aug 2006
    Posts
    13

    quick question concerning "this"

    Hi, further in my quest to make the transtition from java to c++. Code anybody tell me the equivalent c++ syntax to assign a method parameter to a class variable... ie, in java you would use

    Code:
    private int myInt;
    
    public Class myClass(int myInt)
    {
          this.myInt = myInt;
    }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    In C++, this is a pointer, so you can do this->myInt = myInt. I'd prefer to give the variables different names, though.

  3. #3
    HIM
    HIM is offline
    Registered User
    Join Date
    Aug 2006
    Posts
    13
    Thanks for the quick reply.

    When you say you'd prefer to give them different names, is that just personal preference, or is there justification for doing this? Just so I know which would be the more professional form.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    It just reduces the potential for confusion and errors for me. Others feel that using this-> helps make things clearer and reduces potential for errors. I've seen both techniques used, but in my experience it has been harder to follow through on actually using this everywhere than it is to give the parameters different names.

    Another option that many people follow, including myself, is to prefix or suffix member variables so that they stand out from local variables and parameters. Many people use "m_" in front of member variables, and others use a suffix of "_", so either m_myInt or myInt_. If you stick to such a convention that might be the best solution.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Just so I know which would be the more professional form.
    It's better to use distinct names. First, all of that "this->" crap is often pointless noise that distracts a reader from the meat of the code. Second, when two variables of the same name are visible within the same scope, confusion reigns. Whether you can disambiguate with something like "this->" or not is irrelevant.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Very quick math question
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 10-26-2005, 11:05 PM
  2. very quick question.
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 07-24-2002, 03:48 AM
  3. quick question
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-22-2002, 04:44 AM
  4. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 05:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21