Thread: question

  1. #1
    quiteblack
    Guest

    question

    I found something uncommon(at least puzzled me) when playing with operator overloading, please check the code below to see what happened(if any spelling mistake just because I'm absent minded, please forgive it.):

    Code:
    #include <iostream>
    
    class myClass 
    {
    		int value;
    	public:
    		myClass(){};
    		myClass(int);
    		~myClass(){};
    		myClass operator + (myClass);
    		void check();
    };
    
    myClass :: myClass(int para)
    {
    	value=para;
    }
    
    myClass myClass :: operator + (myClass newClass)
    {
    	myClass tempClass;
    	tempClass.value=value+newClass.value;
    	return tempClass;
    }
    
    void myClass :: check()
    {
    	cout << "private value: " << value << endl;
    }
    
    void main()
    {
    	myClass a(1); 
    	myClass b(2); 
    	myClass c;
    	c.check();
    	c=a+b;
    	c.check();
    }
    the point is the block of operator definition(other parts are not so serious), as we could see instance c got the value of 3 finally as expected but did you notice something inside with operator +'s definition ? yes we could play with private members inside public function but how could we get private ones belong to other objects ? so strange here we still could see tempClass.value and newClss.value could work fine, any ideas ?

    thanx in adv~

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793

    Re: question

    Originally posted by quiteblack
    yes we could play with private members inside public function but how could we get private ones belong to other objects ? so strange here we still could see tempClass.value and newClss.value could work fine, any ideas ?

    thanx in adv~
    Objects of the same class have access to each other's private variables...

  3. #3
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563

    Re: Re: question

    Originally posted by Fordy
    Objects of the same class have access to each other's private variables...
    did you mean all the private members are visible among those instances which are derived from the same class ?

  4. #4
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    Objects of the same class have access to each other's private variables...
    Can you explner a little bit more please?

  5. #5
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793

    Re: Re: Re: question

    Originally posted by black
    did you mean all the private members are visible among those instances which are derived from the same class ?
    No....for that you would need to make the data in the base class protected.

    But 2 objects of the same class can access each other's private data. This is needed for situation like in the OP's original example (operator+()).....it's not a weakness as you might at this moment be thinking because you still cant access these variables from "the outside"

    Code:
    #include <iostream>
    #include <string>
    
    class foobar{
    private:
    	std::string m_str;//private!
    public:
    	foobar(const std::string& str):m_str(str){}
    	//constructor sets string
    	
    	void PrintMyString(){std::cout << m_str;}
    	
    	void PrintOtherString(const foobar& foo){
    		std::cout << foo.m_str;
    	}//has access to other object's private members
    };
    	
    int main(void){
    
    	foobar f("Hello "),g("World");
    	
    	f.PrintMyString();//Access to its own private data
    	
    	f.PrintOtherString(g);//Access to other foobar's private data
    	
    	//std::cout << f.m_str;
    	//Still cant access private members from outside 
    
    	
    }

  6. #6
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    Now I get it!
    Thanks alot!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM