Forumite

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 16 replies - 101 through 116 (of 116 total)
  • Author
    Replies
  • #62569
    Participant
    Wheels-Of-Fire
    @grahamdearsley
    Forumite Points: 6,132

    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,132

    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,132

    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: 16,489

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

    link

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

    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,132

    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,132

    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: 16,489

    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,132

    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: 16,489

    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,132

    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,132

    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: 16,489

    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,132

    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,132

    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,132

    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 🤨

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