>> I need to access but can't because it is declared as private...
In MyClassB write a get() function that returns the value of the private variable, and you can also write a put() variable that lets you change it.
>>Could anyone also explain what & does?
Depends on the context -- when used in the parameter to a function it is a reference operator. If you don't know what that is then look it up in your text book.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
Look at this problem from the other side. Ask yourself: why I need access to the private variable of the other class?
You (or the 2nd class author) declared this variable as a private one to prevent such access. For example, only the 2nd class member functions guarantee its proper utilization with lock/unlock machinery, increase/decrease reference counters or what else. May be, you want to prevent unexpected assignments from the outer world because of special assumptions etc.
If you want to access a private variable at any price, it's a sure sign of:
- It was incorrect class design - no need to declare a variable as a private, or declare this variable "getter" member function (see Vernon's post) if it's read-only style variable.
- or you have a tightly coupled "class family" where every member works on its part of a common task. In that case you may declare friendship between these classes:
class B; // predeclare B if its declaration placed after A
class A { friend class B; ... }; // Now A can access privates of B
class B { friend class A; ... }; // Now B can access privates of A
- or see the 1st point again: redesign both classes (if you can do it)
ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
Not saying you should use it, but one way is to remake the structure with the same variable types and sizes, but leave all the members public, heres an exmaple.
#include<iostream>
using namespace std;
class A {
private:
int a;
public:
A() { a = 10; }
};
class B {
public:
int a;
};
int main() {
A a;
B *b = reinterpret_cast<B*>(&a);
cout << b->a; // Displaying private member in a
cin.ignore();
return 0;
}
William Hemsworth
Posting Virtuoso
1,591 posts since Mar 2008
Reputation Points: 1,429
Solved Threads: 129
Not saying you should use it, but one way is to remake the structure with the same variable types and sizes, but leave all the members public, heres an exmaple.
That's not a reasonable solution. If class A contains hundreds of lines of code/methods, do you really intend to duplicate all of them in class B, then try to make class B act like class A? Sorry, but IMO that is just a lousy suggestion.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
That's not a reasonable solution. If class A contains hundreds of lines of code/methods, do you really intend to duplicate all of them in class B, then try to make class B act like class A? Sorry, but IMO that is just a lousy suggestion.
You dont need to rewrite all the code / functions. Just the variables you need. Heres another example :)
#include<iostream>
using namespace std;
class A {
private:
int a;
int b;
int c;
int d;
int e;
int f;
int g; // Want to acess this variable
public:
void SetG(int value) {
g = value;
}
};
class B {
public:
int unneeded[6];
int g;
};
int main() {
A a;
a.SetG(100);
B *b = reinterpret_cast<B*>(&a);
cout << b->g;
cin.ignore();
return 0;
}
I personally dont see a problem with it, if it works.
William Hemsworth
Posting Virtuoso
1,591 posts since Mar 2008
Reputation Points: 1,429
Solved Threads: 129