5
APPLICAZIONI .NET CON C++/CLI INDICE PREFAZIONE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 IL FRAMEWORK .NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 L'architettura .NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 CLR. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 La compilazione JIT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Common Type System. . . . . . . . . . . . . . . . . . . . . . . . . . . . .11 Gestione della memoria . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 MANAGED O NATIVO? . . . . . . . . . . . . . . . . . . . . . . . . . . .15 SINTASSI E SEMANTICA DI C++/CLI . . . . . . . . . . . . . . . . 16 HELLO C++/CLI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .16 IL METODO MAIN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 Argomenti del main . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 ORGANIZZARE LE CLASSI . . . . . . . . . . . . . . . . . . . . . . . . . 19 I namespace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 ISTRUZIONI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 DICHIARAZIONI E VARIABILI . . . . . . . . . . . . . . . . . . . . . . . .22 Variabili. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 Inizializzazione . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 Blocco di istruzioni . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 TIPI DI DATI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .27 Tipi fondamentali . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 Tipi riferimento . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 TIPI PERSONALIZZATI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 Tipi valore . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 Tipi riferimento . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 Scaricalo gratis su http://www.punto-informatico.it/ PILibri

APPLICAZIONI .NET CON C++/CLI

  • Upload
    pilibri

  • View
    44

  • Download
    0

Embed Size (px)

Citation preview

Page 1: APPLICAZIONI .NET CON C++/CLI

APPLICAZIONI .NET CON C++/CLI

• INDICE• PREFAZIONE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3• IL FRAMEWORK .NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9• L'architettura .NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9• CLR. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9• La compilazione JIT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11• Common Type System. . . . . . . . . . . . . . . . . . . . . . . . . . . . .11• Gestione della memoria . . . . . . . . . . . . . . . . . . . . . . . . . . . 12• MANAGED O NATIVO? . . . . . . . . . . . . . . . . . . . . . . . . . . .15• SINTASSI E SEMANTICA DI C++/CLI . . . . . . . . . . . . . . . . 16• HELLO C++/CLI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .16• IL METODO MAIN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18• Argomenti del main . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18• ORGANIZZARE LE CLASSI . . . . . . . . . . . . . . . . . . . . . . . . . 19• I namespace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20• ISTRUZIONI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21• DICHIARAZIONI E VARIABILI . . . . . . . . . . . . . . . . . . . . . . . .22• Variabili. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22• Inizializzazione . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24• Blocco di istruzioni . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25• TIPI DI DATI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .27• Tipi fondamentali . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28• Tipi riferimento . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29• TIPI PERSONALIZZATI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30• Tipi valore . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30• Tipi riferimento . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32

Scaricalo gratis su http://www.punto-informatico.it/PILibri

Page 2: APPLICAZIONI .NET CON C++/CLI

APPLICAZIONI .NET CON C++/CLI

• Le classi . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33• Interfacce . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34• Delegate ed eventi . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34• Array. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34• Conversioni di tipi semplici . . . . . . . . . . . . . . . . . . . . . . . . . . . 36• CONTROLLO DI FLUSSO . . . . . . . . . . . . . . . . . . . . . . . . . . . 39• OPERATORI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .39• GLI OPERATORI ARITMETICI . . . . . . . . . . . . . . . . . . . . . . . 40• OPERATORI DI CONFRONTO . . . . . . . . . . . . . . . . . . . . . . . 42• Operatori logici . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43• Operatori bitwise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43• OPERATORI DI SHIFT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45• L’operatore condizionale . . . . . . . . . . . . . . . . . . . . . . . . . . . . .45• Operatori di assegnamento. . . . . . . . . . . . . . . . . . . . . . . . . . . 47• Operatori di indirizzamento . . . . . . . . . . . . . . . . . . . . . . . . . . 48• ISTRUZIONI DI SELEZIONE . . . . . . . . . . . . . . . . . . . . . . . . 49• Il costrutto if/else . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50• L’istruzione switch. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51• ISTRUZIONI DI ITERAZIONE. . . . . . . . . . . . . . . . . . . . . . . . .53• Il ciclo while . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53• Il ciclo do while . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55• Il ciclo for . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55• L'istruzione foreach. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57• ISTRUZIONI DI SALTO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58• break. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59• Continue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60

Scaricalo gratis su http://www.punto-informatico.it/PILibri

Page 3: APPLICAZIONI .NET CON C++/CLI

APPLICAZIONI .NET CON C++/CLI

• Goto . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61• GLI ARRAY. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .62• Array monodimensionali. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63• Array multidimensionali . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64• Array jagged . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65• PROGRAMMAZIONE AD OGGETTI . . . . . . . . . . . . . . . . . . . 67• OGGETTI E CLASSI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .67• C++/CLI ORIENTATO AGLI OGGETTI . . . . . . . . . . . . . . . . . 68• CLASSI E STRUTTURE . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68• Modificatori di accesso . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69• Ereditarietà di classi ref. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70• Classi sealed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71• Istanziare oggetti . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72• Handle ed oggetti . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73• La Parola chiave this . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .74• Costruttori. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75• Costruttore per copia . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77• Costruttore statico . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78• Campi di classe . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79• Metodi . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81• Metodi virtuali . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83• Override dei metodi . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84• Metodi virtuali puri . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86• Overloading dei metodi. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86• Overloading degli operatori . . . . . . . . . . . . . . . . . . . . . . . . . . 86• Operatori unari . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87• Operatori binari. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88

Scaricalo gratis su http://www.punto-informatico.it/PILibri

Page 4: APPLICAZIONI .NET CON C++/CLI

APPLICAZIONI .NET CON C++/CLI

• Proprietà . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89• Classi innestate. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92• CLASSI ASTRATTE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93• INTERFACCE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94• Override esplicito . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101• CONVERSIONI DI TIPO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104• L’operatore dynamic_cast . . . . . . . . . . . . . . . . . . . . . . . . . . . 105• L’operatore static_cast . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106• L’operatore safe_cast . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106• CONCETTI AVANZATI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109• DELEGATE ED EVENTI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109• Delegate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110• Dichiarazione di un delegate. . . . . . . . . . . . . . . . . . . . . . . . . 110• Istanziazione e invocazione di un delegate . . . . . . . . . . . . . . 113• Delegati e interfacce . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116• La classe System.Exception . . . . . . . . . . . . . . . . . . . . . . . . . . 134• Eccezioni personalizzate . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135• LE CLASSI DEL FRAMEWORK . . . . . . . . . . . . . . . . . . . . . . . . 137• La classe System::Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138• Il metodo ToString . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139• I metodi Equals e ReferenceEquals . . . . . . . . . . . . . . . . . . . . 141• Il metodo GetHashCode . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142• Il metodo GetType. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144• CONCLUSIONI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145• Note biografiche . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145

Scaricalo gratis su http://www.punto-informatico.it/PILibri

Page 5: APPLICAZIONI .NET CON C++/CLI

APPLICAZIONI .NET CON C++/CLI

INTRODUZIONE

Questo libro è una guida alla programmazione in C++/CLI, il nuovo linguaggio che consentirà ai numerosi programmatori C++ sparsi nel mondo di sviluppare applicazioni per .NET, da 2.0 in su, senza avere nulla da invidiare agli sviluppatori C#, VB.NET, o a quelli che utilizzano uno dei tanti linguaggi ormai supportati dal framework di programmazione di casa Microsoft. Anzi, C++/CLI si candida, ed ha tutte le carte in regola, per diventare uno degli attori protagonisti sul palcoscenico dello sviluppo software .NET. C++ è stato ed è tuttora uno dei linguaggi più utilizzati e più potenti. Con C++ si può realizzare qualunque tipo di applicazione, e forse l’unica mancanza, a meno di un tentativo poco elegante a dir poco come le managed extensions, era l’impossibilità di sviluppare applicazioni .NET. Prima di rispondere alla domanda che molti di voi si sono probabilmente posti, quando hanno visto per la prima volta il termine C++ accostato alla sigla CLI, è meglio partire da cosa siano separatamente C++ e CLI. C++/CLI rappresenta una tupla. Il primo termine è C++, che si riferisce al linguaggio C++ standard inventato da Bjarne Stroustrup. C++ supporta un modello ad oggetti statico ottimizzato per la velocità di esecuzione e le prestazioni. Consente poi l’accesso al sistema operativo e all’hardware sottostante, ma dà solo un piccolo supporto per accedere ai tipi attivi durante l’esecuzione. CLI si riferisce invece alla Common Language Infrastructure, l’architettura multi strato che è un componente fondamentale di .NET e che supporta il modello di programmazione dinamico ed a componenti. Lo slash (/) che unisce rappresenta proprio il collegamento dra C++ e CLI.

Scaricalo gratis su http://www.punto-informatico.it/PILibri