|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Senior Member
Iscritto dal: Jan 2012
Messaggi: 1267
|
[C/C++] Manipolare nomi di variabili con MACRO
Ciao,
sto scrivendo una classe con tante proprietà, e vorrei farlo tramite una macro: anzichè ripetere ogni volta getter e setter: Codice:
private:
int m_var;
public:
int var() const { return m_var; }
void setVar(int newVar) { m_var = newVar; }
Codice:
#define DECLARE_QPROPERTY(type, name) \
private: type m_##name = 3; \
public: type name() const { return m_##name; } \
public: void setname(type new##name) { m_##name = new##name; }
|
|
|
|
|
|
#2 |
|
Senior Member
Iscritto dal: Jul 2005
Città: Vicenza
Messaggi: 1570
|
E dare semplicemente ai campi di classe nomi che iniziato con lettera maiuscola?
ps: la nomenclatura classica per i getter/setter solitamente non è getValue, setValue? ps2: odio con tutta la mia anima i getter/setter. Siano lodate le property (alla C# per intendersi) Ultima modifica di [Kendall] : 15-10-2013 alle 18:09. |
|
|
|
|
|
#3 | |
|
Senior Member
Iscritto dal: Jan 2012
Messaggi: 1267
|
Quote:
Cmq non sono indispensabili, non mi infastidisce troppo il dover scrivere i getter/setter. Niente lettere maiuscole per i campi privati, te lo insegnano anche in C# La nomenclatura standard prevede getValue, ma solo value() mi sembra molto pulito e cmq logico, anzi, più pulito di getValue, semanticamente, in certi usi. Infatti, considera la proprietà "inSecondFunction" della mia calcolatrice. Nella UI scriverei: if (casio.inSecondFunction) anzichè if (casio.getInSecondFunction) molto meglio Cmq ho provato così: Codice:
#define DECLARE_QPROPERTY(type, name, readonly, notify) \
private: type m_##name; \
public: type name() const { return m_##name; } \
#if !readonly
public Q_SLOTS: void set_##name(type n##name) { m_##name = n##name; } \
#endif
#if notify
Q_SIGNALS: void name##Changed();
#endif
Ultima modifica di vendettaaaaa : 15-10-2013 alle 18:27. |
|
|
|
|
|
|
#4 | |
|
Senior Member
Iscritto dal: Jul 2005
Città: Vicenza
Messaggi: 1570
|
Quote:
Riguardo ai nomi dei campi, io uso sempre la nomenclatura _value, a mio modo di vedere la migliore per identificarli in maniera precisa e "ad occhio". |
|
|
|
|
|
|
#5 |
|
Senior Member
Iscritto dal: Jan 2012
Messaggi: 1267
|
Penso che questa soluzione sia ragionevole:
Codice:
#define DECLARE_QPROPERTY(type, name) \
private: type m_##name; \
public: type name() const { return m_##name; } \
Q_PROPERTY(type name READ name)
#define DECLARE_QPROPERTY_WRITE(type, name) \
private: type m_##name; \
public: type name() const { return m_##name; } \
public Q_SLOTS: void set_##name(type n##name) { m_##name = n##name; } \
Q_PROPERTY(type name READ name WRITE set_##name)
#define DECLARE_QPROPERTY_NOTIFY(type, name) \
private: type m_##name; \
public: type name() const { return m_##name; } \
Q_SIGNALS: void name##Changed(); \
Q_PROPERTY(type name READ name WRITE set_##name)
#define DECLARE_QPROPERTY_WRITE_NOTIFY(type, name) \
private: type m_##name; \
public: type name() const { return m_##name; } \
public Q_SLOTS: void set_##name(type n##name) { m_##name = n##name; } \
Q_SIGNALS: void name##Changed(); \
Q_PROPERTY(type name READ name WRITE set_##name NOTIFY name##Changed)
|
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 16:41.




















