There is a class. I need to conditionally create an object (s) by calling one of its constructors (several conditions, several constructors). But, as stated in the title, what is declared inside the body of an if is not considered declared outside this body.

Thus: if the condition works, then I create a class object, calling the constructor I need, and inside the body of this condition I can work with this object, everything is fine so far.

BUT as soon as I try to work with this object OUTSIDE the body of an if , the object is undeclared. As a result, I can not work with him. And with the usual variables, too, the same, but it does not matter.

I have to create objects in advance (for example, the simplest constructor), and then by if they are re-declared with the constructor I need, and the code runs, but it does not work correctly. The object does not change.

(and will rework everything for a long time so that it simply changes according to the condition, and not exactly created with the necessary constructor, and this is a very undesirable option).

I tried to delete / clear / call the destructor right after the object was declared, but, whatever one may say, the program did not work normally.

How it looks specifically in the code:

 int main () { /*1*/КЛАСС объект1, объект2; //Тут могут быть попытки сразу эти классы удалить/очистить/вызвать деструкто if (условие) {КЛАСС объект1(параметры нужного конструктора); Вывод объекта1; //Всё хорошо! } if (другое условие) {КЛАСС объект2(параметры нужного конструктора); Вывод объекта2; //Всё хорошо! } if (ещё одно условие) /* создание 1 или 2 объекта */ /* ... */ Вывод объекта1; //Выводит неправильно, ибо внутри этого объекта записано не то, //что мне нужно, а непонятно что Вывод объекта2; //Если же я убираю строчку /*1*/, то, как сказано в заголовке, //работать с объектами 1 и 2 я уже не могу. Компилятор говорит, что они не описаны. return 0; } 

Please tell me :

OR how to create an object in the if body so that it is considered to be described in main and I could work with it (I think you need to assign some necessary word before creating an object in the if body, but which one ???),

OR, as I, at the beginning, after / 1 /, delete / clear / call the destructor of objects 1 and 2 so that then they are newly created in the if body with the constructors I need, and then I could work with them later, and not to see some kind of nonsense in objects. Or just make a normal destructor, and just call it.

PS I think it does not play a special role in this problem, because it is quite generally described, but the class consists of an int array of 5x5 and another int , and several constructors for filling the array in the creation, several methods, several operator overloads, in general, nothing unusual. And so far, having called after / 1 / destructor, in which one line is delete [] int_массив_5x5 , I have achieved that what is needed is written to the object, except for the first two columns in which there is some kind of nonsense.

  • @Aftorik, To format the code, select it with the mouse and click on the {} button of the editor. - VladD

1 answer 1

See it.

First about what is happening.

If you inside the block declare an object with the same name as outside the block, then inside the block you work only with this new object, and outside - with the old one again. That is, the constructor does not do anything with the old block.

Now that needs to be done. Immediately the question: does it make sense to work with an object if you have not designed it? Well, that is, with the default constructor case? If not, then all work must be inside if 'a, and that’s the problem solved.

If you still need to “pull out” an object from the block, get a pointer to this object (do not forget to initialize it!), And select the object in the heap with new . Remember to destroy the object when you no longer need it. (If you know what a smart pointer is, it makes sense to use it.)

  • Thank! Yes, it was done through the pointer and {new}! - Aftorik
  • @Aftorik: Please! - VladD