2

Header File (.h):

bool canTravelWith(const Passenger&) const;

Implementation File (.cpp):

bool Passenger::canTravelWith(const Passenger& x) const
    {
        bool canTravel = false;

        //if both passengers have the same destination on the same date...
        if (strcmp(x.m_destination,this->m_destination) == 0 && x.m_year == this->m_year && x.m_month == this->m_month && x.m_day == this->m_day)
        {
            canTravel = true;
        }

        return canTravel;
    }

Hey guys,

The code above works but what I wanted to know is if the parameter's object's members are privately accessed; how am I able to call that object's members inside my canTravelWith()?

In any other cases; I wouldn't be able to call an object's private members.

I want to understand why that is.

Thanks. (:

mincedMinx
  • 159
  • 1
  • 3
  • 12
  • 1
    I am confused, you are accessing the private members of your class? What is the problem with that? – Fantastic Mr Fox Oct 06 '17 at 17:40
  • 1
    So, you are asking why can you access `Passenger` class' private members from within the `Passenger` class? What? Consider reading a [good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Algirdas Preidžius Oct 06 '17 at 17:40
  • It is accessible because the object itself was passed as a reference to the function. – Viraj Padsala Oct 06 '18 at 03:13

2 Answers2

3

The definition of private and protected do not restrict you from accessing those sorts of properties or functions from within the same class.

This is a Passenger function so it has full access to everything. That there are two instances of an object in play does not restrict you. private does not mean other instances can't touch it, only other classes.

tadman
  • 208,517
  • 23
  • 234
  • 262
0

That's because a private member is a private member of a class, not an instance.

This is the same thing as asking why you can access the private member of this, since this is a pointer passed as parameter to your function, but simply hidden.

To put that in perspective, consider this code:

bool Passenger::canTravelWith(const Passenger& x) const
{
    Passenger const* ptr = this;

    // ptr is this, of course I can access private stuff!
    (void) ptr->m_destination;

    if (rand() % 10) {
        ptr = &x;
    }

    // Is the compiler will really try to guess if ptr
    // still points to this? privateness is a compile time
    // property of the class, not runtime.
    (void) ptr->m_destination;
}

Also, accessing private member don't require an instance at all:

struct Passenger {
private:
    static constexpr int value() { return 42; }

public:
    static constexpr int v = value();
};

As you can see, we are creating a static variable that his value came from a private static function. But since we are in the scope of the class, we can access it. Here's another example:

struct Outer {
private:
    int value = 42;

public:
    struct Inner {
        int getPrivate(Outer& o) { return o.value; }
    };
};

int main() {
    Outer o;
    Outer::Inner i;

    int v = i.getPrivate(o);
}

As you can see, Inner access private members of Outer, whithout being a friend. This is simply because Inner is inside the scope of the class.

Guillaume Racicot
  • 39,621
  • 9
  • 77
  • 141