The last one was here   C++ Hybrid programming idlcpp course Python piece (4)

The first one is here C++ Hybrid programming idlcpp course ( One )

Similar to the previous project , engineering PythonTutorial3 in , Three files have also been added ：PythonTutorial3.cpp, Tutorial3.i, tutorial3.py. among PythonTutorial3.cpp The content of this paper is basically the same PythonTutorial2.cpp The same , I won't repeat .

Let's look at it first Tutorial3.i The content of ：

```namespace tutorial
{
struct Point
{
float x;
float y;
nocode Point();
nocode Point(float a, float b);
nocode Point(const Point& pt);
#{
Point()
{}
Point(float a, float b)
{
x = a;
y = b;
}
#}
};
struct Shape
{
abstract float getArea();
## virtual ~Shape() {}
};
struct Triangle : Shape
{
Point m_vertices[#];
nocode static Triangle+ New();
#{
virtual float getArea()
{
return fabs(m_vertices[].x * m_vertices[].y
+ m_vertices[].x * m_vertices[].y
+ m_vertices[].x * m_vertices[].y
- m_vertices[].x * m_vertices[].y
- m_vertices[].x * m_vertices[].y
- m_vertices[].x * m_vertices[].y) * 0.5;
}
static Triangle* New()
{
return new Triangle;
}
#}
};
}```

There are still struct Point.  Introduces the base class struct Shape. This line

abstract float getArea();

Means to declare a pure virtual function , amount to C++ Medium

virtual float getArea() = 0;

If it's not a pure virtual function , Use keywords virtual Instead of abstract that will do .

struct Triangle : Shape

And C++ equally , use : Represents inheritance . because idlcpp It represents the interface information , So only public inheritance . And C++ Different ,idlcpp did not public, protected, private These three key words .

Then there are data members m_vertices;

Point m_vertices[#3];

idlcpp Only the information about the interface , In its grammar analysis part , You can only see Point m_vertices[]. Numbers 3 You need to insert code . among # Represents an identifier or integer directly followed by （ It's actually made up of letters , A string of underscores and numbers ） Insert into the generated C++ In the corresponding position of the header file . The next line

nocode static Triangle+ New();

Declared a New The static function of , In fact, modern code is in the following type declaration , So here we use nocode Prevent generating function declarations in header files , As mentioned earlier ,idlcpp If you see the constructor declaration , Will generate static functions New, So the constructor declaration cannot appear at this time , In case of name conflict . Let's compare the following implementation part C++ Statement

static Triangle* New()

Here and C++ The inconsistency is + Instead of *, Represents the function interior with new Creates an object in the form of , Return its pointer , The outside world needs to use delete Delete it in the form of （ There's another case of reference counting , Don't talk about ）. In scripting languages, garbage collection mechanism is usually included , Script language automatically manages the allocation and release of memory . Programmers generally don't care when to delete objects , And in the C++ The lifetime of objects allocated on the heap in is generally maintained by the programmer . To deal with the differences ,idlcpp There are several forms in the return value type part of the function declaration ：

 idlcpp Statement C++ Statement Realization typeName typeName Return value typeName & typeName& Return reference typeName * typeName* Return pointer typeName + typeName* Return pointer , The outside world needs delete, Or increase the reference count , The outside world needs release typeName +[] typeName* Return pointer , The outside world needs delete[]

For example, the following C++ Code ：

```int g_a;
int* getGlobal()
{
return &g_a;
}
int* getNew()
{
return new int;
}
int* getNewArray(int count)
{
return new int[count];
}```

The return value type of the three functions is int*, But for the last two , We need to use delete and delete[] Free memory , In terms of grammar , You can't tell these from the declaration of a function . It can only be handled by the programmer according to the actual situation . In scripting languages, you don't want to see the call to delete objects displayed . therefore idlcpp Through the declaration at the grammatical level , Differentiate in the generated metadata code , And then by the runtime library （pafcore.dll） To deal with .

Generated after compilation Tutorial3.h Is as follows ：

```//DO NOT EDIT THIS FILE, it is generated by idlcpp
//http://www.idlcpp.org
#pragma once
#include "./Tutorial3.h"
namespace tutorial{ struct Triangle; }
namespace tutorial
{
struct Point
{
public:
float x;
float y;
Point()
{}
Point(float a, float b)
{
x = a;
y = b;
}
};
struct Shape
{
public:
virtual float getArea() = ;
virtual ~Shape() {}
};
struct Triangle : public Shape
{
public:
Point m_vertices[];
virtual float getArea()
{
return fabs(m_vertices[].x * m_vertices[].y
+ m_vertices[].x * m_vertices[].y
+ m_vertices[].x * m_vertices[].y
- m_vertices[].x * m_vertices[].y
- m_vertices[].x * m_vertices[].y
- m_vertices[].x * m_vertices[].y) * 0.5;
}
static Triangle* New()
{
return new Triangle;
}
};
}```

The content is basically about Tutorial3.i One to one correspondence of .

And then take a look at the script tutorial3.py The content of :

```import pafpython;
paf = pafpython.paf;
triangle = paf.tutorial.Triangle();
triangle.m_vertices = paf.tutorial.Point(0,0);
triangle.m_vertices = paf.tutorial.Point(0,1);
triangle.m_vertices = paf.tutorial.Point(1,1);
print(triangle.getArea()._);```

Created a tirangle object , Then set the data members , The range of subscripts that can be detected by the runtime here is 0 to 2, If out of range, an error will be reported , Finally, call its base class Shape Function declared in getArea(), Because this is a virtual function , So it will eventually call Traingle::getArea().

The compilation results are shown in the figure below ： ## C++ Hybrid programming idlcpp course Python piece (5) More articles about

1. C++ Hybrid programming idlcpp course Python piece (9)

The last one was here C++ Hybrid programming idlcpp course Python piece (8) The first one is here C++ Hybrid programming idlcpp course ( One ) Compared with the previous project , engineering PythonTutorial7 In addition to four files PythonTu ...

2. C++ Hybrid programming idlcpp course Python piece (8)

The last one was here C++ Hybrid programming idlcpp course Python piece (7) The first one is here C++ Hybrid programming idlcpp course ( One ) Similar to the previous project , engineering PythonTutorial6 in , Four files have also been added :Pyt ...

3. C++ Hybrid programming idlcpp course Python piece (7)

The last one was here C++ Hybrid programming idlcpp course Python piece (6) The first one is here C++ Hybrid programming idlcpp course ( One ) And PythonTutorial4 The project is similar , engineering PythonTutorial5 in , Same as ...

4. C++ Hybrid programming idlcpp course Python piece (6)

The last one was here  C++ Hybrid programming idlcpp course Python piece (5) The first one is here C++ Hybrid programming idlcpp course ( One ) engineering PythonTutorial4 Four files have been added to :PythonTutorial4 ...

5. C++ Hybrid programming idlcpp course Python piece (4)

The last one was here C++ Hybrid programming idlcpp course Python piece (3) The first one is here C++ Hybrid programming idlcpp course ( One ) Similar to the previous project , engineering PythonTutorial2 in , Three files have also been added Pyt ...

6. C++ Hybrid programming idlcpp course Python piece (3)

Last one  C++ Hybrid programming idlcpp course Python piece (2)  It's a hello world Example , Only static function calls are involved . This one will have something new . And PythonTutorial0 be similar , engineering Pyth ...

7. C++ Hybrid programming idlcpp course Python piece (2)

In the last article C++ Hybrid programming idlcpp course ( One )  This paper introduces the in idlcpp Tool use . Right now idlcpp With the example tutorial to explain , This is for  Python Examples of language . Let's start with the first example program Pyth ...

8. C++ Hybrid programming idlcpp course Lua piece (6)

The last one was here C++ Hybrid programming idlcpp course Lua piece (5) The first one is here C++ Hybrid programming idlcpp course ( One ) engineering LuaTutorial4 Four files have been added to :LuaTutorial4.cpp, Tut ...

9. C++ Hybrid programming idlcpp course Lua piece (9)

The last one was here C++ Hybrid programming idlcpp course Lua piece (8) The first one is here C++ Hybrid programming idlcpp course ( One ) Compared with the previous project , engineering LuaTutorial7 In addition to four files LuaTutorial7.c ...

## Random recommendation

1. Java Authoritative guide to performance optimization - Reading notes （ Two ）-JVM performance tuning - summary

summary :JVM Performance tuning doesn't have a very fixed setting , For example, how much heap size is set , How many in the old age . But according to the actual application system requirements , The actual active memory is determined . Text : JVM Tuning workflow The whole tuning process is an iteration that repeats itself , ...

YJQApp *appInfo =self.apps[i]; //. Add images UIImageView * iconView = [[UIImageView alloc]init]; CGFloat i ...

3. 【gitlab】 Version management tools

4. JavaScript Advanced programming ( One )：JavaScript brief introduction

One .JavaScript Realization 1. A complete JavaScript It consists of three parts : 1)ECMAScript The core 2)DOM Document object model 3)BOM Browser object model 2. Document object model (DOM) The document is right ...

5. 【Dog】

6. Java Knowledge arrangement one

7. use IDEA stay Tomcat Project of the Ministry of Commerce

In fact, every time you need to run jsp Right click on the page =>run It can also run , But there will be the following Normal should be Run==>Edit Con-- You'll see this page , Never in Defaults inform Tomcat with ...

8. JavaScript Medium namespace

<head> <title> New Document </title> <script> var global = window.global||{} ...

9. Tomcat-----&gt;tomcat Configure the virtual host （ Build a website ）mac

1. First, in the server.xml Add HOST <Host name="www.snowing.com" appBase="/Users/snowing/Downlo ...

10. （ translate ）Getting Started——1.2.4 Tutorial：Storyboard（ The storyboard ）

This tutorial is based on the project you built in the previous lesson . After this tutorial , You will use the view you learned earlier . View controller . action . The content of the navigation , Some key user interfaces will also be created for the application , And add behavior to the scene The following is the content of this lesson : 1. ...