tag 标签: function

相关博文
  • 热度 34
    2014-6-27 16:02
    10334 次阅读|
    1 个评论
    IAR,CC2530的工程中,自己添加函数报错。 Error: function "~~~" has no prototype 在工程OPtion-C/C++ Compiler-Language中 去掉 Require prototype 选项,重新编译。
  • 热度 19
    2013-1-15 19:27
    3463 次阅读|
    0 个评论
    Last year, I wrote a number of articles on how virtual functions behave in C++ and how you can obtain similar behaviour in C. In short, you can emulate a polymorphic C++ class (a class with at least one virtual function) as a C structure that has an additional member commonly called a vptr (VEE-pointer). The vptr points to a table of function pointers called a vtbl (VEE-table). Last month, I introduced the concept of pure virtual functions in C++. This month, I'll show how compilers typically implement pure virtual functions by showing how you can achieve similar behaviour in C. As in my prior articles, my sample classes represent an assortment of two-dimensional geometric shapes such as circle, rectangle, and triangle, all derived from a common base class called shape. The C++ definition for the shape base class looks in part like: class shape { public: shape(color o, color f); // constructor virtual double area() const = 0; virtual double perimeter() const = 0; ~~~ private: color outline, fill; }; The characters = 0 appearing at the end of the declarations for area and perimeter indicates that they are pure virtual functions. They need not be defined. As I asked at the end of my previous column, if shape's area and perimeter functions are undefined, what happens when you try to call them? Pure virtual function virtually impossible C++ makes calling a pure virtual function nearly impossible. A class with at least one pure virtual function is called an abstract class . C++ doesn't let you create an abstract class object except as the base class subobject of some derived class object. For example, these declarations won't compile: shape s; // compile error shape x ; // compile error You can declare a pointer to a shape, as in: shape *ps; // OK However, the compiler will reject a new-expression that tries to construct an abstract base class object, as in: ps = new shape; // compiler error Again, any class with at least one pure virtual function is an abstract class. If you can't create any such objects, then you don't have to worry that you might call a pure virtual function. Unfortunately, C++ compilers have trouble detecting all possible calls to pure virtual functions, such as when the constructor of an abstract class indirectly calls a pure virtual function of the same class. Thus, most compilers provide another line of defence. More on this shortly. The definition for our circle class derived from shape looks like: class circle: public shape { public: circle(double r, color o, color f); // constructor virtual double area() const; virtual double perimeter() const; ~~~ private: double radius; }; If you don't declare the area and perimeter functions in circle, then circle will inherit them as pure virtual functions, and circle will be an abstract class. You won't be able to create any circle objects. However, the class definition above does declare area and perimeter, so they're not pure virtual and circle isn't an abstract class. Now you're on the hook to define circle's area and perimeter functions. Inasmuch as circle is not an abstract class, you can create circle objects, as in: circle c (3); or: shape *ps = new circle (4); If the compiler could detect and prevent all calls to pure virtual functions, implementing pure virtual functions would be trivial—simply initialise the vtbl entry for each pure virtual function as a null pointer. In C, the code to define the vtbl for the shape base class would look something like: typedef struct shape_vtbl shape_vtbl; struct shape_vtbl { double (*area)(shape const *); double (*perimeter)(shape const *); }; shape_vtbl the_shape_vtbl = { 0, // 0 converted to null pointer 0 // ditto }; A second line of defence Unfortunately, compilers may fail to catch some pure virtual function calls. Such failures are rare, but nevertheless possible. As a second line of defence, if a pure virtual function has no definition, most compilers generate a definition for that function that makes a stink if executed. The C code to do this for the shape class might look like: double shape_area(shape const *s) { pure_virtual_alert(); return 0; } double shape_perimeter(shape const *s) { pure_virtual_alert(); return 0; } ~~~ shape_vtbl the_shape_vtbl = { shape_area, shape_perimeter }; On a desktop system, the pure_virtual_alert function might simply write a message to the system's stderr stream or display a message box, and then terminate the program. For an embedded system, you would have to tailor the response to be something more appropriate. Endnotes: Saks, Dan, "Implementing virtual functions in C," Eetindia.co.in, August 14, 2012. www.embedded.com/4391967. Saks, Dan, "The concept of pure virtual functions," Eetindia.co.in, December 20, 2012. www.embedded.com/4403313.  
  • 热度 28
    2012-4-7 11:12
    2384 次阅读|
    0 个评论
    Over the past few years, I've written several articles about representing and manipulating memory-mapped devices in C and C++. Some readers commented that the C++ techniques I presented were lacking in that they failed to use more advanced features such as inheritance and virtual functions. Classes with virtual functions can be very useful, but they aren't the solution to every problem. Classes that represent memory-mapped devices, such as the ones I've presented, work with real hardware specifically because they don't use virtual functions. Last month (" Discriminated unions ," March 2012), I looked at the sort of problem that virtual functions are good at solving. I described a typical C solution using discriminated unions and discussed the limitations of that approach. This month, I'll explain how to use virtual functions in C++ to solve the same problem. In the not-too-distant future, I'll also show you how C++ typically implements virtual functions by showing how you can emulate them in C. Although my primary focus here is virtual functions, of necessity I'll discuss class derivation as well. However, I'll cover only those aspects of derivation that I need to explain virtual functions. Deriving a class The problem I presented last month involved representing a collection of two-dimensional geometric shapes such as circles, rectangles, and triangles. Each shape object contains some linear or angular distances sufficient to characterise the physical extent of the shape. For example, a circle has a radius, a rectangle has a height and a width, and a triangle has two sides and an angle. Each shape also had common attributes, such as position (planar coordinates), or outline and fill colours. In C++, you can represent the various shapes as a collection of classes. You start by defining a shape class which captures the properties common to all shapes: class shape { public: shape(); // constructor double area() const; double perimeter() const; ~~~ private: coordinates position; colour outline, fill; }; The class declares several functions as public members, including area and perimeter. The intent is that every shape—whether it's a circle, rectangle, triangle, or anything else—will provide these functions. In effect, class shape defines a common interface shared by every specific type of shape. The shape class also declares data members position, outline, and fill. The intent is that every shape object—no matter what kind of shape it is—will contain storage for these members. Now you can define classes for each specific shape by deriving them from class shape. For example, you can define the circle class as: class circle: public shape { public: circle(double r); // constructor double area() const; double perimeter() const; ~~~ private: double radius; }; In the class definition above, the class heading: class circle: public shape specifies that class circle is derived from class shape. The "derived from" class is commonly called the "base" class. The keyword public in the class heading indicates that the derivation is public. C++ also permits private and protected derivation, but I'm going to ignore those variants here, and make the simplifying assumption that all derivation is public. It's by far the most common usage. A derived class inherits all the data members of its base class. That is, the data members declared in the base class occupy storage in the derived class as well. Members declared private in the base class won't be accessible in the derived class, but they will still occupy storage in derived class objects. For example, the base class shape has three data members, declared as: coordinates position; colour outline, fill; The derived class circle declares one more data member: double radius; Thus, the data storage for a circle object is essentially the same as that of a structure defined as: struct circle { coordinates position; colour outline, fill; double radius; }; A derived class also inherits member functions from its base class. However, special member functions such as constructors, destructors, and copy assignment operators aren't inherited. If the circle class didn't declare area and perimeter as member functions, circle would inherit the functions exactly as they're defined in the shape base class. For example, I have yet to show the definition for shape's area functions, but whatever it is, it can't be right for circle. The problem is that a circle's area depends on its radius, but the radius data member is defined in class circle, not in class shape. Although a derived class may have access to data members inherited from a base class, a base class normally can't see the members declared in its derived classes. Thus, the shape class doesn't know that a circle has a radius. The definition for the circle's area function is very simple: double circle::area() const { return pi * radius * radius; } where pi is presumably a previously-defined constant representing Π. The keyword const appearing after the function parameter list indicates that area function treats each circle as a constant object. That is, computing the area of a circle doesn't alter the circle. A hierarchy of shapes You can easily create derived classes for additional shapes. For example, here are class definitions for rectangle: class rectangle: public shape { public: rectangle(double h, double w); double area() const; double perimeter() const; ~~~ private: double height, width; }; and for triangle: class triangle: public shape { public: triangle(double s1, double s2, double a); double area() const; double perimeter() const; ~~~ private: double side1, side2, angle; }; All of these shapes can be viewed as a hierarchy, typically drawn with the base class at the top, shown in Figure 1.   The base class doesn't know that it has any derived classes. Each derived class knows of its base class, but doesn't know about any of the other derived classes.  
相关资源
  • 所需E币: 3
    时间: 2023-5-16 21:37
    大小: 290.9KB
    上传者: 20221231
    AIM&THURLBYTHANDARINSTRUMENTS
  • 所需E币: 1
    时间: 2022-7-23 19:22
    大小: 2.06KB
    上传者: Argent
    CfunctionforModbusCRCchecksum
  • 所需E币: 1
    时间: 2022-7-23 18:06
    大小: 4.06KB
    上传者: Argent
    FBDaylightsavingsfunctionblock
  • 所需E币: 1
    时间: 2022-7-23 18:04
    大小: 1.46MB
    上传者: Argent
    CX-ProgrammerVer.5IntroductiontoFunction
  • 所需E币: 1
    时间: 2022-7-23 16:40
    大小: 124.84KB
    上传者: Argent
    RAMPFunctionExampleLogic
  • 所需E币: 1
    时间: 2022-7-23 10:44
    大小: 108.22KB
    上传者: Argent
    HowtotriggeranAutotuneofaPIDEFunctionBlock
  • 所需E币: 1
    时间: 2022-5-31 09:54
    大小: 799.1KB
    FrequencyResponseFunctionIdentificationofLPVSystems-aGlobalApproachwithApplicationtoMechanicalSystems(TomOomen)
  • 所需E币: 0
    时间: 2022-5-3 21:05
    大小: 286.36KB
    AGeneticAlgorithmforFunctionOptimization-AMatlabImplementation
  • 所需E币: 0
    时间: 2020-8-16 17:48
    大小: 5.38MB
    上传者: bwj312
    KEYSIGHT-5TIPSFORGETTINGTHEMOSTOUTOFYourFunctionGenerator
  • 所需E币: 3
    时间: 2020-4-7 12:20
    大小: 430.76KB
    上传者: wsu_w_hotmail.com
    Speech_Function_User_Manual_V1...,Speech_Function_User_Manual_V1……
  • 所需E币: 3
    时间: 2019-12-27 20:45
    大小: 644.06KB
    上传者: 238112554_qq
    1HowtoMeasuretheLoopTransferFunctionofPowersupplyies.pdfLM22670,LM22671,LM22672,LM22673,LM22674,LM22675,LM22676,LM22677,LM22678,LM22679,LM25005,LM3578A,LM5000,LM5001,LM5002,LM5005ApplicationNote1889HowtoMeasuretheLoopTransferFunctionofPowerSuppliesLiteratureNumber:SNVA364HowtoMeasuretheLoopTransferFunctionofPowerSuppliesNationalSemiconductorHowtoMeasuretheLoopApplicationNote1889TransferFunctionofPowerFrederikDostal……
  • 所需E币: 5
    时间: 2019-12-25 17:30
    大小: 1.27MB
    上传者: 二不过三
    SerialliteMegaCoreFunctionUserGuide……
  • 所需E币: 5
    时间: 2019-12-25 15:18
    大小: 608.8KB
    上传者: 二不过三
    RABBIT嵌入式操作系统资料……
  • 所需E币: 4
    时间: 2019-12-25 15:02
    大小: 361.36KB
    上传者: 二不过三
    ContinuoussignalprocessingisaparallelfieldtoDSP,andmostofthetechniquesarenearlyidentical.Forexample,bothDSPandcontinuoussignalprocessingarebasedonlinearity,decomposition,convolutionandFourieranalysis.Sincecontinuoussignalscannotbedirectlyrepresentedindigitalcomputers,don'texpecttofindcomputerprogramsinthischapter.Continuoussignalprocessingisbasedonmathematics;signalsarerepresentedasequations,andsystemschangeoneequationintoanother.JustasthedigitalcomputeristheprimarytoolusedinDSP,calculusistheprimarytoolusedincontinuoussignalprocessing.Thesetechniqueshavebeenusedforcenturies,longbeforecomputersweredeveloped.CHAPTERContinuousSignalProcessing13ContinuoussignalprocessingisaparallelfieldtoDSP,andmostofthetechniquesarenearlyidentical.Forexample,bothDSPandcontinuoussignalprocessingarebasedonlinearity,decomposition,convolutionandFourieranalysis.Sincecontinuoussignalscannotbedirectlyrepresentedindigitalcomputers,don'texpecttofindcomputerprogramsinthischapter.Continuoussignalprocessingisbasedonmathematics;signalsarerepresentedasequations,andsystemschangeoneequationintoanother.JustasthedigitalcomputeristheprimarytoolusedinDSP,calculusistheprimarytoolusedincontinuoussignalprocessing.Thesetechniqueshavebeenusedforcenturies,longbeforecomputersweredeveloped.The……
  • 所需E币: 3
    时间: 2019-12-25 15:02
    大小: 318KB
    上传者: wsu_w_hotmail.com
    Convolutionisamathematicalwayofcombiningtwosignalstoformathirdsignal.ItisthesinglemostimportanttechniqueinDigitalSignalProcessing.Usingthestrategyofimpulsedecomposition,systemsaredescribedbyasignalcalledtheimpulseresponse.Convolutionisimportantbecauseitrelatesthethreesignalsofinterest:theinputsignal,theoutputsignal,andtheimpulseresponse.Thischapterpresentsconvolutionfromtwodifferentviewpoints,calledtheinputsidealgorithmandtheoutputsidealgorithm.ConvolutionprovidesthemathematicalframeworkforDSP;thereisnothingmoreimportantinthisbook.CHAPTERConvolution6Convolutionisamathematicalwayofcombiningtwosignalstoformathirdsignal.ItisthesinglemostimportanttechniqueinDigitalSignalProcessing.Usingthestrategyofimpulsedecomposition,systemsaredescribedbyasignalcalledtheimpulseresponse.Convolutionisimportantbecauseitrelatesthethreesignalsofinterest:theinputsignal,theoutputsignal,andtheimpulseresponse.Thischapterpresentsconvolutionfromtwodifferentviewpoints,calledtheinputsidealgorithmandtheoutputsidealgorithm.ConvolutionprovidesthemathematicalframeworkforDSP;thereisnothingmoreimportantinthisbook.TheDeltaFunctionandImpulseResponseThepreviouschapterdescribesh……
  • 所需E币: 5
    时间: 2019-12-25 15:02
    大小: 574.33KB
    上传者: quw431979_163.com
    Foreverytimedomainwaveformthereisacorrespondingfrequencydomainwaveform,andviceversa.Forexample,arectangularpulseinthetimedomaincoincideswithasincfunction[i.e.,sin(x)/x]inthefrequencydomain.Dualityprovidesthatthereverseisalsotrue;arectangularpulseinthefrequencydomainmatchesasincfunctioninthetimedomain.WaveformsthatcorrespondtoeachotherinthismannerarecalledFouriertransformpairs.Severalcommonpairsarepresentedinthischapter.CHAPTERFourierTransformPairs11Foreverytimedomainwaveformthereisacorrespondingfrequencydomainwaveform,andviceversa.Forexample,arectangularpulseinthetimedomaincoincideswithasincfunction[i.e.,sin(x)/x]inthefrequencydomain.Dualityprovidesthatthereverseisalsotrue;arectangularpulseinthefrequencydomainmatchesasincfunctioninthetimedomain.WaveformsthatcorrespondtoeachotherinthismannerarecalledFouriertransformpairs.Severalcommonpairsarepresentedinthischapter.DeltaFunctionPairsFordiscretesignals,thedeltafunctionisasimplewaveform,andhasanequallysimpleFouriertransformpair.Figure11-1ashowsadeltafunctionin……
  • 所需E币: 3
    时间: 2020-1-4 23:30
    大小: 279.51KB
    上传者: 16245458_qq.com
    VIA的一份设计规范……
  • 所需E币: 4
    时间: 2020-1-6 12:43
    大小: 62.96KB
    上传者: rdg1993
    UpdatingSimulationModelsforthePOS-PHYLevel4MegaCoreFunction……
  • 所需E币: 4
    时间: 2019-12-24 22:11
    大小: 66.57KB
    上传者: quw431979_163.com
    Abstract:TheBandgapReferenceCalculator(BGRC)aidsinthedesignandanalysisofaBrokawbandgapreferencecircuit.Itcalculatesallcircuitparametersandtheoutputvoltageasafunctionofjunctiontemperature.ThecalculatorcanbeusedwithanHP®50gcalculatororafreePCemulator.Maxim>DesignSupport>AppNotes>A/DandD/AConversion/SamplingCircuits>APP5062Maxim>DesignSupport>AppNotes>AmplifierandComparatorCircuits>APP5062Maxim>DesignSupport>AppNotes>DigitalPotentiometers>APP5062Keywords:accuracycalculatortutorial,analogdigitaldesignanalysis,dataconverterapplicationcircuits,HP50g,voltagereference,Bandgap,BandGap,Brokawcell,magictemperature,curvaturecorrection,lineartransferfunction,temperaturecoefficient,TempcoAug09,2011APPLICATIONNOTE5062BandgapReferenceCalculatorTutorialBy:BillLaumeister,StrategicApplicationsEngineerAbstract:TheBandgapReferenceCalculator……
  • 所需E币: 3
    时间: 2019-12-24 22:06
    大小: 97.62KB
    上传者: 978461154_qq
    Abstract:Alsocalledcodedensitytest,thehistogramtestapproachhelpsdeterminenonlinearityparameterssuchasdifferentialandintegralnonlinearities(INLandDNL)indataconverters.ThefollowingapplicationnotelendsinsightintothemathematicalrelationshipbetweenprobabilitydensityfunctionandvariousdataconverterspecificationsrequiredtosuccessfullycompletethehistogramtestMaxim>AppNotes>A/DandD/ACONVERSION/SAMPLINGCIRCUITSHIGH-SPEEDSIGNALPROCESSINGKeywords:codedensitytest,histogramtesting,ADC,high-speeddataconverter,integralnonlinearity,INL,May31,2003differentialnonlienarity,DNL,samplerecord,codecount,binwidth,probabilitydensityfunction,confidencelevel,hardwarehistogramAPPLICATIONNOTE2085HistogramTestingDeterminesDNLandINLErrorsAbstract:Alsocalledcodedensitytest,thehistogramtestapproachhelpsdeterminenonlinearityparameterssuchasdifferentialandintegralnonlinearities(INLandDNL)indataconverters.Thefollowingapplicationnotelendsinsightintothemathematicalrelationshipbetweenprobabilitydensityfunctionandvariousdataconverterspecificationsrequired……