0
#include<iostream>
using namespace std;

class Complex {
private:
    int real, imag;
public:
    Complex(int r = 0, int i = 0) {real = r; imag = i;}
    
    // This is automatically called when '+' is used with
    // between two Complex objects
    Complex operator + (Complex const &obj) {
        Complex res;
        res.real = real + obj.real;
        res.imag = imag + obj.imag;
        return res;
    }
    void print() { cout << real << " + i" << imag << '\n'; }
};

int main()
{
    Complex c1(10, 5), c2(2, 4);
    Complex c3 = c1 + c2;
    c3.print();
}

Here operator + is overloaded and it is accesing the private member of res class

Some more examples are ex1 -

struct Edge {
    int a,b,w;
};
bool operator<(const Edge& x, const Edge& y) { return x.w < y.w; }

ex2-

#include <bits/stdc++.h>
using namespace std;

struct Edge {
    int a,b,w;
    bool operator<(const Edge& y) { return w < y.w; }
};

int main() {
    int M = 4;
    vector<Edge> v;
    for (int i = 0; i < M; ++i) {
        int a,b,w; cin >> a >> b >> w;
        v.push_back({a,b,w});
    }
    sort(begin(v),end(v));
    for (Edge e: v) cout << e.a << " " << e.b << " " << e.w << "\n";
}

ex1 and ex2 are from the usaco.guide and the first example was from the geeks for geeks

Can anyone explain how it works ?

  • 1
    What exactly is your question? How `operator+` can access private members? It is a member function, it has access to it. – mch Jun 08 '22 at 11:12
  • If a class couldn't access **its own** private members, then such private members wouldn't be much use, would they? – Adrian Mole Jun 08 '22 at 11:15
  • And, in your Ex1 & Ex2 cases, the `Edge` `struct` doesn't have any private members. That's one of the very few differences between `struct` and `class` - default access attributes. – Adrian Mole Jun 08 '22 at 11:18
  • The `operator+()` you have defined in your first example is a member function of the class so has access to all (`private`, `protected`, and `public`) members of that class. In "ex1", `Edge` is a `struct` type, so its members are `public` by default which means they can be accessed by any code (including the `operator<()` which is not a member of that class). In "ex2", `Edge` is a `struct` type (so its members are accessible by anything) AND the `operator<()` is a member function of `Edge`. – Peter Jun 08 '22 at 11:20
  • Are you not equally surprised that it also accesses the private members of the parameter `obj`? – molbdnilo Jun 08 '22 at 11:40
  • Typically for geeksforgeeks, it's not a very good example - a less inexperienced C++ programmer than the author would write `return Complex(real + obj.real, imag + obj.imag);`, and use an initializer list for the constructor. That site has a pretty bad reputation and it's barely (if at all) worth what you're paying for it. Get a [good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) instead. – molbdnilo Jun 08 '22 at 11:43

1 Answers1

0

How here private data members are accessed from the class instance. Can anyone explain how it works ?

First things first, in ex1 and ex2 you're using a struct and so by default every member is public. So any user of the class has access to its members.

Now, even if those data members were private, you've overloaded operator+ and operator< as member functions. And a member function has full access to any(private, public or protected) member of the corresponding class type.

Jason
  • 36,170
  • 5
  • 26
  • 60