Hello,

I am a newbee in C++. I am trying to have a connection setup and trying to access private varaibles of a class. Please look at my piece of code below. Any comments would be greatly appreciated.
Basically, I want to access two private varaibles from MyClass into AnotherClass(myNewClass = new MyClass(?, ?)).

Thanks.
Amy

//start of header
MyClass.h
class MyClass{
public:
//constructor
MyClass(xyz* const Var1_X, mno* Var2_M);

private:
xyz* const PrivateVar1;
mno* const PrivateVar2;
}
//end of header

//start of source
MyClass.cpp
MyClass(xyz* const Var1_X, mno* const Var2_M):PrivateVar1(Var1_X),PrivateVar2(Var2_M)
{
}
//end of source

//start of another source
AnotherClass.cpp
Class AnotherClass{
private:
MyClass* myNewClass;

public:
myNewClass = new MyClass(how do i get those two private varaibles here?)
//end of another source

Recommended Answers

All 2 Replies

Make the two classes friends.

Could you give an example of how I do that in my case. Also, is there a way to do it ONLY by modifying AnotherClass.cpp or AnotherClass.h file and not modiying any other .cpp files above?

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.