Forumite Members › General Topics › Tech › Makers & Builders › Programming/Code tips › C++ Programming
- This topic has 258 replies, 5 voices, and was last updated 2 years, 12 months ago by
Wheels-Of-Fire.
-
AuthorPosts
-
October 12, 2020 at 12:19 pm #62559
There are 2 ways of escaping ring 3 and getting into ring 0, software interrupt and syscall, and Windows traps them all.
October 12, 2020 at 6:12 pm #62569Another “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 😆
October 12, 2020 at 6:30 pm #62571In 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 😁
October 15, 2020 at 7:33 pm #62598I 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 ?
October 16, 2020 at 7:46 am #62602October 16, 2020 at 11:40 am #62613Ah 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 !
October 16, 2020 at 11:53 am #62615I also see that the author of the article isn’t keen on the stupid operator overloading of the & symbol either 😁
October 16, 2020 at 7:10 pm #62625My 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.
October 16, 2020 at 7:57 pm #62627If I remember correctly, parameters CAN be changed in a function but arguments cannot. Iirc you use const to stop a parameter being changed.
October 16, 2020 at 9:25 pm #62632I 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.
October 17, 2020 at 9:24 am #62637You 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.
October 17, 2020 at 10:38 am #62644https://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.
October 17, 2020 at 10:39 am #62646https://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.
October 17, 2020 at 9:20 pm #62672In 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.
October 18, 2020 at 11:15 am #62675C++ 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.
October 18, 2020 at 11:28 am #62677Visual 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.
October 18, 2020 at 11:42 am #62679I 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 🤨
October 26, 2020 at 11:53 am #63007Been 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.
October 26, 2020 at 12:14 pm #63009And the bloody compiler is always right,if a member is private then it IS 😆
November 2, 2020 at 4:52 pm #63157I 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.
-
AuthorPosts
- You must be logged in to reply to this topic.
