|
|
|
![]() |
|
Strumenti |
![]() |
#1 |
Senior Member
Iscritto dal: Jan 2012
Messaggi: 1267
|
[C++] typedef in stile C su pointer type
Ciao,
stavo facendo esercizi presi da un corso che usa il C come linguaggio. Ad un certo punto c'era Codice:
typedef struct Elem { int x; int y; struct Elem *next; } Punto; typedef Punto * Linea; Codice:
using Line = Node<Point>*; Poi mi sono reso conto che non posso usare Line come faccio invece usando esplicitamente Node; cioè non posso fare così: Codice:
Node<int>* list = new Node<int>{ 2 }; Codice:
Line line = new Line{ Point{ 0, 2 } }; Devo fare Codice:
Line line = new Node<Point>{ Point{ 0, 2 } }; Codice:
double length(Line l); // anzichè double length(Node<Point>* l); ![]() Ultima modifica di vendettaaaaa : 11-12-2013 alle 08:20. |
![]() |
![]() |
![]() |
#2 | |
Senior Member
Iscritto dal: Jun 2002
Città: Dublin
Messaggi: 5989
|
Non sono sicuro di aver capito cosa vuoi fare...
Intanto rispondo a ciò: Quote:
![]() Con new costruisci una nuova istanza nell'heap, mentre per istanziare un nuovo oggetto come faresti con una normale variabile automatica si usa questa sintassi: Codice:
Class instance(/* ...parametri del costruttore... */);
__________________
C'ho certi cazzi Mafa' che manco tu che sei pratica li hai visti mai! |
|
![]() |
![]() |
![]() |
#3 | ||
Senior Member
Iscritto dal: Jan 2012
Messaggi: 1267
|
Quote:
Codice:
Line line{ Point{ 0, 2 } }; Quote:
![]() |
||
![]() |
![]() |
![]() |
#4 |
Senior Member
Iscritto dal: Jun 2002
Città: Dublin
Messaggi: 5989
|
Azz hai ragione, dimenticavo fosse un puntatore
![]()
__________________
C'ho certi cazzi Mafa' che manco tu che sei pratica li hai visti mai! |
![]() |
![]() |
![]() |
#5 |
Senior Member
Iscritto dal: Jan 2012
Messaggi: 1267
|
Sì alla fine Line è un typedef per puntatori a Node<Point>* poichè in questo esercizio si vuol vedere una linea come una sequenza (lista) di punti. Però tradurre l'artificio dal C al C++, sebbene sintatticamente lecito, non si sposa bene con new poichè in C++ sarebbe meglio incapsulare il Node<Point>* dentro ad una classe Line, ed a quel punto avremmo un costruttore che prende un Point come parametro e potremmo scrivere
Codice:
Line line{ Point{ 0, 2 } }; |
![]() |
![]() |
![]() |
#6 | |
Senior Member
Iscritto dal: Dec 2005
Città: Istanbul
Messaggi: 1817
|
Quote:
![]() Non e' una critica a te, perche' mi par di capire che il testo ti e' stato dato, ma nel codice e' importante quanto e forse piu' che nella lingua normale usare i termini corretti. In sole due righe l'autore del testo e' riuscito a fare un sacco di confusione: il Punto e' un punto e una lista concatenata contemporaneamente, mentree una linea e' un puntatore a punti... che vuol dire ?!?! ![]() Perche' non scrivere le cose per come stanno ? (uso una notazione C++ perche' e' quella del thread, ma il discorso e' analogo in C) Codice:
struct Punto { int x; int y; }; struct Lista { Punto p; Lista* next; }; typedef Lista Linea;
__________________
One of the conclusions that we reached was that the "object" need not be a primitive notion in a programming language; one can build objects and their behaviour from little more than assignable value cells and good old lambda expressions. —Guy Steele |
|
![]() |
![]() |
![]() |
#7 | |||
Senior Member
Iscritto dal: Dec 2005
Città: Istanbul
Messaggi: 1817
|
Quote:
Quote:
Codice:
typedef Node<Point> Line; Quote:
Dovrai usare Codice:
double length(Line* l)
__________________
One of the conclusions that we reached was that the "object" need not be a primitive notion in a programming language; one can build objects and their behaviour from little more than assignable value cells and good old lambda expressions. —Guy Steele |
|||
![]() |
![]() |
![]() |
#8 | ||
Senior Member
Iscritto dal: Jan 2012
Messaggi: 1267
|
Quote:
using Line = Node<Point>*; dove Point è struct { int x, int y }; e Node è come la tua Lista (è quello che intendevo dicendo "dove Node è un template tipo Elem, tipico nodo di lista singly linked."). Quote:
Ad ogni modo lo using è una nuova feature C++11, serve a fare il template aliasing ma puoi usarlo anche con un typedef, quindi lo sostituisce in toto con una notazione più chiara e concisa (alla C#)! Vedi http://www.stroustrup.com/C++11FAQ.html#template-alias |
||
![]() |
![]() |
![]() |
#9 | ||
Senior Member
Iscritto dal: Dec 2005
Città: Istanbul
Messaggi: 1817
|
Quote:
Sono d'accordo che Codice:
Line line = new Node<Point>{ Point{ 0, 2 } }; Codice:
Line newLine(const Point& p) { return new Node<Point> { p }; } Line line = newLine(Point{ 0, 2} ); Quote:
__________________
One of the conclusions that we reached was that the "object" need not be a primitive notion in a programming language; one can build objects and their behaviour from little more than assignable value cells and good old lambda expressions. —Guy Steele |
||
![]() |
![]() |
![]() |
#10 |
Senior Member
Iscritto dal: Jan 2012
Messaggi: 1267
|
Meh...la funzione può andare ma è ancora una cosa C style...vabbuò farò una classe wrapper anziché usare l'alias.
Grazie cmq nonnino ![]() |
![]() |
![]() |
![]() |
#11 | ||
Senior Member
Iscritto dal: Dec 2005
Città: Istanbul
Messaggi: 1817
|
Quote:
1 - Usi la new direttamente Questo vuol dire che devi chiamare la new con l'oggetto concreto, non un puntatore. Perfare questo usi la classe originale, un ulteriore typedef, o qualche funzione che ti ritorni la classe partendo dal puntatore Codice:
using Line = Node<Point>*; using LineImpl = Node<Point>; Line l = new LineImpl( ... ); Codice:
using Line = Node<Point>*; template<typename T> struct class_of { typedef T type; }; template<typename T> struct class_of<T*> { typedef typename class_of<T>::type type; }; Line l = new class_of<Line>::type( ... ); 2 - Usi una funzione E' l'esempio che ho fatto io 3 - Implementi Line come una classe wrapper, e allora li' puoi utilizzre qualsiasi metodo che vuoi, new, o factory. Questo perche' quando hai un tipo che e' un semplice puntatore, puoi solo applicarci funzioni. In ogni caso l'approccio (definire la line come typedef) ha il suo senso in C, molto meno in C++ Quote:
![]()
__________________
One of the conclusions that we reached was that the "object" need not be a primitive notion in a programming language; one can build objects and their behaviour from little more than assignable value cells and good old lambda expressions. —Guy Steele |
||
![]() |
![]() |
![]() |
#12 | |
Senior Member
Iscritto dal: Jan 2012
Messaggi: 1267
|
Quote:
![]() |
|
![]() |
![]() |
![]() |
Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 14:09.