Thread: why does this have private member access?

  1. #1
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787

    why does this have private member access?

    I was wondering why, when you overload an operator, does it have private member access? I know it does, but why? for example:

    Code:
    Class Date
    ...
    public:
         void operator=(Date&);
         ...
    private:
         int day,month,year;
    ...
    
    void Date::operator=(Date& x)
    {
         day=x.day;
         month=x.month;
         year=x.year;
    }
    Why can that be done? does it have to do with *this?
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  2. #2
    Compulsive Liar Robc's Avatar
    Join Date
    Jul 2004
    Posts
    149
    Your overloaded operator is a member function. Member functions have access to the private members of a class, it doesn't necessarily have to be the same object as *this. So Date::operator= can see the private members of its Date argument.

  3. #3
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    It is correct (as Robc was saying). The code written there is equivalent to:
    Code:
         this->day=x.day;
         this->month=x.month;
         this->year=x.year;
    Lack of source object implies that the source object is the "this" pointer. The class can access it's own internal data, regardless of what object of the class it is dealing with.

  4. #4
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Compile it.

  5. #5
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    Quote Originally Posted by Zach L.
    It is correct (as Robc was saying). The code written there is equivalent to:
    Code:
         this->day=x.day;
         this->month=x.month;
         this->year=x.year;
    I do agree that this is equivalent, but I think that this
    day=x.day and this->day=x.day is not the same regarding effeciency. Code day=x.day is more effecient because that doesn't imply a function call beacuse of the operator ->.
    Am I right?

  6. #6
    Compulsive Liar Robc's Avatar
    Join Date
    Jul 2004
    Posts
    149
    >I think that this day=x.day and this->day=x.day is not the same regarding effeciency.
    I'm not a compiler writer, but I would wager than the two result in equivalent machine code. The former is just a syntactic convenience for us lowly programmers.

    >how is it possibe to compile?
    A simple statement from the standard about private members:
    A member of a class can be

    -- private; that is, its name can be used only by members and friends of the class in which it is declared.
    Note how it says "class" and not "object". This means that member functions which declare objects of the same type as *this are able to access the private members of those objects. Funky, huh?

  7. #7
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    >how is it possibe to compile?

    It is possible because whan you call any function memeber of a class this pointer is also sent like argument. Every acces to a memer variable is possible through this pointer.

    I'm not sure if it that would be the same machine code, I think this->... would be something like function call.
    Anyway I'd like to someone makes this clear

  8. #8
    Compulsive Liar Robc's Avatar
    Join Date
    Jul 2004
    Posts
    149
    >I think this->... would be something like function call.
    Dereferencing a pointer isn't like a function call. And because this is done implicitly anyway, the two methods are probably identical in performance. I don't really have the inclination to go rooting through the standard to verify this, so I'll just say that even if they aren't identical, it really doesn't matter because the difference would be negligable.

    On one of my compilers (MVC++ .NET) the code:
    Code:
    class C {
    public:
      C();
    private:
      int i;
    };
    
    C::C()
    {
      i = 10;
      this->i = 10;
    }
    
    int main()
    {
      C c;
    }
    results in the following relevant piece of assembly code using the /Fa switch:
    Code:
    ; Line 10
    	ldarg.0				; _this$
    	ldc.i4.s	10		; i32 0xa
    	stind.i4	
    ; Line 11
    	ldarg.0				; _this$
    	ldc.i4.s	10		; i32 0xa
    	stind.i4

  9. #9
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    idk... I just though that it should't be able to access x.day because it's a member of a different object, but after reading the definition Robc gave, It makes perfect sense...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  10. #10
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    If you think about it a bit, it is perfectly in keeping with the idea of data hiding. As a quite common example, say you have a class that needs a meaningful (and non-trivial) copy constructor and/or assignment operator. If the object has a lot of internal data, then copying it, without being able to access the internal data of members of the same class would mean exposing that internal data through public accessor functions.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 02-14-2008, 02:59 PM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. private data member with public set member function
    By Mario in forum C++ Programming
    Replies: 2
    Last Post: 05-28-2002, 10:53 AM
  4. class member access denied
    By chiqui in forum C++ Programming
    Replies: 2
    Last Post: 05-27-2002, 02:02 PM