Click here to Skip to main content
16,008,469 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe:arrange pic1 into pic2 Pin
Stephen Hewitt6-Jul-08 15:21
Stephen Hewitt6-Jul-08 15:21 
GeneralRe: best arrange tiny pic in big pic Pin
Stephen Hewitt6-Jul-08 16:37
Stephen Hewitt6-Jul-08 16:37 
QuestionBasic operator overloading Pin
bkelly136-Jul-08 8:21
bkelly136-Jul-08 8:21 
AnswerRe: Basic operator overloading Pin
Saurabh.Garg6-Jul-08 8:36
Saurabh.Garg6-Jul-08 8:36 
GeneralRe: Basic operator overloading Pin
bkelly136-Jul-08 12:43
bkelly136-Jul-08 12:43 
GeneralRe: Basic operator overloading Pin
Bram van Kampen6-Jul-08 13:11
Bram van Kampen6-Jul-08 13:11 
GeneralRe: Basic operator overloading Pin
bkelly136-Jul-08 16:01
bkelly136-Jul-08 16:01 
GeneralRe: Basic operator overloading Pin
Saurabh.Garg6-Jul-08 17:51
Saurabh.Garg6-Jul-08 17:51 
I think the biggest thing you are missing is that member functions of a class have full access to all member variables. It doesnt matter which object called the function, all objects of that class can access private members in a member function of same class. Lets take an example

class Foo
{
private:
    
    int x;

public:

    Foo(int _x)
    {
        x = _x;
    }
    
    inline int AddX(const Foo& B)
    {
        return x + B.x;
    }
};


Now you might think that it is a bad design to make private variable accessible - A can access B' x and vice-versa. But that is not the case. Why is it so? Because access modifies are for the classes which use this class. So for example if you want to hide member variables from other classes you will use private. These member variable will still be visible within the class definition.

Now lets see if this was not the case. Then you must have a public function to set and get value for every private variable. Well in this case the private variables are as good as public variables. Does this make sense?

Okay this is c++ side of the explanation. Lets work on intuition of how a = b + c should work. In the class definition I gave above I can do the following.

Foo f1(10);
Foo f2(2);
int y = f1.AddX(f2);


Look at it carefully it has, mathematically, same structure as a = b + c. But will you in this case say that since y is the return value the function should be called in perspective of y? And AddX should have two arguments instead of one? I hope answer to these questions in no. Now lets see how to read a = b + c in terms of c++ -> call b's member function operator+ with a single argument c and return a value a. In simpler words a = b.operator+(c). Now since a, b, and c are objects of same class the function operator+ (which is member of same class) have full access to class data. If we add operator+ to Foo it will look like.

class Foo
{
private:
    
    int x;

public:

    Foo(int _x)
    {
        x = _x;
    }
    
    inline int AddX(const Foo& B)
    {
        return x + B.x;
    }
    
    Foo& operator+(const Foo& B)
    {
        Foo a;
        a.x = x + B.x // Or a.x = this.x + B.x
    }
};


Only differece unary operator+ has is that it operates on same object which calls the function instead of creating a new one.

Foo& operator+(const Foo& B)
{
    x = x + B.x;
    return *this;
}


I hope this makes things a bit clearer now.

-Saurabh
GeneralRe: Basic operator overloading Pin
Saurabh.Garg6-Jul-08 18:05
Saurabh.Garg6-Jul-08 18:05 
AnswerRe: Basic operator overloading [modified] Pin
Luc Pattyn6-Jul-08 10:03
sitebuilderLuc Pattyn6-Jul-08 10:03 
GeneralRe: Basic operator overloading Pin
Graham Shanks6-Jul-08 10:37
Graham Shanks6-Jul-08 10:37 
GeneralRe: Basic operator overloading Pin
Luc Pattyn6-Jul-08 11:15
sitebuilderLuc Pattyn6-Jul-08 11:15 
AnswerRe: Basic operator overloading Pin
Dan6-Jul-08 12:54
Dan6-Jul-08 12:54 
GeneralRe: Basic operator overloading Pin
bkelly136-Jul-08 16:03
bkelly136-Jul-08 16:03 
QuestionHow to perform structured exception handling in Windows Vista Pin
V K 26-Jul-08 5:09
V K 26-Jul-08 5:09 
AnswerRe: How to perform structured exception handling in Windows Vista Pin
Stephen Hewitt6-Jul-08 15:37
Stephen Hewitt6-Jul-08 15:37 
GeneralRe: How to perform structured exception handling in Windows Vista Pin
V K 26-Jul-08 17:49
V K 26-Jul-08 17:49 
GeneralRe: How to perform structured exception handling in Windows Vista Pin
Saurabh.Garg6-Jul-08 18:08
Saurabh.Garg6-Jul-08 18:08 
GeneralRe: How to perform structured exception handling in Windows Vista Pin
Stephen Hewitt6-Jul-08 18:28
Stephen Hewitt6-Jul-08 18:28 
QuestionReplacement for IsBadReadPtr in Windows Vista Pin
V K 26-Jul-08 4:46
V K 26-Jul-08 4:46 
AnswerRe: Replacement for IsBadReadPtr in Windows Vista Pin
CPallini6-Jul-08 5:02
mveCPallini6-Jul-08 5:02 
GeneralRe: Replacement for IsBadReadPtr in Windows Vista Pin
V K 26-Jul-08 5:10
V K 26-Jul-08 5:10 
GeneralRe: Replacement for IsBadReadPtr in Windows Vista Pin
CPallini6-Jul-08 5:22
mveCPallini6-Jul-08 5:22 
GeneralRe: Replacement for IsBadReadPtr in Windows Vista Pin
Luc Pattyn6-Jul-08 11:35
sitebuilderLuc Pattyn6-Jul-08 11:35 
GeneralRe: Replacement for IsBadReadPtr in Windows Vista Pin
CPallini6-Jul-08 21:14
mveCPallini6-Jul-08 21:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.