Original Post
I was experimenting with some classes and inheritance and what not, and I came about this 'discovery' that somewhat made sense and at the same time was somewhat shocking. I use discovery lightly, because I don't know if this is commonly known, but I've certainly never heard of it.
If you have a class A and a class B that aren't related, then you can't access any of B's private or protected properties, correct?
However, I've found that if you instead pass a reference to an object of the same class, you can access the private properties.
This may be a stupid question to some of you, but I just found it somewhat bizarre as well as obvious. Is it supposed to work this way, or is it inefficient from an OO standpoint as far as extendability and maintenance goes? If that same reference "same_class" was used anywhere else but inside a method of that class, you wouldn't be able to access the private property.
Edit: So that some of you can understand why I'd be using this, I have a class with a method called "addChild" that takes a reference to an object of the same type. This class also contains a private property called "parent" that I don't want accessible outside of the class. Because the child object was of the same type, I found that I could access it's private parent property with no problem from inside the class, which was great but left me questioning whether or not it was the 'right' way.
Example:
If you have a class A and a class B that aren't related, then you can't access any of B's private or protected properties, correct?
class B {
public:
B() {
privateProperty = 25;
// arbitrary value for testing
}
private:
int privateProperty;
}
class A {
public:
A(B &ref_to_b) {
printf("%d\n", ref_to_b.privateProperty);
// error, can't access private property in B
}
}However, I've found that if you instead pass a reference to an object of the same class, you can access the private properties.
class A {
public:
A() {
privateProperty = 25;
// default constructor
}
A(A &same_class) {
printf("%d\n", same_class.privateProperty);
// success, can access private property of referenced object
}
private:
int privateProperty;
}This may be a stupid question to some of you, but I just found it somewhat bizarre as well as obvious. Is it supposed to work this way, or is it inefficient from an OO standpoint as far as extendability and maintenance goes? If that same reference "same_class" was used anywhere else but inside a method of that class, you wouldn't be able to access the private property.
Edit: So that some of you can understand why I'd be using this, I have a class with a method called "addChild" that takes a reference to an object of the same type. This class also contains a private property called "parent" that I don't want accessible outside of the class. Because the child object was of the same type, I found that I could access it's private parent property with no problem from inside the class, which was great but left me questioning whether or not it was the 'right' way.
Example:
class A {
public:
A *addChild(A &child) {
child.parent = this;
// extra code here to add the child
// to an array of children in this object
return child;
}
private:
A *parent;
// array of children here, e.g.
// A children[20]; or something
}