Hi there,

I have one class (MyClassA), with a method draw() from this method I am trying to access an instance of MyClassB. The instance has a variable v[X] and v[Y] which I need to access but can't because it is declared as private... Is this possible via pointers?

Could anyone also explain what & does?

Really keen to find out what the problem is here :/

MyClassA.cpp

void MyClassA::set(MyClassB*& inc){
   myVert=new MyClassB[n];
   for(int j=0;j<NUM;j++) myVert[i]=inc[i];
}

void MyClassA::draw(){
//LINE BELOW WHERE ERROR IS, Compile error : v is private.
int lol = myVert[0].v[X];

}

MyClassB.h

class MyClassB {

public:

MyClassB(const MyClassB& v);

private:
	float *v;
};

Recommended Answers

All 6 Replies

>> 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.

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)

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;
}

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.

ok thanks for your help. I think your right. Writing a getter function would be unwise here. Not because this program is used in a production environment. (It's a Uni assignment) . But the lecturer didn't intend for us to write any extra methods. I think I have misunderstood the orientation of Objects in the prog. Think I will go and read up more about Objects in C++ pointers e.t.c then return to this post.

Thanks for your help so far guys.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.