I’m using Visual Studio 2008 Professional in C++ with Windows Form, I have a class that I have to pass to the other Form, so here is what I’m doing
//FirstFile.cpp
Usuario user;
user.usuario = "test";
user.senha = "stackoverflow";
ChooseService cs(user);
cs.ShowDialog();
//SecondFile.cpp
public ref class ChooseService : public System::Windows::Forms::Form
{
public:
Usuario* usuario;
ChooseService(Usuario user)
{
usuario = user;
//I need to cast the Usuario into Usuario*, so I can use it in the class
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
/...
as I said in my comment, I need to cast “Usuario” into “Usuario*” so I can access in the class using “this”, by the way I can’t make this
Usuario usuario;
instead of pointer, because windows forms doesn’t allow you, you have to use pointer. so how do I do this? is there any trick?
I hope I was clear enough, Thanks!
Getting the pointer from an object is easy:
But it might render the pointer invalid, because
useris in automatic storage and might be destroyed before you’re done usingpUser.I suggest you allocate
userdynamically and modify the constructor ofChooseServiceto take a pointer:In your particular case, your variant would work because both objects have the same lifetime, but it’s dangerous in the long run.