Thread: This pointers

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    23

    Unhappy This pointers

    Hello,

    Being fairly new to C++ I am having a little trouble with the idea of a this pointer, can anyone enlighten me?

    If you pass a dereferenced this pointer into a copy constructor what exactly is happening there?

    //I have this:

    Virtual Mammal* Clone() { return new Cat(*this);}

    //Is the this pointer pointing to the beginning of the array which holds the private data of the original object and some magic then goes on which involves copying values over into the new Cat?

    A general explanation would be really helpful.

    Thanks

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    the this pointer points to whichever object you called the virtual function through.
    All classes that do not specify a copy constructor will be given one automatically. This is a straight bitwise copy of values. This is sufficient normally unless you have pointer or reference members in which case you will have to write a copy constructor to do the job properly otherwise you will end up with 2 objects pointing to the same piece of dynamic memory.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    194

    Re: This pointers

    Originally posted by da_fall_guy
    Hello,

    Being fairly new to C++ I am having a little trouble with the idea of a this pointer, can anyone enlighten me?

    If you pass a dereferenced this pointer into a copy constructor what exactly is happening there?

    //I have this:

    Virtual Mammal* Clone() { return new Cat(*this);}

    //Is the this pointer pointing to the beginning of the array which holds the private data of the original object and some magic then goes on which involves copying values over into the new Cat?

    A general explanation would be really helpful.

    Thanks
    That line is creating a new Cat, and it is being passed the value that this points to. this points to the data for what ever object, that you called Clone from. That data gets passed to the copy constructor of the Cat class.
    Mammal * Cloned = MyCat.Clone();
    Cloned whould now be equal to MyCat

    If you have a class T, then the this pointer points to a class T.
    you can acces vars or functions with
    varname->membername
    or
    (*varname).membername

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    23
    Thanks to both of you.

    The bitwise copy, is that just of the object's member variables or is it also a copy of methods and whatnot?

  5. #5
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    if you think that when you have 100 objects of a class you also have 100 sets of member functions then you are very wrong. There will be 1 set of member functions. These are accessible by all objects of the class. An object is only data that has member functions associated with it.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  2. function pointers
    By benhaldor in forum C Programming
    Replies: 4
    Last Post: 08-19-2007, 10:56 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM