Tải bản đầy đủ - 0 (trang)
A.1 Borland C++ Builder and Kylix

A.1 Borland C++ Builder and Kylix

Tải bản đầy đủ - 0trang

class.Forexample,__declspec(noreturn)isafunction

specifierthattellsthecompilerthatthefunctiondoesnot

return,whichpermitsadditionaloptimizationanderrorchecking(forexample,eliminatingstatementsthatfollowa

calltothenoreturnfunction):

void__declspec(noreturn)abort();

Otherattributesinclude:

thread

Astorage-classspecifierthatdeclaresanobjecttobe

localtoathread;thatis,eachruntimethreadhasa

separatecopyoftheobject.

dllexport

Afunctionspecifierthattellsthelinkertoexportthe

functionnamefromadynamic-linklibrary(DLL).

uuid(string-literal)

Modifiesaclassdeclaration.Itassociatesauniversally

uniqueidentifier(UUID)withtheclass,whichis

requiredforimplementingCOMobjectsinWindows.A

class'sUUIDcanberetrievedwiththe__uuidof

operator.

__int64

The__int64typeisa64-bitintegertype.Incurrent

releasesofC++BuilderandKylix,longis32bits.A64-bit

integerliteraliswrittenwithasuffixofi64(e.g.,

10000000000000i64).

__property

Apropertyisaclassmemberthatisusedlikeadata



member,butitcanhavethesemanticsofamember

function.PropertiesarethefoundationfortheRADfeatures

ofC++BuilderandKylix.Apropertyisassociatedwitha

readerandwriter,whichcanbedatamembernamesor

memberfunctionnames:

classTControl{

private:

intheight_;

voidset_height(inth);

...

__published:



__propertyintheight{read=height_,write=set_height}

};

TControl*ctl=newTControl;

ctl->height=10;//Callsctl->set_height(10)

inth=ctl->height;//Getsctl->height_

__published

The__publishedaccessspecifierlabelyieldsthesame

accessibilityasthepublickeyword,butitalsodirectsthe

compilertostoreadditionalruntimetypeinformation(RTTI)

forthepublisheddeclarations.TheRADfeaturesusethe

RTTIwhentheuserdesignsanapplication

__thread



The__threadkeywordisasynonymfor_

_declspec(thread).

__uuidof

The__uuidofoperatortakesanexpressionasanoperand

andreturnstheUUIDoftheexpression'sclass.Theclass

declaresitsUUIDwith__declspec(uuid(...)).Aclass

canimplementthestandardCOMmemberfunction,

QueryInterface,with__uuidof:

classdemo{



virtualHRESULTQueryInterface(constUUID&iid,void**obj

{

if(iid==__uuidof(IUnknown)){

*obj=reinterpret_cast(this);

static_cast(*obj)->AddRef();

returnS_OK;

}

returnE_NOINTERFACE;

}

};



A.2GNUCompilerCollection

TheGNUC++compilerhasmanyextensionstothestandard.

Themostwidespreadversion,2.95,ismature,stable,butvery

muchoutdatedwithregardtotheC++standard.Thenew3.x

versionhewsmuchclosertothestandard,whileretainingthe

familiarGNUextensions.Thissectionpresentshighlightsofonly

afewoftheextensions.

__attribute__

Afunctioncanbemodifiedwithattributestotellthe

compileraboutthefunction.Forexample,__attribute_

_((noreturn))tellsthecompilerthatthefunctiondoesnot

return,whichenablesthecompilertoremoveunneeded

code,suchasstatementsthatfollowcallstothefunction.

Someattributescanapplytoobjects,labels,andtypes.

Severaldifferentattributesaresupported,suchas:

always_inline

Alwaysexpandthisfunctioninline,evenifoptimizations

aredisabled.

const

Thefunctionhasnosideeffectsanddoesnotdependon

anyglobalvariables;therefore,thecompilercan

replacerepeatedcallstothefunctionwithasinglecall,

savingtheresult.

deprecated

Thefunction,object,ortypeisdeprecated.Usinga

deprecatedentityresultsinacompilerwarning.

dllexport



OnWindows,marksthefunctionasexportedfroma

DLL.

pure

Slightlylessstrongthanconst,apurefunctionhasno

sideeffectsbutcandependonglobalvariables.The

compilercanoptimizeawayrepeatedcallswhenit

knowsinterveningcodedoesnotmodifyanyglobal

variables.

caserange

Inaswitchstatement,asinglecaselabelcanspecifya

rangeofvalues:

switch(c){

case'a'...'z':case'A'...'Z':

do_english_letter(c);

break;

//...Othercases,etc.

}

longlong

Thelonglongtypeisanintegraltypethathasatleast64

bits.AlonglongliteraliswrittenwithasuffixofLL(e.g.,

10000000000000LL).

Minimumandmaximumoperators

Theoperators?returntheminimumandmaximum

oftheirtwooperands.Youcanoverloadtheseoperatorsthe



wayyouwouldanyotheroperator:

template

Tmax3(constT&a,constT&b,constT&c)

{

returna>?b>?c;

}

typeof

Thetypeofkeywordtakesanexpressionasanoperandand

yieldsthetypeoftheexpression.Youcanusetypeof

whereveryoucanuseatypeidentifier.Templatescreatea

needfortypeofbecausewhenwritingthetemplate,you

havenowayofknowingthereturntypeofanoperatoror

function.Forexample:

template

typeof(T+U)incr(constT&t,constU&u)

{

returnt+u;

}



A.3MicrosoftVisualC++

Microsoft'slatestC++compilerhasasplitpersonality.Itcan

generateaconventionalprogramfollowingthestandard(more

orless),oritcanproduce.NEToutputusingamodified

languagecalledManagedC++,whichrestrictssomefeatures

andaddsothers.Thefollowingaresomehighlightsofthe

ManagedC++extensions:

__box

The__boxoperatortakesavalueobjectasanargument

andreturnsamanaged(__gc)objectthatwrapsthevalue

inamanaged"box."Youcanalsodeclareapointertoa

valuetypewiththe__boxspecifiersothepointercan

storeboxedvalues.Thecompilertreatsboxedvalues

speciallyandletsyouaccessthemembersofthevalue

transparently,asthoughtheboxwerenotpresent.

Nonetheless,theboxmanagesthelifetimeofthevalueit

contains.

__declspec

The__declspeckeywordtakesalistofattributesin

parenthesesandservesasadeclarationspecifier.

Dependingontheattributes,itcanbeusedtomodifya

function,object,orclass.SeeSectionA.1formore

information.

__gc

ThekeyfeatureofManagedC++isthatobjectsare

garbage-collected.Thismeansthelifetimeandmemoryare

managedautomaticallybytheruntimeenvironment.As

longastheobjectisinuse,itremainsalive.Whenthe

objectisnolongerusedanywhereintheprogram,itisup



forreclamation.

Declareaclassusingthe__gcspecifiertomarktheclass

asmanaged.Amanagedclasshasnumerousrestrictions,

suchas:

Nomorethanonebaseclass(whichmustalsobe

managed)

Nounmanageddatamembers(exceptPODtypes)

Nofriends

Nooperatorneworoperatordeletemembers

Nouser-definedcopyconstructors

Noconstorvolatilequalifiedmemberfunctions

Managedobjectsarecreatedwiththeglobalnewoperator.

Theyarefreedautomaticallywhentheyarenolonger

needed.Iftheclasshasadestructor,youcaninvokethe

deleteoperator,whichcallsthedestructorbutdoesnot

freethememory.

__int64

The__int64typeisa64-bitintegertype.Inthecurrent

releasesofVisualC++,longis32bits.A64-bitinteger

literaliswrittenwithasuffixofi64(e.g.,

10000000000000i64).

__pin

Sometimes,amanagedapplicationmustcallanunmanaged

libraryorsystemcall.The__pinkeywordlocksamanaged

objectatitsaddressinmemoryandpreventsthegarbage

collectorfrommovingtheobject.Theaddressoftheobject

cansafelybepassedtotheunmanagedfunction.Whenthe



__pinpointergoesoutofscope,themanagedobjectisno

longerpinnedandcanbemovedorreclaimed.

__property

Apropertyisapseudo-memberthatisusedlikeadata

member,butitcanhavethesemanticsofamember

function.PropertiesarethefoundationoftheVisualStudio

RADfeatures.Apropertyisassociatedwithagetterand

setter,whichhavetheformsget_name()andset_name()

forthepropertynamedname:

__gcclassControl{

private:

intheight_;

public:

__propertyintget_height()const{returnheight_;}

__propertyvoidset_height(inth);

};

Control*ctl=newControl;

ctl->height=10;//Callsctl->set_height(10)

inth=ctl->height;//Callsctl->get_height()

__value

A__valueclassisintendedforsmallobjectswithshort

lifetimes,whichareallocatedontheruntimestack,notthe

managedheap(theway__gcobjectsaremanaged).A

managedclasscanhave__valuedatamembers,butnot



theotherwayaround.

#using

The#usingdirectiveissimilarto#include,exceptthe

includedobjectisnotaheaderorfilebuta.NETassembly,

whichcontainsalltheinformationthecompilerneedstouse

theclasses,types,andfunctionsthataredefinedinthe

assembly.



AppendixB.Projects

ThisappendixliststhreeinterestingC++projects.Ofcourse,

many,manymoreprojectsexist.Thesewerechosenbecause

theybestreflectthecapabilitiesorfuturedirectionsofC++.

Thisbook'swebsite(http://www.tempest-sw.com/cpp/)has

linkstotheseandotherprojects.



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

A.1 Borland C++ Builder and Kylix

Tải bản đầy đủ ngay(0 tr)

×