Movatterモバイル変換


[0]ホーム

URL:




Chapter 2: Introduction

This document offers an introduction to theC++ programming language. Itis a guide forC/C++ programming courses, yearly presented by Frank at theUniversity of Groningen. This document is not a completeC/C++ handbook,as much of theC-background ofC++ is not covered. Other sourcesshould be referred to for that (e.g., theon-line book suggested tome by George Danchev (danchev at spnet dot net)).

The reader should be forewarned that extensive knowledge of theCprogramming language is actually assumed. TheC++ Annotations continue wheretopics of theC programming language end, such as pointers, basic flowcontrol and the construction of functions.

Some elements of the language, like specific lexical tokens (likedigraphs (e.g.,<: for[, and>: for])) are not coveredby theC++ Annotations, as these tokens occur extremely seldom inC++ sourcecode. In addition,trigraphs (using??< for{, and??> for}) have been removed fromC++.

The working draft of theC++ standard is freely available, and can becloned from the git-repository athttps://gitlab.com/cplusplus/draft.git

The version number of theC++ Annotations (currently 13.02.02) is updated whenthe content of the document change. The first number is the major number, andis probably not going to change for some time: it indicates a majorrewriting. The middle number is increased when new information is added to thedocument. The last number only indicates small changes; it is increased when,e.g., series of typos are corrected.

This document is published by the Center of Information Technology,University of Groningen,the Netherlands under theGNU General Public License.

TheC++ Annotations were typeset using theyodlformatting system.

All correspondence concerning suggestions, additions, improvements or changes to this document should be directed to the author:

Frank B. Brokken
University of Groningen,
PO Box 407,
9700 AK Groningen
The Netherlands
(email: f.b.brokken@rug.nl)

In this chapter an overview ofC++'s defining features is presented. A fewextensions toC are reviewed and the concepts of object based and objectoriented programming (OOP) are briefly introduced.

2.1: What's new in the C++ Annotations

This section is modified when the first or second part of the version numberchanges (and occasionally also for the third field of the version number). Ata major version upgrade the entries of the previous major version are kept,and entries referring to older releases are removed.

2.2: C++'s history

The first implementation ofC++ was developed in the 1980s atthe AT&T Bell Labs, where the Unix operating system was created.

C++ was originally a `pre-compiler', similar to the preprocessor ofC,converting special constructions in its source code to plainC. Back thenthis code was compiled by a standardC compiler. The `pre-code', which wasread by theC++ pre-compiler, was usually located in a file with theextension.cc,.C or.cpp. This file would then be converted to aC source file with the extension.c, which was thereupon compiled andlinked.

The nomenclature ofC++ source files remains: the extensions.cc and.cpp are still used. However, the preliminary work of aC++pre-compiler is nowadays usually performed during the actual compilationprocess. Often compilers determine the language used in a source file from itsextension. This holds true for Borland's and Microsoft'sC++ compilers,which assume aC++ source for an extension.cpp. TheGNUcompilerg++, which is available on many Unix platforms, assumes forC++ the extension.cc.

The fact thatC++ used to be compiled intoC code is also visiblefrom the fact thatC++ is a superset ofC:C++ offers the fullC grammar and supports allC-library functions, and adds to thisfeatures of its own. This makes the transition fromC toC++ quite easy. Programmers familiar withC may start`programming inC++' by using source files having extensions.cc or.cpp instead of.c, and may then comfortably slip into all thepossibilities offered byC++. No abrupt change of habits is required.

2.2.1: History of the C++ Annotations

The original version of theC++ Annotations was written by Frank Brokkenand Karel Kubat in Dutch usingLaTeX. After some time, Karel rewrote thetext and converted the guide to a more suitable format and (of course) toEnglish in September 1994.

The first version of the guide appeared on the net in October 1994. By then itwas converted toSGML.

Gradually new chapters were added, and the content was modified and furtherimproved (thanks to countless readers who sent us their comments).

In major version four Frank added new chapters and converted the document fromSGML toyodl.

TheC++ Annotations are freely distributable. Be sure to read thelegal notes.

Reading the annotations beyond this point implies that you are aware of these notes and that you agree with them.

If you like this document, tell your friends about it. Even better, let usknow by sending email toFrank.

2.2.2: Compiling a C program using a C++ compiler

ProspectiveC++ programmers should realize thatC++ is not a perfectsuperset ofC. There are some differences you might encounter when yousimply rename a file to a file having the extension.cc and run it throughaC++ compiler:

2.2.3: Compiling a C++ program

To compile aC++ program, aC++compiler is required. Consideringthe free nature of this document, it won't come as a surprise that afree compiler is suggested here. TheFree Software Foundation (FSF) provides athttp://www.gnu.org a freeC++compiler which is, among other places, also part of theDebian (http://www.debian.org) distribution ofLinux (http://www.linux.org).

Always use the latestC++ standard supported by your compiler. When thelatest standard isn't used by default, but is already partially implemented itcan usually be selected by specifying the appropriate flag. E.g., to use theC++26 standard specify the flag--std=c++26.

Note: in theC++ Annotations it is assumed that the lastest available standardis specified using the--std flag, even if no--std flag has beenspecified.

2.2.3.1: C++ under MS-Windows

ForMS-WindowsCygwin(http://cygwin.com) orMinGW(http://mingw-w64.org/doku.php) provide the foundationfor installing theWindows port of theGNUg++compiler(see alsohttps://docs.microsoft.com/en-us/windows/wsl/about).

The GNUg++ compiler's official home page ishttp://gcc.gnu.org, also containinginformation about how to install the compiler in an MS-Windows system.

2.2.3.2: Compiling a C++ source text

Generally the following command can be used to compile aC++ source file`source.cc':
        g++ source.cc

This produces a binary program (a.out ora.exe). If the defaultname is inappropriate, the name of the executable can be specified using the-o flag (here producing the programsource):

        g++ -o source source.cc

If a mere compilation is required, the compiled module can be produced usingthe-c flag:

        g++ -c source.cc

This generates the filesource.o, which can later on be linked toother modules.

C++ programs quickly become too complex to maintain `by hand'. With allserious programming projects program maintenance tools are used. Usually thestandardmake program is used to maintainC++ programs, but goodalternatives exist, like theicmake orccbuild programmaintenance utilities.

It is strongly advised to start using maintenance utilities early in the studyofC++.

2.3: C++: advantages and claims

Often it is said that programming inC++ leads to `better' programs. Someof the claimed advantages ofC++ are: Which of these allegations are true? Originally, our impression was thattheC++ language was somewhat overrated; the same holding true for theentire object-oriented programming (OOP) approach. The enthusiasm for theC++ language resembles the once uttered allegations aboutArtificial-Intelligence (AI) languages likeLisp andProlog: theselanguages were supposed to solve the most difficult AI-problems `almostwithout effort'. New languages are often oversold: in the end, each problemcan be coded in any programming language (sayBASIC orassembly language). The advantages and disadvantages of a givenprogramming language aren't in `what you can do with them', but rather in`which tools the language offers to implement an efficient and understandablesolution to a programming problem'. Often these tools take the form ofsyntacticrestrictions, enforcing or promoting certain constructions orsimply suggesting intentions by applying or `embracing' such syntacticforms. Rather than a long list of plain assembly instructions we now use flowcontrol statements, functions, objects or even (withC++) so-calledtemplates to structure and organize code and to expressoneself `eloquently' in the language of one's choice.

Concerning the above allegations ofC++, we support the following, however.

C++ in particular (and OOP in general) is of course notthesolution to all programming problems. However, the languagedoes offervarious new and elegant facilities which are worth investigating. At thedownside, the level of grammatical complexity ofC++ has increasedsignificantly as compared toC. This may be considered a serious drawbackof the language. Although we got used to this increased level of complexityover time, the transition was neither fast nor painless.

With theC++ Annotations we hope to help the reader when transiting fromCtoC++ by focusing on the additions ofC++ as compared toC and byleaving out plainC. It is our hope that you like this document andmay benefit from it.

Enjoy and good luck on your journey intoC++!

2.4: What is Object-Oriented Programming?

Object-oriented (and object-based) programming propagates a slightly differentapproach to programming problems than the strategy usually used inCprograms. InC programming problems are usually solved using a`procedural approach': a problem is decomposed into subproblems and thisprocess is repeated until the subtasks can be coded. Thus a conglomerate offunctions is created, communicating through arguments and variables, global orlocal (orstatic).

In contrast (or maybe better: in addition) to this, an object-based approachidentifies thekeywords used in a problem statement. These keywords arethen depicted in a diagram where arrows are drawn between those keywords todepict an internal hierarchy. The keywords become the objects in theimplementation and the hierarchy defines the relationship between theseobjects. The term object is used here to describe a limited, well-definedstructure, containing all information about an entity: data types andfunctions to manipulate the data. As an example of anobject oriented approach, an illustration follows:

The employees and owner of a car dealer and auto garage company are paid as follows. First, mechanics who work in the garage are paid a certain sum each month. Second, the owner of the company receives a fixed amount each month. Third, there are car salesmen who work in the showroom and receive their salary each month plus a bonus per sold car. Finally, the company employs second-hand car purchasers who travel around; these employees receive their monthly salary, a bonus per bought car, and a restitution of their travel expenses.
When representing the above salary administration, the keywords could bemechanics, owner, salesmen and purchasers. The properties of such units are: amonthly salary, sometimes a bonus per purchase or sale, and sometimesrestitution of travel expenses. When analyzing the problem in this manner wearrive at the following representation: The hierarchy of the identified objects are further illustrated inFigure1.

Figure 1: Hierarchy of objects in the salary administration.

The overall process in the definition of a hierarchy such as the above startswith the description of the most simple type. Traditionally (and stillencountered with some popular object oriented languages) more complex typesare thereupon derived from the basic type, with each derived type adding somenew functionality. From these derived types, more complex types can again bederivedad infinitum, until a representation of the entire problem can bemade.

Over the years this approach has become less popular inC++ as ittypically results in very tightcoupling among those types, which inturnsreduces rather than enhances the understanding, maintainability andtestability of complex programs. The termcoupling refers to the degree ofindependence between software components: tight coupling means a strongdependency, which is frowned upon inC++. InC++ object orientedprograms more and more favor small, easy to understand hierarchies, limitedcoupling and a developmental process wheredesignpatterns (cf.Gamma et al. (1995)) play a central role.

InC++classes are frequently used to define thecharacteristics ofobjects. Classes contain the necessary functionality todo useful things. Classes generally do not offer all their functionality (andtypicallynone of their data) to objects of other classes. As we will see,classes tend tohide their properties in such a way that they are notdirectly modifiable by the outside world. Instead, dedicated functions areused to reach or modify the properties of objects. Thus class-type objects areable to uphold their own integrity. The core concept here isencapsulationof whichdata hiding is just an example. These concepts are furtherelaborated in chapter7.

2.5: Differences between C and C++

In this section some examples ofC++ code are shown. Some differencesbetweenC andC++ are highlighted.

2.5.1: The function `main'

InC++ there are only two variants of the functionmain:int main() andint main(int argc, char **argv).

Notes:

2.5.2: End-of-line comment

According to theANSI/ISO definition, `end of line comment' isimplemented in the syntax ofC++. This comment starts with// and endsat the end-of-line marker. The standardC comment, delimited by/* and*/ can still be used inC++:
    int main()    {        // this is end-of-line comment        // one comment per line        /*            this is standard-C comment, covering            multiple lines        */    }

Despite the example, it is advisednot to useC type commentinside the body ofC++ functions. Sometimes existing code must temporarilybe suppressed, e.g., for testing purposes. In those cases it's very practicalto be able to use standardC comment. If such suppressed code itselfcontains such comment, it would result in nested comment-lines, resulting incompiler errors. Therefore, therule of thumb is not to useC typecomment inside the body ofC++ functions (alternatively,#if 0 until#endif pair of preprocessor directives could of course also be used).

2.5.3: Strict type checking

C++ uses very stricttype checking. A prototype must be known for eachfunction before it is called, and the call must match the prototype.The program
    int main()    {        printf("Hello World\n");    }

often compiles underC, albeit with a warning thatprintf()is an unknown function. ButC++ compilers (should) fail to produce code insuch cases. The error is of course caused by the missing#include <stdio.h> (which inC++ is more commonly included as#include <cstdio> directive).

And while we're at it: as we've seen inC++mainalways usestheintreturn value. Although it is possible to defineintmain() without explicitly defining a return statement, withinmain it isnot possible to use areturn statement without an explicitint-expression. For example:

    int main()    {        return;     // won't compile: expects int expression, e.g.                    // return 1;    }

Implicit conversions fromvoid * to non-void pointers are notallowed. E.g., the following isn't accepted inC++:

    void *none()    {        return 0;    }        int main()    {        int *empty = none();    }

2.5.4: Function Overloading

InC++ it is possible to definefunctions having identical names but performing different actions. Thefunctions must differ in their parameter lists (and/or intheirconst attribute). An example is givenbelow:
    #include <stdio.h>    void show(int val)    {        printf("Integer: %d\n", val);    }    void show(double val)    {        printf("Double: %lf\n", val);    }    void show(char const *val)    {        printf("String: %s\n", val);    }    int main()    {        show(12);        show(3.1415);        show("Hello World!\n");    }
In the above program three functionsshow are defined, only differingin their parameter lists, expecting anint,double andchar *,respectively. The functions have identical names. Functions having identicalnames but different parameter lists are calledoverloaded. The act ofdefining such functions is called `function overloading'.

TheC++ compiler implements function overloading in a rather simpleway. Although the functions share their names (in this exampleshow), thecompiler (and hence the linker) use quite different names. The conversion of aname in the source file to an internally used name is called `name mangling'. E.g., theC++ compiler might convert the prototypevoidshow(int) to the internal nameVshowI, while ananalogous function having achar * argument might be calledVshowCP. The actual names that are used internally depend on the compilerand are not relevant for the programmer, except where these names show up ine.g., a listing of the content of a library.

Some additional remarks with respect to function overloading:

2.5.5: Default function arguments

InC++ it is possible to provide `default arguments' when defining afunction. These arguments are supplied by the compiler when they are notspecified by the programmer. For example:
    #include <stdio.h>    void showstring(char *str = "Hello World!\n");    int main()    {        showstring("Here's an explicit argument.\n");        showstring();           // in fact this says:                                // showstring("Hello World!\n");    }

The possibility to omit arguments in situations where default argumentsare defined is just a nice touch: it is the compiler who supplies the lackingargument unless it is explicitly specified at the call. The code of theprogram will neither be shorter nor more efficient when default arguments areused.

Functions may be defined with more than one default argument:

    void two_ints(int a = 1, int b = 4);    int main()    {        two_ints();            // arguments:  1, 4        two_ints(20);          // arguments: 20, 4        two_ints(20, 5);       // arguments: 20, 5    }

When the functiontwo_ints is called, the compiler supplies one or twoarguments whenever necessary. A statement liketwo_ints(,6) is, however,not allowed: when arguments are omitted they must be on the right-hand side.

Default arguments must be known atcompile-time since at that momentarguments are supplied to functions. Therefore, the default arguments must bementioned at the function'sdeclaration, rather than at itsimplementation:

    // sample header file    void two_ints(int a = 1, int b = 4);    // code of function in, say, two.cc    void two_ints(int a, int b)    {        ...    }

It is an error to supply default arguments in both function definitions andfunction declarations. When applicable default arguments should be provided infunction declarations: when the function is used by other sources the compilercommonly reads the header file rather than the function definitionitself. Consequently the compiler has no way to determine the values ofdefault arguments if they are provided in the function definition.

2.5.6: NULL-pointers vs. 0-pointers and nullptr

InC++ all zero values are coded as0. InCNULL is often usedin the context of pointers. This difference is purely stylistic, though onethat is widely adopted. InC++NULL should be avoided (as it is amacro, and macros can --and therefore should-- easily be avoided inC++, see also section8.1.4). Instead0 can almost always beused.

Almost always, but not always. AsC++ allows function overloading(cf. section2.5.4) the programmer might be confronted withan unexpected function selection in the situation shown in section2.5.4:

    #include <stdio.h>    void show(int val)    {        printf("Integer: %d\n", val);    }    void show(double val)    {        printf("Double: %lf\n", val);    }    void show(char const *val)    {        printf("String: %s\n", val);    }    int main()    {        show(12);        show(3.1415);        show("Hello World!\n");    }

In this situation a programmer intending to callshow(char const *) mightcallshow(0). But this doesn't work, as 0 is interpreted asint and soshow(int) is called. But callingshow(NULL) doesn't work either, asC++ usually definesNULL as 0, rather than((void *)0). So,show(int) is called once again. To solve these kinds of problems the newC++ standard introduces the keywordnullptr representing the 0pointer. In the current example the programmer should callshow(nullptr)to avoid the selection of the wrong function. Thenullptr value can alsobe used to initialize pointer variables. E.g.,

    int *ip = nullptr;      // OK    int value = nullptr;    // error: value is no pointer

2.5.7: The `void' parameter list

InC, a function prototype with anempty parameter list, such as
    void func();

means that the argument list of the declared function is not prototyped: forfunctions using this prototype the compiler does not warn against callingfunc with any set of arguments. InC the keywordvoid is used whenit is the explicit intent to declare a function with no arguments at all, asin:

    void func(void);

AsC++ enforces strict type checking, inC++ an empty parameterlist indicates thetotal absence of parameters. The keywordvoid isthus omitted.

2.5.8: The `#define __cplusplus'

EachC++ compiler which conforms to theANSI/ISO standard defines thesymbol__cplusplus: it is as if each source file were prefixed with thepreprocessor directive#define __cplusplus.

We shall see examples of the usage of this symbol in the following sections.

2.5.9: Using standard C functions

NormalC functions, e.g., which are compiled and collected in a run-timelibrary, can also be used inC++ programs. Such functions, however, must bedeclared asC functions.

As an example, the following code fragment declares a functionxmallocas aC function:

    extern "C" void *xmalloc(int size);

This declaration is analogous to a declaration inC, except that theprototype is prefixed withextern "C".

A slightly different way to declareC functions is the following:

    extern "C"    {        // C-declarations go in here    }

It is also possible to place preprocessor directives at the location ofthe declarations. E.g., aC header filemyheader.h which declaresC functions can be included in aC++ source file as follows:

    extern "C"    {        #include <myheader.h>    }

Although these two approaches may be used, they are actually seldomencountered inC++ sources. A more frequently used method to declareexternalC functions is encountered in the next section.

2.5.10: Header files for both C and C++

The combination of the predefined symbol__cplusplus and thepossibility to defineextern "C" functions offers the ability tocreate header files for bothC andC++. Such a header file might,e.g., declare a group of functions which are to be used in bothC andC++ programs.

The setup of such a header file is as follows:

    #ifdef __cplusplus    extern "C"    {    #endif        /* declaration of C-data and functions are inserted here. E.g., */        void *xmalloc(int size);    #ifdef __cplusplus    }    #endif

Using this setup, a normalC header file is enclosed byextern "C"{ which occurs near the top of the file and by}, which occurs nearthe bottom of the file. The#ifdef directives test for the type of thecompilation:C orC++. The `standard'C header files, such asstdio.h, are built in this manner and are therefore usable for bothCandC++.

In additionC++ headers should supportinclude guards.InC++ it is usually undesirable to include the same header file twice inthe same source file. Such multiple inclusions can easily be avoided byincluding an#ifndef directive in the header file. For example:

    #ifndef MYHEADER_H_    #define MYHEADER_H_        // declarations of the header file is inserted here,        // using #ifdef __cplusplus etc. directives    #endif

When this file is initially scanned by the preprocessor, the symbolMYHEADER_H_ is not yet defined. The#ifndef condition succeeds and alldeclarations are scanned. In addition, the symbolMYHEADER_H_ is defined.

When this file is scanned next while compiling the same source file,the symbolMYHEADER_H_ has been defined and consequently all informationbetween the#ifndef and#endif directives is skipped by the compiler.

In this context the symbol nameMYHEADER_H_ serves only forrecognition purposes. E.g., the name of the header file can be used for thispurpose, in capitals, with an underscore character instead of a dot.

Apart from all this, the custom has evolved to giveC header files theextension.h, and to giveC++ header filesno extension. Forexample, the standardiostreamscin, cout andcerr are availableafter including the header fileiostream, ratherthaniostream.h. In the Annotations this convention is usedwith the standardC++ header files, but not necessarily everywhere else.

There is more to be said about header files. Section7.11 providesan in-depth discussion of the preferred organization ofC++ header files.In addition, starting with theC++26 standardmodules are availableresulting in a somewhat more efficient way of handling declarations thanoffered by the traditional header files.

Currently, theC++ Annotations very briefly coversmodules (cf. section25).

2.5.11: Defining local variables

Although already available in theC programming language,local variables should only be defined once they're needed. Althoughdoing so requires a little getting used to, eventually it tends to producemore readable, maintainable and often more efficient code than definingvariables at the beginning of compound statements. We suggest to apply thefollowing rules of thumb when defining local variables:

If considered appropriate,nested blocks can be used to localizeauxiliary variables. However, situations exist where local variables areconsidered appropriate inside nested statements. The just mentionedforstatement is of course a case in point, but local variables can also bedefined within the condition clauses ofif-else statements, withinselection clauses ofswitch statements and condition clauses ofwhilestatements. Variables thus defined are available to the fullstatement, including its nested statements. For example, consider thefollowingswitch statement:

    #include <stdio.h>    int main()    {        switch (int c = getchar())        {            case 'a':            case 'e':            case 'i':            case 'o':            case 'u':                printf("Saw vowel %c\n", c);            break;            case EOF:                printf("Saw EOF\n");            break;            case '0' ... '9':                printf("Saw number character %c\n", c);            break;            default:                printf("Saw other character, hex value 0x%2x\n", c);        }    }
Note the location of the definition of the character `c': it isdefined in the expression part of theswitch statement. This impliesthat `c' is availableonly to theswitch statement itself,including its nested (sub)statements, but not outside the scope of theswitch.

The same approach can be used withif andwhile statements: avariable that is defined in the condition clause of anif andwhilestatement is available in their nested statements. There are some caveats,though:

The former point of attention should come as no big surprise: in order tobe able to evaluate the logical condition of anif orwhile statement,the value of the variable must be interpretable as either zero (false) ornon-zero (true). Usually this is no problem, but inC++objects (likeobjects of the typestd::string (cf. chapter5)) are oftenreturned by functions. Such objects may or may not be interpretable as numericvalues. If not (as is the case withstd::string objects), then suchvariables cannot be defined at the condition or expression clauses ofcondition- or repetition statements. The following example will thereforenot compile:
    if (std::string myString = getString())     // assume getString returns    {                                           // a std::string value        // process myString    }

The above example requires additional clarification. Often a variable canprofitably be given local scope, but an extra check is required immediatelyfollowing its initialization. The initializationand the test cannotboth be combined in one expression. Insteadtwo nested statements arerequired. Consequently, the following example won't compile either:

    if ((int c = getchar()) && strchr("aeiou", c))        printf("Saw a vowel\n");

If such a situation occurs, either use two nestedif statements, orlocalize the definition ofint c using a nested compound statement:

    if (int c = getchar())             // nested if-statements        if (strchr("aeiou", c))            printf("Saw a vowel\n");    {                                  // nested compound statement        int c = getchar();        if (c && strchr("aeiou", c))           printf("Saw a vowel\n");    }

TheC++26 standard introducedname-independent declarations. They'recovered in section ??.

2.5.12: The keyword `typedef'

The keywordtypedef is still used inC++, but is not requiredanymore when definingunion,struct orenum definitions.This is illustrated in the following example:
    struct SomeStruct    {        int     a;        double  d;        char    string[80];    };

When astruct, union or other compound type is defined, the tag ofthis type can be used as type name (this isSomeStruct in the aboveexample):

    SomeStruct what;    what.d = 3.1415;

2.5.13: Functions as part of a struct

InC++ we may definefunctions as members of structs. Here weencounter the first concrete example of an object: as previously described(see section2.4), anobject is a structure containing data whilespecialized functions exist to manipulate those data.

A definition of astruct Point is provided by the code fragment below.In this structure, twoint data fields and one functiondraw aredeclared.

    struct Point            // definition of a screen-dot    {        int x;              // coordinates        int y;              // x/y        void draw();        // drawing function    };

A similar structure could be part of a painting program and could, e.g.,represent a pixel. With respect to thisstruct it should be noted that:

ThePoint structure could be used as follows:
    Point a;                // two points on    Point b;                // the screen    a.x = 0;                // define first dot    a.y = 10;               // and draw it    a.draw();    b = a;                  // copy a to b    b.y = 20;               // redefine y-coord    b.draw();               // and draw it

As shown in the above example a function that is part of the structure maybe selected using the dot (.) (the arrow (->) operator is used whenpointers to objects are available). This is therefore identical to the waydata fields of structures are selected.

The idea behind this syntactic construction is that several types maycontainfunctions having identical names. E.g., a structure representing acircle might contain threeint values: two values for the coordinates ofthe center of the circle and one value for the radius. Analogously to thePoint structure, aCircle may now have a functiondraw to draw thecircle.

2.5.14: Evaluation order of operands

Traditionally, the evaluation order of expressions of operands of binaryoperators is, except for the boolean operatorsand andor, notdefined. C++ changed this for postfix expressions, assignment expressions(including compound assignments), and shift operators:

In the following examplesfirst is evaluated beforesecond, beforethird, beforefourth, whether they are single variables, parenthesizedexpressions, or function calls:

    first.second    fourth += third = second += first    first << second << third << fourth    first >> second >> third >> fourth

In addition, when overloading an operator, the function implementing theoverloaded operator is evaluated like the built-in operator it overloads, andnot in the way function calls are generally ordered.




[8]ページ先頭

©2009-2025 Movatter.jp