The type of *this

This is a discussion on The type of *this within the C++ Programming forums, part of the General Programming Boards category; Hello everyone, I am confused what is the type of *this? Type of reference to instance or type of instance? ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    The type of *this

    Hello everyone,


    I am confused what is the type of *this? Type of reference to instance or type of instance?

    Looks like both code can compile, no warning messages. Any ideas?

    Code:
    class A{
    
    	A& foo1()
    	{
    		return *this;
    	}
    
    	A foo2()
    	{
    		return *this;
    	}
    };

    thanks in advance,
    George

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,031
    this is a pointer to A*. In other words, it's a pointer to the current instance of the class to which the functions belong (in this example, that's A, so A*).
    Last edited by Elysia; 12-16-2007 at 06:22 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    this is A*
    *this is A&

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Would you be puzzled by the following code too?

    Code:
    void foo(int n) {}
    void bar(int& n) {}
    
    int main()
    {
        int a = 42;
        int* x = &a;
        
        //calls using int a
        foo(a);
        bar(a);
    
        //calls using dereferenced pointer x
        foo(*x);
        bar(*x);
    }
    Last edited by anon; 12-16-2007 at 06:22 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Registered User t3chn0n3rd's Avatar
    Join Date
    Dec 2007
    Location
    kansas city
    Posts
    25

    object oriented programming

    this was a great example of object oriented programming

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by t3chn0n3rd View Post
    this was a great example of object oriented programming
    Huh? It's not object orient as such - it's about some things (references) that appear in C++, rather than C - but it's actually little to do with object orientation.

    Note that in the original code, foo1 and foo2 have similar results - the main difference is that foo1 returns an alias of the original instance of A (so you now likely have TWO references the same object) whilst foo2 creates a new copy of the type A with the same content as *this, but it is NOT the same object.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 03-20-2008, 08:59 AM
  2. Replies: 28
    Last Post: 07-17-2006, 12:35 AM
  3. Compiler "Warnings"
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 04-24-2005, 02:09 PM
  4. Errors
    By Rhidian in forum C Programming
    Replies: 10
    Last Post: 04-04-2005, 01:22 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 07:51 PM

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