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 :)

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.