Is a class implicitly a friend class of itself?

Write here if you have problems with your C++ source code

Moderator: Board moderators

Post Reply
ImLazy
Experienced poster
Posts: 215
Joined: Sat Jul 10, 2004 4:31 pm
Location: Shanghai, China

Is a class implicitly a friend class of itself?

Post by ImLazy »

Watch this code:

Code: Select all

#include <iostream>

using namespace std;

class A {
private:
    int i;

public:
    A() {
        i = 0;
    }

    void f(A& a) {
        a.i++;
        cout << a.i << endl;
    }
};

int main() {
    A a, b;
    a.f(b);
    return 0;
}
It is accepted by the compiler(g++ 4.1.0). I think it's strange. A class can access the member variable of another class with out declaring a friend. Is it a bug of the compiler or a feature of C++ language?
I stay home. Don't call me out.

Krzysztof Duleba
Guru
Posts: 584
Joined: Thu Jun 19, 2003 3:48 am
Location: Sanok, Poland
Contact:

Post by Krzysztof Duleba »

In C++ public/private/protected access restrictions work on class level, not on variable level. A class always has full access to all its members.
For millions of years, mankind lived just like the animals. Then something happened which unleashed the power of our imagination. We learned to talk and we learned to listen...

ImLazy
Experienced poster
Posts: 215
Joined: Sat Jul 10, 2004 4:31 pm
Location: Shanghai, China

Post by ImLazy »

Watch the function "f()" in my first post. It changes the value of a variable in another object. I think it's strange. I typed another code and it get compilation error.

Code: Select all

#include <iostream>

using namespace std;

class B {
private:
    int i;
};

class A {
public:
    void f(B& b) {
        b.i++;
        cout << b.i << endl;
    }
};

int main() {
    A a;
    B b;
    for (int i = 0; i < 10; i++) {
        a.f(b);
    }
    return 0;
}
In this code, class A cannot access the variable i of class B unless class B has declared a friend class. But in the first code I post, class A can access the variable i of another object of class A.
I stay home. Don't call me out.

sumankar
A great helper
Posts: 286
Joined: Tue Mar 25, 2003 8:36 am
Location: calcutta
Contact:

Post by sumankar »

You have aptly demonstrated what Krzysztof claimed!

Think of a problem where you have a class called Complex with two members of arithmetic type called real and img.

Also, assume that an object of class Complex cannot access the private member(s) of another object of the same class. Can you still go ahead and overload the `+' operator meaningfully?

Krzysztof Duleba
Guru
Posts: 584
Joined: Thu Jun 19, 2003 3:48 am
Location: Sanok, Poland
Contact:

Post by Krzysztof Duleba »

If you think about it, only class level access restrictions can be enforced by a compiler:

Code: Select all

// warning: not tested
class foo {
    public:
    int doit(const foo *f) const {
        return x + f->x;
    }

    private:
    int x;
};

int main() {
    foo *f1 = new foo;
    foo *f2 = new foo;
    if (rand() % 2 == 0) {
        f2 = f1;
    }

    f1->doit(f2);
}
Do you want this code to compile only half of the time?

Your intuition is wrong. You'll just have to learn that a class has always full access to all its members, regardless of which object they really belong to. With time you'll read/write such code without thinking twice.
For millions of years, mankind lived just like the animals. Then something happened which unleashed the power of our imagination. We learned to talk and we learned to listen...

sumankar
A great helper
Posts: 286
Joined: Tue Mar 25, 2003 8:36 am
Location: calcutta
Contact:

Post by sumankar »

Nice example. When you come to think about it, assuming that the restrictions were on a per object basis, we wouldn't be able go beyond:
Krzysztof Duleba wrote:...

Code: Select all

// warning: not tested
class foo {
    public:
    int doit(const foo *f) const {
        return x + f->x;
...
The compiler would never ever compile this!

Post Reply

Return to “C++”