good evening guys.
I have a problem with my -simple- application.
I have 2 forms (Form1 - Form2), i create an object in Form1 (Person type) and i want to
get the values of data members when i focused to the Form2 and i press getPerson button.
But how can i do this?. I worked with static data and static functions but this is no correct cause if i want to create e vector<Person> person for example i could not to take correct values of my objects.

I attached my project. Thanks for reading :)

Dani AI

Generated

Short summary and practical plan based on the thread: the Person instance you create in Form1 must have a lifetime and an identity that Form2 can see. s static-field approach is the wrong tool (statics are shared across all instances). pointed at pointers/references or a shared container. correctly flagged the common bug: creating a local Person inside a click handler and then returning/using a pointer to it leads to a destroyed object.

Recommended, safe patterns (pick one that matches your UI flow):

  • Keep the Person as a member of TForm1 and give Form2 a reference or const-reference accessor. Simple, but Form1 must stay alive while Form2 uses it.
  • Allocate Person on the heap and share ownership with std::shared_ptr<Person>. Store persons in a vector<std::shared_ptr<Person>> if you need a collection; pass the shared_ptr to Form2 via constructor or a SetPerson method.
  • Use a repository/manager (singleton or application object) that stores Person objects indexed by id; forms ask the repository for the Person they need.

Example (heap + shared_ptr, minimal):

auto p = std::make_shared<Person>();
p->setHeight(...);
people.push_back(p);        // vector<std::shared_ptr<Person>>
auto f2 = new TForm2(this);
f2->SetPerson(p);
f2->Show();

And in TForm2:

void SetPerson(std::shared_ptr<Person> p) { person_ = p; UpdateUI(); }

Troubleshooting checklist: avoid passing pointers to stack objects; if you keep pointers to vector elements, remember vector reallocation can invalidate them—use smart pointers or reserve() to prevent reallocation. Verify Form2 reads the Person after SetPerson has been called (not before). Prefer const references for read-only access and smart pointers for shared lifetime. These steps remove the typical causes of “wrong” values when switching forms.

Recommended Answers

All 5 Replies

Static data, also known as class data, is the same for every object of a class. In this case static data members will not help you.

Since you are creating a Person objects in your Form1 ( Unit1.cpp ), you must either:
- pass the Person objects as a reference or a pointer to the Form2 ( Unit2.cpp ).
- store the Person object as a reference or a pointer in a container in a suitable namespace.

Please show me with an example code if u can :)

#ifndef personH
#define personH
class Person
{
        private:
//Try making height_ and weight_ protected instead of private
                static float height_;
                static int weight_;
        public:
                void setHeight(float height);
                void setWeight(int weight);

                static float getHeight();
                static int getWeight();
};
//---------------------------------------------------------------------------
#endif

My problem is that i can not access the object in a different form, when i create this object in form1 for example. The protected data members don't help me, perhaps it helped to use public data members but this is against the philosophy of object oriented programming.

You are correct that you don't want the height, weight and getter/setter methods for them to be static members of your class, or you can't have multiple instances with different values. So you need to fix that first, and then deal with where your Person-instances will live.

First of all, in your click-handler callback method TForm1::CreatePersonClick(), you have created a local instance of class Person, which will be destroyed when control returns from the function. There are a variety of ways to deal with this, depending on how you want to handle your TForm1 -- if the form will be dismissed after you create a person, then you can have your Person instance object be a member of the TForm1 class, and provide a pubic GetPerson() method which returns a reference to it, so TForm2 can call TForm1::GetPerson(); or if you want TForm1 to stay visible and create a new Person from the current values each time the user clicks the button, then (if you want to store the person for further use) you need to add the person into a vector<> or other container, and either pass it as an argument to the constructor of TForm2 when you create the second form, or provide a TForm2::SetPerson() method that your CreatePersonClick() method can call after it creates the TForm2 object.

I can provide you some simple code that does any of these things, but I'm not yet clear what exactly you want your program to do. I think if -you- think through your program clearly, it'll become more clear what needs to be done and where in the code to do it. Give it a try, and then post your revised code with additional specific questions.

Also, since your code is (so far) quite small, please consider including it in your post, with the source of each file labeled and surrounded by [ CODE ] tags, rather than attaching each file separately, so we can more easily scroll through and see what's going on without having to fire up VisualStudio or some external viewer. Thanks!

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.