Private access  
Author Message
Shouvik





PostPosted: Visual C++ General, Private access Top

Say if we ve certain private methods along with private data members and public methods.
other than friend function is thr any other way we access these members(they being private only) directly. its one of the requirements in a project of mine.
The real prob comes when accessing function.
say we've classes ported to us in .obj format. then going for DMA using pointers is a risky n unreliable job.

class x
{
private:
void meth1()
{
cout<<"Meth1";
}
void meth2()
{
cout<<"meth 2";
}
};
class xduplicate
{
public:
void virtual meth1();
void virtual meth2();
};
main()
{
x obj;
xduplicate *obj1;
obj1=(xduplicate *)&obj;
obj1->meth1();
}
we can access meth1 only if in class x it is virtual. i.e. the entry is in the virtual table.
But now i cant jst change the native code ported to us. i've to anyhow access the private methods without any change in native code.
wht we're plannin is x will be the producer class n xduplicate the consumer class.
x will be available as .obj along with .h file
so how can 1.

though one of the cheap n unprofesssional sol is 
#define private public
 
but this unwantedly unveals many non required methods toopen arena

plz hlp





Visual C++5  
 
 
Sarath.





PostPosted: Visual C++ General, Private access Top

You may need to adopt the unprofessional way


 
 
Shouvik





PostPosted: Visual C++ General, Private access Top

the unprofessional method somehow doesn't suits the integrity of the project can't there be any other way

 
 
einaros





PostPosted: Visual C++ General, Private access Top

Why on earth wouldn't you just design the base class so that the functions which should be public, *are* public

 
 
Shouvik





PostPosted: Visual C++ General, Private access Top

The answer is i'm not designing the base class. it'll be ported to me as .obj file. So its a constraint i cant change the class characteristics

 
 
einaros





PostPosted: Visual C++ General, Private access Top

The answer is i'm not designing the base class. it'll be ported to me as .obj file. So its a constraint i cant change the class characteristics

Well there's no completely safe solution to this problem. You could binary patch the obj file, or mock around with pointers such as in your original post, but neither would be very healthy for a production system IMHO.

An alternative (but equally horrifying) way to go about this, is to set up the call yourself:

class x
{
private:
void meth1()
{
cout<<"Meth1";
}
void meth2()
{
cout<<"meth 2";
}
};

x obj;
__asm
{
lea ecx, obj
call x::meth1
}



 
 
Shouvik





PostPosted: Visual C++ General, Private access Top

Thanks a lot.

after toiling for 4 weeks with 7 different forums this is the first working solution

will keep in touch

thanks again