13

Possible Duplicate:
With a private modifier, why can the member in other objects be accessed directly?

Private members of a C++ class are designed to be invisible to other class instances. I'm confused since private members can be accessed as shown below! Can anyone explain it to me?

Here's my code:

#include <iostream> 
using namespace std; 
class Person
{
private:
    char* name;
    int age;
public:
    Person(char* nameTemp, int ageTemp)
    {
      name = new char[strlen(nameTemp) + 1];
      strcpy(name, nameTemp);
      age = ageTemp;
    }
    ~Person()
    {
      if(name != NULL)
        delete[] name;
      name = NULL;
    }
    bool Compare(Person& p)
    {
      //p can access the private param: p
      //this is where confused me
      if(this->age < p.age) return false;
        return true;
    }
};
int main() 
{ 
  Person p("Hello, world!", 23);
  return 0; 
}
Community
  • 1
  • 1
Triumphant
  • 948
  • 1
  • 11
  • 28
  • 8
    It's on a per-class basis, not a per-object one. – chris Dec 23 '12 at 02:38
  • 1
    Try accessing p.age from your main function - that's what it prevents, access outside the class. – PeterJ Dec 23 '12 at 02:43
  • Use std::string to hold strings. Currently because you don't you are not obeying the rule of three (which is a problem). – Martin York Dec 23 '12 at 05:09
  • 1
    Remember that access specifiers is not a security mechanism. It is a mechanism to prevent accidental incorrect changing of an objects state by **other** authors. As the author of **person** you are expecting to be able to correctly modify the state from other instances. – Martin York Dec 23 '12 at 05:16

5 Answers5

21

Methods of a class can access its private attributes throughout all class instances, at least in C++.

Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
Phil Rykoff
  • 11,999
  • 3
  • 39
  • 63
  • Hmm.. +1 'cos this answer is correct, but does anyone do such a thing often? I'm struggling to think of any time I've needed to do it. – Martin James Dec 23 '12 at 09:52
  • You most likely need it when overloading an operator, nearly as in the example given. Other than that, I presume it's a rare use case. – Phil Rykoff Dec 23 '12 at 10:53
6

Any snippet of code that "belongs" to the type Person will be able to access public, protected and private variables of any Person object.

The type matters here, not the instance.

cmc
  • 2,061
  • 1
  • 19
  • 18
4

When a variable is designated private, it means that only methods of this class and optionally classes and methods designated as friends can access it. Any instance of the class can access private variables of all other instances of the same class.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

Any access outside of the class is "prohibited". Unless you for example use the friend class. (Which let's any other class access to the private members of the class)

You can define friend-class like this:

class MyClass {
    friend class MyClassB;
public:
    MyClass();
    ~MyClass();
private:
    int number;
};

Then MyClassB will have access to the private variables of "MyClass".

If you do something like this:

class MyClass {
public:
    MyClass();
    ~MyClass();
private:
    int number;
};


int main(int argc, char *argv[])
{
    MyClass A;
    A.number = 11; // You can't do this
    if(A.number > 10) { // Either you can't do this.
        qDebug() << "It's more than 10"; // Qt. well.
    }


    return 0;
}

You'll get an "error" because you're trying to access A.number from outside of the class.

But if you want to access from some function inside the class:

class MyClass {
public:
    myClass() {
       number = 10;
       if(number > 10) {
           qDebug() << "It's more than 10"; // Qt. well.
       }
   }
    ~MyClass();
private:
    int number;
};
Blastcore
  • 360
  • 7
  • 19
1
if(this->age < p.age) return false;

p.age: age is private, and can access like this because this line is inside the method of class, so it can access all private member.

if you put p.age outside method of class, it will notice error. For example:

int main() 
{ 
    Person p("Hello, world!", 23);
    // will be error, because main is not inside class People
    if (p.age < 18) {   
       cout << "You are not adult" << endl;
    }
    return 0; 
} 

Hope this help :)

hqt
  • 29,632
  • 51
  • 171
  • 250