Wheels-Of-Fire

Forum Replies Created

Viewing 20 posts - 121 through 140 (of 1,996 total)
  • Author
    Posts
  • in reply to: C++ Programming #67861
    Wheels-Of-FireWheels-Of-Fire
    Participant
      @grahamdearsley
      Forumite Points: 4

      Yes you would probably put things like system() calls in a header file so that if you changed anything then only that file would need to be recompiled.

      The ANSI thing is new in Windows, apparently it was widely used on UNIX systems but you had to load the ANSI.SYS driver to make it work on DOS and it never really caught on, Windows didn’t bother supporting it at all until last month.

      The Windows Terminal program also supports ANSI codes now and there is talk of merging its code with the Windows console code.

      Again its all to do with enhanced Linux support. If you use a Windows machine to remotely connect to a Linux machine then you may get sent ANSI control codes. Apparently 😁

      in reply to: C++ Programming #67844
      Wheels-Of-FireWheels-Of-Fire
      Participant
        @grahamdearsley
        Forumite Points: 4

        And on a related note, the Visual Studio Console Window now supports ANSI control codes so cout << char(27) << “[4;31m Hello”; prints Hello in underlined red text.

        Its a bit pointless at the moment though because the standard CMD window still does not, your .EXE file will just print the control codes.

        Apparently its part of the Linux subsystem support.

        in reply to: C++ Programming #67840
        Wheels-Of-FireWheels-Of-Fire
        Participant
          @grahamdearsley
          Forumite Points: 4

          Just came accross an interesting function thats not in any of my books.

          system() let’s you issue commands to your systems command processor so system(“CHCP 850”) will set your console code page to CP 850 and system( “DATE”) will display the date.

          In fact you can issue any command  that you could in a command prompt window.

          in reply to: C++ Programming #67802
          Wheels-Of-FireWheels-Of-Fire
          Participant
            @grahamdearsley
            Forumite Points: 4

            Here is some real working code wot I just wrote:

            #include<iostream>
            #include<vector>

            using namespace std;

            int main()
            {
            vector<int> test{ 1,2,3,4 };

            vector<int>::iterator begin = test.begin();
            vector<int>::iterator end = test.end();

            while (begin != end)
            {
            cout << *begin << endl;
            ++begin;
            }
            }

            Prints the numbers 1-4 and does not attempt to print a fifth element. Instead of specifying the iterator type I could have just bunged in “auto” but seeing as I have been looking at container member types I thought I would do it long hand.

            in reply to: C++ Programming #67801
            Wheels-Of-FireWheels-Of-Fire
            Participant
              @grahamdearsley
              Forumite Points: 4

              Ah thats the thing, the last element pointed to by begin will indeed be one off the end but because begin will be equal to end we do not attempt to assign a value to it.

              in reply to: C++ Programming #67798
              Wheels-Of-FireWheels-Of-Fire
              Participant
                @grahamdearsley
                Forumite Points: 4

                Oh yeh, the type of value and value1 ends up being ptrdiff_t.

                in reply to: C++ Programming #67797
                Wheels-Of-FireWheels-Of-Fire
                Participant
                  @grahamdearsley
                  Forumite Points: 4

                  I have been looking at some of the things that container types provide, this time in the “probably don’t need to know” category.

                  Containers provide a number of TYPE members that are designed for generic programming without regard for your particular system, they will be defined to match your environment.

                  An example is difference_type below:

                   

                  #include <iostream>
                  #include <vector>

                  using namespace std;

                  int main()
                  {
                  vector<int> test{ 1,2,3,4 };

                  vector<int>::difference_type value = std::distance(test.begin(),test.end()) ;
                  cout << value << endl;

                  vector<int>::difference_type value1 = test.end() – test.begin();
                  cout << value1;
                  }

                  This outputs 4 twice.

                  in reply to: C++ Programming #67796
                  Wheels-Of-FireWheels-Of-Fire
                  Participant
                    @grahamdearsley
                    Forumite Points: 4

                    The C++ Primer book recommends always using the pre increment form to avoid unexpected results, unless you really DO know what the post increment will do in an expression. As a stand alone operation it don’t matter 😁

                    in reply to: C++ Programming #67795
                    Wheels-Of-FireWheels-Of-Fire
                    Participant
                      @grahamdearsley
                      Forumite Points: 4

                      It would work the same either way. The difference between the pre and post increment is that if you stick the ++ in front then the increment will occur before any other operations in an expression that its in.

                      in reply to: C++ Programming #67758
                      Wheels-Of-FireWheels-Of-Fire
                      Participant
                        @grahamdearsley
                        Forumite Points: 4

                        I have found a reason why the end() iterator points to one past the end of a sequence, it seems to be entirely so I can write code like this.

                        while (begin != end)

                        { *begin = val;

                        ++begin;}

                        The begin/end range is known as a “left inclusive interval” so it includes the element denoted by begin but not the one denoted by end.

                        The above while loop will read all the elements in the range but not the one past the end. If end() pointed to the last element then that element would not be read.

                        in reply to: C++ Programming #67756
                        Wheels-Of-FireWheels-Of-Fire
                        Participant
                          @grahamdearsley
                          Forumite Points: 4

                          It is the stated aim of the C++ committee to bring out a new version every 3 years and they have done since C++11, whether they have anything worth releasing or not 😂

                          in reply to: C++ Programming #67749
                          Wheels-Of-FireWheels-Of-Fire
                          Participant
                            @grahamdearsley
                            Forumite Points: 4

                            C++11 was a long time in development and it was a major advance. C++14 tidied up a few things that C++11 still didn’t get quite right. Anything since then does indeed look like it was done just for the sake of it.

                            I haven’t got any books that cover the final release of C++20 but the bits I have seen all add extra complication, modules instead of #include is a case in point.

                            in reply to: C++ Programming #67583
                            Wheels-Of-FireWheels-Of-Fire
                            Participant
                              @grahamdearsley
                              Forumite Points: 4

                              Hmm. It seems to me that the only thing a degree in computer science qualifies you for is writing papers about computer science 😁

                              But seriously, some types of sort are faster in Python 2 than they are in C++ prior to C++20 ?

                              Im not going to lose any sleep 😂

                              in reply to: C++ Programming #67573
                              Wheels-Of-FireWheels-Of-Fire
                              Participant
                                @grahamdearsley
                                Forumite Points: 4

                                One more (for the moment 😁) note on constructors.

                                The fact that you can provide in class initialisers is new to C++11 and if you provide them for all objects of built in type then you can use:

                                class name () = default;

                                That will generate a default constructor for the built in types even if you did supply constructors for other types.

                                Another thing is that if one of your class data objects is its self a class type and that object has its own constructor then that will be used to initialise the object, no need to supply another one.

                                in reply to: C++ Programming #67561
                                Wheels-Of-FireWheels-Of-Fire
                                Participant
                                  @grahamdearsley
                                  Forumite Points: 4

                                  And a note on Destructors.

                                  If you dont’t supply a destructor then the compiler will synthesise a default destructor for you. Even if you use “new” to allocate an object on the heap but forget to use “delete” the default destructor will delete the object for you when the class goes out of scope. One reason why garbage collection is not required.

                                  If you wan’t an object to remain in memory after the class goes out of scope then you must declare it “static”, such objects are your own look out 😃

                                  in reply to: C++ Programming #67557
                                  Wheels-Of-FireWheels-Of-Fire
                                  Participant
                                    @grahamdearsley
                                    Forumite Points: 4

                                    Just a note on class constructors, to remind me as much as anything else 😁

                                    ALL class data members are initialized by a comstructor, if you don’t provide ANY then the compiler will define synthesised default constructors. If you provide an initializer in class:

                                    int avar = 0;

                                    Then the default constructor will use that but if not it will use the default, no variables are left uninitialized.

                                    A thing to note is that if you supply even ONE constructor then you must supply ALL of them because defaults will no longer be generated.

                                    Default constructors do not appear in your source code but they will be there in the compiled code.

                                    in reply to: Strange Problem #67543
                                    Wheels-Of-FireWheels-Of-Fire
                                    Participant
                                      @grahamdearsley
                                      Forumite Points: 4

                                      You are not alone with this problem, unfortunately it looks like it may be a hardware fault, if you have the intel WiFi module then it almost certainly is 🙄

                                      in reply to: 6 Weeks and I am out. #67516
                                      Wheels-Of-FireWheels-Of-Fire
                                      Participant
                                        @grahamdearsley
                                        Forumite Points: 4

                                        Unless you have previous experience and it still hasn’t put you off then please don’t go for a boat !

                                        in reply to: C++ Programming #67496
                                        Wheels-Of-FireWheels-Of-Fire
                                        Participant
                                          @grahamdearsley
                                          Forumite Points: 4

                                          I have come to notice something about ALL teach yourself C++ books, they are ALL mighty big on function and class declarations but tiny on definitions.

                                          I understand that there is limited space in the book but you can’t play with a code fragment when most of it is missing.

                                          Classes are the worst because they can have internal and external functions.

                                          The definition of internal functions just gets replaced with “// definition goes here” and external definitions don’t get a mention.

                                          in reply to: C++ Programming #67484
                                          Wheels-Of-FireWheels-Of-Fire
                                          Participant
                                            @grahamdearsley
                                            Forumite Points: 4

                                            I was thinking the same thing.

                                            C with classes introduced nice concrete classes that enclosed things that belonged together into an object, you could use derived classes with inheritance if you wanted too.

                                            C++ brought in constructors and destructors which help greatly in ensuring that a class frees all its resources when it goes out of scope. All part of what Stroustrup calls RAII and if used correctly it eliminates any need for a garbage collector because you don’t leave any garbage.

                                            Next up was template classes which helps with writing generic classes, <vector> is a template class.

                                            As far as I am concerned they could have stopped there !

                                          Viewing 20 posts - 121 through 140 (of 1,996 total)