Recently I began to study classes in C ++, I am interested in the question: how to transfer the value obtained in one class to another.

I tried through pointers, but nothing happened.

#include <iostream> using namespace std; class A { protected: int *a; public: void set_a(int set) { a = &set; } }; class B : public A { protected: int b; public: void show_b() { b = *(A::a); std::cout << b << endl; } }; int main() { A obj_a; B obj_b; obj_a.set_a(10); obj_b.show_b(); return 0; } 

It is necessary that the value of the variable a is passed to the variable b.

  • There are such things setters and getters. - Alex.B

2 answers 2

In fact, it is not clear from your sample code what you are trying to achieve. :)

However, with regard to your code, the parameters of the functions are their local variables, which will end their lives after the functions are completed. Therefore, in this function

 void set_a(int set) { a = &set; } 

pointer a will contain the address of an object that no longer exists, which will lead to undefined program behavior. You must create a copy of a local variable in dynamic memory and assign the address of the copy to a member of class a . In this case, you should not forget to release the memory allocated dynamically.

The program may look like this.

 #include <iostream> class A { protected: int *a = nullptr; public: ~A() { delete a; } void set_a(int set) { if ( a == nullptr ) a = new int; *a = set; } }; class B : public A { protected: int b; public: void show_b() { b = *(A::a); std::cout << b << std::endl; } }; int main() { B b; b.set_a( 10 ); b.show_b(); return 0; } 

Or you can rewrite the program as follows.

 #include <iostream> class A { protected: int *a = nullptr; public: ~A() { delete a; } void set_a(int set) { if ( a == nullptr ) a = new int; *a = set; } }; class B : public A { protected: int b; public: void show_b() { b = *(A::a); std::cout << b << std::endl; } }; int main() { B b; A &ra = b; ra.set_a( 10 ); b.show_b(); return 0; } 

In this example, a reference to an object of class B declared, but of type A Using this link you can refer only to methods declared in class A

As for your question

how to pass the value obtained in one class to another.

this is done using class methods that return the resulting value. :)

For example,

 #include <iostream> class A { private: int a; public: void set_a( int set ) { a = set; } int get_a() const { return a; } }; class B { private: int b; public: void show_b( const A &a ) { b = a.get_a(); std::cout << b << std::endl; } }; int main() { A a; a.set_a( 10 ); B b; b.show_b( a ); return 0; } 
  • Comments are not intended for extended discussion; conversation moved to chat . - Nick Volynkin

It is necessary to distinguish the class (type) and the object (instance) of this type. classes allow you to organize the code (divide it into parts). class instances (objects) share data. In this case, it is deeply wrong to give one class access to the internal data of another class, since in a complex case, when, for example, memory allocation / freeing a resource of one class is required, it is impossible to find out in another class that: 1) the memory has already been initialized, 2) the memory has already been freed. And this will lead to difficult to catch errors. That's right - to share resources and not give access to them outside the class instance.

1) the option when we need to have one object, but there is inheritance (we distribute the functional to different classes, but we use one object)
1. so wrong

 class A { protected: int *a; public: void set_a(int set) { a = &set; // так сохраняется адрес временной переменной set созданной для вызова этого метода, после возврата из метода адрес этой переменной не валиден } }; 

must be

 class A { protected: int a; public: void set_a(const int& set) { a = set; // так мы сохраним значение } }; 

2

 class B : public A { protected: int b; // просто не нужна, ибо B имеет доступ к полям A (public и protected) public: ... 

should be so

 class B : public A { public: void show_b() { std::cout << a << endl; } }; 
  1. in this example

    int main () {A a; a.set_a (10);

     B b; b.show_b( ); return 0; 

    }

a and b are different objects. so will work.

 int main() { B b; A *pa = &b; B *pb = &b; a->set_a( 10 ); b.show_b(); pb->b.show_b(); return 0; } 

2) we use two classes, two objects of different types, one of the classes has access to the data of the second class

 class A { int val; public: int get(void) const { return val; } void set(const int& v) { val = v; } }; class B { int val; public: B(const A& a) { val = a.get(); } void show(void) { std::cout << val << std::endl; } }; int main() { A a; a.set(10); B b(a); b.show(); return 0; }; 
  • Fixed, as you said, it still does not work. Calling the show_b () method of the obj_b object displays garbage. - Code
  • yeah, found another mistake, correcting the answer - vasily-vm