Good day. There is a class that changes the console encoding. Code:

#include "stdafx.h" #include <iostream> #include <Windows.h> class edit_locale { int old_cin; int old_cout; public: edit_locale(int); ~edit_locale(); }; int main() { edit_locale(1251); std::cout << "Иван" << std::endl; return 0; } edit_locale::edit_locale(int cp) { this->old_cin = GetConsoleCP(); this->old_cout = GetConsoleOutputCP(); SetConsoleCP(cp); SetConsoleOutputCP(cp); } edit_locale::~edit_locale() { SetConsoleCP(this->old_cin); SetConsoleOutputCP(this->old_cout); } 

The problem is this: the SetConsole function works only in the constructor; in the main function, no changes occur. And the destructor is not called. With what it can be connected? Thank.

    2 answers 2

    Problem in line

     edit_locale(1251); 

    You create a temporary object, which is immediately destroyed in the same line.

    Write like this:

     edit_locale l(1251); 
    • By the way, it is possible and time to manage - as for me, so even in some sense is more beautiful :) - see the addition in my answer. - Harry

    Try to create an object :)

     int main() { edit_locale el(1251); std::cout << "Иван" << std::endl; return 0; } 

    Without this, in main() a temporary object is created (and right there, before the line is output).

    To make it clearer -

     #include <iostream> class test { public: test() { std::cout << "ctor\n"; } ~test() { std::cout << "dtor\n"; } }; void f() { test(); std::cout << "in f\n"; } void g() { test t; std::cout << "in g\n"; } int main() { std::cout << "call f()\n"; f(); std::cout << "call g()\n"; g(); } 

    Update

    You can do with a temporary object - if you do this:

     #include <iostream> #include <Windows.h> class edit_locale { int old_cin; int old_cout; public: template<typename T> edit_locale(int,T f); ~edit_locale(); }; int main() { edit_locale{1251,[](){ std::cout << "Иван" << std::endl; }}; return 0; } template<typename T> edit_locale::edit_locale(int cp, T f) { this->old_cin = GetConsoleCP(); this->old_cout = GetConsoleOutputCP(); SetConsoleCP(cp); SetConsoleOutputCP(cp); f(); }; edit_locale::~edit_locale() { SetConsoleCP(this->old_cin); SetConsoleOutputCP(this->old_cout); } 

    By the way, I always wanted in C ++ to have the opportunity for all kinds of lockes and their ilk to write not

     { lock lk; // что-то делаем } 

    but

     lock{ // что-то делаем } 

    So, in my opinion, it looks clearer. With lambdas we have a certain surrogate ...

    • one
      Please do not tell anyone that I have created this question .. yes as such a pancake ... As I looked through?: D Thank you! - Range
    • 2
      @Range Agreed :) - Harry
    • @Range By the way, take a look at the addition to the answer ... - Harry
    • @Harry: For such additions, welcome to C # :) There for such purposes there is a using : using (TemporaryLocale(1251)) { Console.WriteLine("ляляфафа"); } using (TemporaryLocale(1251)) { Console.WriteLine("ляляфафа"); } . - VladD
    • @Harry: The new edit_locale problem will be if someone accidentally writes it to a variable: edit_locale{1251,[](){ ... }} l; . - VladD