There is such a class:

public class Env { private Hashtable table; protected Env prev; public Env(Env n) { table = new HashTable(); prev = n; } } 

The question is how to pass through the constructor an object of the same type in c ++?

  • Is it copying? - vegorov pm
  • Through the link. Just add a star. - nick_n_a pm
  • @vegorov, this is necessary for creating attachments, not copying - helldrg February pm
  • I mean, if you write Env(const Env&) , it will be a copy constructor. Env e; Env e1 = Env(e) Env e; Env e1 = Env(e) , so the option with a pointer (through an asterisk, as in the answer) is probably the only one. Well, either add a special method, such as Env::createFrom(const Env&) or Env::createFrom(const Env*) - vegorov February 1:49 pm

2 answers 2

You need to understand that in c ++ it is better to use not objects directly, but pointers or references. (an object is a continuous section in memory, if an object is without an indicator in the code - it is embedded in the scope-scope of another object, for “independence” [i.e. that it would not be embedded] you need to use a pointer or link) the places of objects' declarations - pointer (asterisk). By the way - new - returns the pointer. For simplicity, a pointer is also passed to java, but the syntax is simplified.

 class Env { private: Hashtable * table; protected: Env * prev; public: Env(Env * n) { table = new HashTable(); prev = n; } } 

Write Hashtable table; valid, but the object will then be created and destroyed implicitly (new is not required), and the object has a "stationary place", i.e. it cannot be directly "copied". Depending on the specific situation, it is possible or impossible to do this (you need to understand the visibility zone and the exit for it, for table without new it is possible, for prev it is impossible because the assignment goes). You can separately read about objects in s ++. In most cases, it is easier to use * when transferring objects.

I sometimes confuse the names link & and pointer * . The pointer may be null . Here we are talking about a pointer that refers to the object. Instead of a dot with a pointer, you will have to write -> (for example, table->toString() ). Working with a reference type or with an object without a pointer in с++ is somewhat more difficult (but there is a familiar point).

UPD some more. In c ++, there is no garbage collector by default, unlike java - the amount of new should later be equal to delete (which would not be a memory leak), but there is also a cleaner in the form of separate libraries.

  • What does it mean "what in c ++ does not use objects directly, but pointers"? - AR Hovsepyan
  • Perhaps yes. we use, not with ++ uses. - nick_n_a
  • Then Env should have another constructor (default constructor) so that the pointers are initialized - AR Hovsepyan
  • The default constructor ... is not needed until a certain point. (depends on specifics) - nick_n_a February
  • there is no desire to add an answer, I just wanted everything to be correct. See: if there is only one constructor, i.e. we are trying to construct an object through a pointer to an object that has two raw pointers (not initialized). And what prev will indicate after prev = n; ? Yes, we can somehow initialize n-> prev first, but the object invariant is violated all the time - AR Hovsepyan

When translating code from one language to another, many nuances arise, especially when languages ​​do not have any kindred relationship with each other.

Java uses reference data types, i.e. For a public Env(Env n) record, public Env(Env n) actually transfers the reference (read as addresses) of the object n to the constructor of the Env class. Those. Immediately before the start of the designer’s code, there is no copying of user data yet.

It should also be borne in mind that there is a feature in Java that uses the garbage collector (Garbage Collector), and (for the most part) there is no need to keep track of the lifetime of objects, if the link to the object is available, then the object itself is also live.

The essence of your designer - the formation of a linear unidirectional list. Each new Env object creates a table and remembers the passed argument as some previous object. Here it is worth considering the option that calling Env(null) essentially starts a new list, losing all previous objects, if they are not remembered somewhere else additionally.

Given the need to pass null to the constructor, a suitable use case in c ++ will be a pointer. But since C ++ does not have a garbage collector, and monitoring the validity of objects using raw pointers is not very rewarding work in the modern world, the most equivalent way here is to use a smart pointer, i.e. wrappers like std::shared_ptr (the object is alive as long as the pointer is available and the object is not copied when copying pointers, almost like in Java!) above your Env . And for table as a private member, it is wiser to completely abandon the pointer and store it by value. Thus, the resulting C ++ code, which is as close as possible to the Java code, seems to me like this:

 #include <memory> class Hashtable { /* */ }; class Env { public: Env(std::shared_ptr<Env> n) { prev = n; } protected: std::shared_ptr<Env> prev; private: Hashtable table; }; 

You may notice that in C ++ there is no need to create an explicitly table object, since it is stored in class by value, and in the constructor, Env is created by the default constructor (essentially the same as in Java by calling new HashTable(); )