Forumite

C++ Programming

HOME Forums Tech Makers & Builders Programming/Code tips C++ Programming

  • Creator
    Topic
  • #27773
    Participant
    Wheels-Of-Fire
    @grahamdearsley

    <p style=”text-align: right;”>Can we put this topic up please boss. In tech ?</p>

Viewing 20 replies - 101 through 120 (of 180 total)
  • Author
    Replies
  • #62569
    Participant
    Wheels-Of-Fire
    @grahamdearsley
    Forumite Points: 6,522

    Another “Helpful” C++ feature that is bound to lead to confusion, this time with vectors.

    It is possible to initialize a vector in a number of ways and from C++ 11 that includes a list enclosed in curly braces.

    vector<int> V1 {10,20}

    That is a vector with two elements of 10 and 20.

    You can also initialize a vector with a number of default elements using brackets.

    vector<string> V1 (10, “hi”)

    That vector has 10 elements and they all have the value “hi”.

    So far so good.

    However if you attempt to list initialize a vector with braces but supply the wrong type C++ will “helpfully” attempt a value initialization instead.

    vector<string> V1 {10, “hi”}

    This gets you a vector of 10 hi’s again even though you used curly braces.

    It is going to confuse me if no one else 😆

    #62571
    Participant
    Wheels-Of-Fire
    @grahamdearsley
    Forumite Points: 6,522

    In case it’s not obvious, i’m going through my book again from the beginning and picking out the bits I missed the first time.

    Expect a lot more 😁

    #62598
    Participant
    Wheels-Of-Fire
    @grahamdearsley
    Forumite Points: 6,522

    I mentioned before that my book keeps banding on about “const” objects and now i’m re reading it there is still a situation I don’t see the point to.

    I can pass a reference to a function as follows.

    Void funct(int &val)

    { val= 10}

    int v=0

    void funct(int &v)

    The result of the above will set the value of v to 10.

    Fair enough.

    The thing is though I can also pass a const reference so the function can’t change its value.

    void funct(const int &val)

    Can anyone tell me what the point of that is ? Objects are already passed by value by default so you can’t change the value of the argument, surely the point of passing by reference is so you CAN change it ?

    #62602
    Participant
    Ed P
    @edps
    Forumite Points: 17,429

    This may help explain C++’s very kludgy use of const.

    link

    #62613
    Participant
    Wheels-Of-Fire
    @grahamdearsley
    Forumite Points: 6,522

    Ah yes there is a reason and in fact my C++ book gets round to it a few pages further on.

    If you are passing a large structure to a function instead of a simple variable then passing a const reference will negate the need to create a copy to pass, as would be done if you passed by value.

    Another thing the book gets into later is top level and low level constants and where the constantness of a top level constant may be ignored !

    #62615
    Participant
    Wheels-Of-Fire
    @grahamdearsley
    Forumite Points: 6,522

    I also see that the author of the article isn’t keen on the stupid operator overloading of the & symbol either 😁

    #62625
    Participant
    Wheels-Of-Fire
    @grahamdearsley
    Forumite Points: 6,522

    My book is being very careful to point out the difference between a parameter and and argument. When you declare a function you supply a list of the parameters it takes and when you call a function you supply a list of arguments with the same type as the parameters.

    I don’t really see why the book is making such a big thing about the distinction between the two.

    #62627
    Participant
    Ed P
    @edps
    Forumite Points: 17,429

    If I remember correctly, parameters CAN be changed in a function but arguments cannot. Iirc you use const to stop a parameter being changed.

    #62632
    Participant
    Wheels-Of-Fire
    @grahamdearsley
    Forumite Points: 6,522

    I just found out that i’m not going to be stuck with programming the Windows API directly after all. Visual studio includes the Microsoft Foundation Class library for C++ programmers. The library includes a load of predefined classes for doing common tasks like managing windows and programming Windows sockets. There are even wizards to auto generate code for some tasks.

     

    #62637
    Participant
    Ed P
    @edps
    Forumite Points: 17,429

    You can even use the low level Win32 GDI graphics library in C#, which is otherwise locked down even tighter than C++. link

    Not for me I’m afraid, I learned to program in the Wild West days of free access to everything including programming self-modifying Assembler routines. As a result  ‘safe’ programming is boring and anathema to me, and modern C++ is on the very boring list of bloated programming languages.

    #62644
    Participant
    Wheels-Of-Fire
    @grahamdearsley
    Forumite Points: 6,522

    https://docs.microsoft.com/en-us/cpp/mfc/general-mfc-topics?view=vs-2019

    Have a look at the above link. Although MFC calls its self a framework it is actually just a useful class library, it in no way restricts what you can do with C++. C# on the other hand, well its not even a proper compiled language, you MUST go through the framework and it intermediate language.

    #62646
    Participant
    Wheels-Of-Fire
    @grahamdearsley
    Forumite Points: 6,522

    https://docs.microsoft.com/en-us/cpp/mfc/general-mfc-topics?view=vs-2019

    Have a look at the above link. Although MFC calls its self a framework it is actually just a useful class library, it in no way restricts what you can do with C++. C# on the other hand, well its not even a proper compiled language, you MUST go through the framework and its intermediate language.

    #62672
    Participant
    Ed P
    @edps
    Forumite Points: 17,429

    In other words MFC is a C++ class library or wrapper for some of the Win API components. The C#  Win API DLL calls are at a slightly lower level and would need some verbose coding to make them functionally equivalent to MFC. Like C++ it is an entry point for writing the ‘unsafe’ code that is often needed when handling graphics.

    You are not completely accurate in saying that C# is not compiled. As in an analogous fashion to C++, C# is compiled into MSIL rather than p-code.

    Compiled is probably a misnomer in both cases as the end result of the ‘compilation’ is not real CPU level machine code, but an intermediate managed ‘safe’ code that runs on a software machine. Compiled Python is similarly not really compiled.

    #62675
    Participant
    Wheels-Of-Fire
    @grahamdearsley
    Forumite Points: 6,522

    C++ does not use P code and it does not require any sort of virtual machine, unlike java.

    The closest C++ comes to Pseudo code is its .OBJ files, these are compilation units that are used by the linker as part of the C++ separate compilation feature.

    When you are in debug mode the compiler will generate an OBJ file for every source file in your project. If you change the code in any source file then you only need to recompile the code in that file and the linker will use the other existing OBJ files to produce an EXE.

    When you do a release build C++ will compile a new .EXE direct from source.

    #62677
    Participant
    Wheels-Of-Fire
    @grahamdearsley
    Forumite Points: 6,522

    Visual Studio also provides C++/CLI which uses the Common Language Infrastructure. This IS a form of framework that produces managed code and it enables C++ to interface with C# code.

    #62679
    Participant
    Wheels-Of-Fire
    @grahamdearsley
    Forumite Points: 6,522

    I do wish someone would write a decent book on Visual Studio and the version of Visual C++ that is supplied with it. The Microsoft Docs are scattered all over the place and i’m picking things up piecemeal as I go along 🤨

    #63007
    Participant
    Wheels-Of-Fire
    @grahamdearsley
    Forumite Points: 6,522

    Been doing a bit more Re-reading and it turns out that in C++ a struct  is almost exactly the same as a class. The ONLY difference is that class members are private by default and struct members are public but you can change that by inserting public or private in your class or struct definition.

     

    #63009
    Participant
    Wheels-Of-Fire
    @grahamdearsley
    Forumite Points: 6,522

    And the bloody compiler is always right,if a member is private then it IS 😆

    #63157
    Participant
    Wheels-Of-Fire
    @grahamdearsley
    Forumite Points: 6,522

    I have found something else I don’t like about the C++ Primer book but its not really a fault.

    Up until now I have been typing in the examples and code snippets to gain practice with the syntax but they are getting a bit long now so I decided to go to the books website and download the sample code from there.

    The problem I have is that, like every other C++ book I know of, they are determined to be as standards compliant and device independent as possible which means that they mostly concentrate on using the ISO compliant GCC Compiler kit. This Compiler is command line only and the code examples assume you will be using a command line compiler.

    There are separate downloads for Linux with GCC and for Visual Studio, pre and post VS12 but they assume you will be using the Visual C++ command line.

    The code samples are divided into chapters but all the .cpp and .h files are in the same folder along with a MAKE file and its template. If you use the NMAKE.EXE Utility from the command prompt then it will use the Make file to build seperate .EXE files for all the sample code, supposedly, but I am using the GUI, NOT the command prompt.

    In the GUI there is an option to create a new project from existing source files, you give it the path to your source files and a name for your new project and hit OK, job done, all set for compilation. The trouble is that the source code for all the examples in a chapter is in the same folder so the build project wizard tries to include ALL of them in the new project. The result is not useful !

    To make things work I have to make new folders for each code sample and copy only the files it needs into it. This is not quite as easy as it sounds because different .cpp files require different .h files and some of them share the same one. I either have to go through the source code to see what #include’s there are or just try a compile to see what errors come up.

    This is just a niggle but they could have made life much simpler by putting the code in separate folders in the first place, each with its own Make file if they wanted.

    #63159
    Participant
    Ed P
    @edps
    Forumite Points: 17,429

    I forgot to correct your assertion that C++ managed compiled bytecode is not analogous to p-code. Microsoft does not call it p-code they call it bytecode, but it is in fact managed by the Common Language Runtime. See M$ article

    This M$ document explains how it works. In my view the principle of using the M$ CLR is not a lot different from the Java or Python runtime machines both of which run in a managed environment. It isn’t old school bare metal C++ unmanaged code that when compiled runs directly on the CPU.

    “The Managed Code running under a Common Language Runtime cannot be accessed outside the runtime environment as well as cannot call directly from outside the runtime environment.”

Viewing 20 replies - 101 through 120 (of 180 total)
  • You must be logged in to reply to this topic.