Path: csiph.com!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Bo Persson Newsgroups: comp.lang.c++ Subject: Re: Differences between C and C++ Date: Thu, 28 Jul 2022 15:04:12 +0200 Lines: 42 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: individual.net ZeuHuiQ5MWwoT1w2WKocqA2fGOFuyLUM1I7mlTUVy6ZmBshKGM Cancel-Lock: sha1:rVD7aQ1ht4I4bmvSALmW+/q5kCk= User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Thunderbird/91.11.0 Content-Language: sv In-Reply-To: Xref: csiph.com comp.lang.c++:85719 On 2022-07-28 at 13:33, Richard Damon wrote: > On 7/28/22 2:53 AM, Juha Nieminen wrote: >> Paul N wrote: >>> For example, in C, a = b + c is a straight-forward addition, though >>> depending on the types it might be a floating-point addition or it >>> might involve a hidden multiplication by a pointer size. In C++, a = >>> b + c could be anything - for instance, it might concatenate two >>> files to form a third one, involving thousands of disk accesses. >> >> If the types of a, b and c are the same as in the C code, then it cannot >> "be anything". I'm not aware of operator overloading being possible for >> basic types in C++. >> >> As you say, even in C, if you don't know what the types are, you don't >> know what operation that is actually doing. Could be pointer arithmetic >> for all we know. Knowing the types is kind of crucial to understand >> what's being done there. > > The difference is that in C, if you see in the code a + b, then you KNOW > that a and b need to be primitive types and the operation will be just a > couple of operations long (the WORSE it can be is a floating point > addition). > > In C++, we don't have that knowledge, and we need to think about what > the types actually are and what that means for the code. Yes, but if I have std::string full_name = first_name + last_name; I wouldn't expect a floating point addition. > > C allows you to more lightly skim code to see what it might be > happening, C++ requires you to know a bit more about the code, but lets > the code express higher level operations compactly. The abstraction part is that you shouldn't *have* to consider what happens down to the hardware level - not right now. Rather you should trust that the next level - which you wrote yesterday - works just as well as if you were to write it now.