- Struct2 has a function called Function() where inside definition is another windowsapi function. This function needs a member variable from Struct1 which is HANDLE hProcess.
- What are the different ways to access the member variable hProcess from Struct1 in Struct2 then call it from Main? Please list a couple different methods on achieving what I want, besides using extern.
Yes I have but still don’t know how it will work because Struct1.cpp stores the HANDLE hProcess which stores the process handle of my process.
I need to use the exact same member variable hProcess from Struct1 inside of Struct2.
Can you show an example how to make a constructor work with my example? No one teaches how to do it across 2 seperate source files.
The link I gave you shows you how to do it. And if I write the code for you, you'll never learn this stuff for yourself (it's literally against my religion to not teach, and just give stuff away). So again, don't just look at constructors, actually write some and work with them til you understand it. It's a really important part of programming, learning to work with parameters in various ways. I think you're trying to jump straight into something more complex than you should, when you seem to lack some of the most basic things needed to write C++.
Here is another link for learning C++: [Link]
But for my code it is abit different because I have 2 seperate classes in 2 seperate source files.
Struct1 : is already storing my process handle inside its member variable called HANDLE hProcess.
Struct2 : has a function definition and inside definition is another function which requires the exact copy of Struct1 member variable hProcess.
How can I pass arguments into a constructor so that I can use Struct1 hProcess in Struct2?
...
Have you written any constructors yet? Post what you have written or even what you tried. But so far you really haven't showed much of anything. But again, create the class constructors, then pass the handle to both classes upon creation of them.
Have you written any constructors yet? Post what you have written or even what you tried. But so far you really haven't showed much of anything. But again, create the class constructors, then pass the handle to both classes upon creation of them.
Does that mean I need to create another new variable in the animal struct called hProcess when passing with constructor?
#include <iostream>
using namespace std;
class Line {
public:
void setLength( double len );
double getLength( void );
Line(double len); // This is the constructor
private:
double length; // The variable to store the length
};
// Member functions definitions including constructor
Line::Line( double len) {
cout << "Object is being created, length = " << len << endl;
length = len;
}
void Line::setLength( double len ) {
length = len;
}
double Line::getLength( void ) {
return length;
}
// Main function for the program
int main() {
Line line(10.0);
// get initially set length.
cout << "Length of line : " << line.getLength() <<endl;
// set line length again
line.setLength(6.0);
cout << "Length of line : " << line.getLength() <<endl;
return 0;
}
just do it with both classes and pass the same handle when you create the instances.