Domanda

Ho una domanda newbie su come assegnare membro della classe (setter). Sono abituato a scripting e per lo più non ci si fa via (in Python)

def set_mymember(mymember):
     self.mymeber = mymember

Il mio collega mi ha detto "io" e "questo" non sono necessari in C ++, "questo" esiste e non è sbagliato in questo contesto, ma che sarebbe difficile da capire per me così mi ha detto che non dovrebbe importare. Così ho provato secondo il suo consiglio:

La mia definizione di classe: - (dovrebbe creare una stringa di query SQL)

class Query
{

public:
   Query() { }
   ~Query() { }

   void setoptions( std::string qtext_where="", bool qtext_erl=true, std::vector<std::string> kids=std::vector<std::string>() );
   Query build_query( );

   void set_db_table( std::string db_table );
   void set_db_key( std::string db_key );
   void set_m_qtext( std::string m_qtext );
   void set_foo( std::string foo );

   std::string sql();
   std::string get_sql_update();

private:
   std::string m_db_table;       // Tabellenname
   std::string m_db_key;         // Tabellen-key

   std::string m_qtext_where;    // add.optionale where clause
   std::string m_qtext;          // fertiger SELECT
   std::string m_sql_update;     // fertiger UPDATE
   bool m_erl;                   // nur erledigte waehlen?
   std::vector<std::string> m_kids;    // Liste von keys zu selecten
};

Ed ecco uno dei metodi setter: Io li chiamo con un filo pieno e vettoriale, doppio controllo in questo codice

void Query::setoptions( string qtext_where, bool erl, vector<string> kids ) {
   m_qtext_where  = qtext_where;
   m_erl          = erl;
   m_kids = kids;     
}

Ma quando la mia app in seguito chiama query.build_query()

le variabili sono vuote

Query Query::build_query( ) {
   cout << "kids size" << m_kids.size() << endl;
   cout << "m_qtext_where " << m_qtext_where << endl;
   // Query zur auswahl der zu uebertragenden Datensaetze
   string sql_update = "UPDATE " + m_db_table;

   string qtext = "SELECT * FROM " + m_db_table;
   string qtext_order = " ORDER BY " + m_db_key;
   (...)

Modifica : parte Quindi, è qui del codice app che chiama 1.setoptions, e 2.build_query

       // read file line by line into vector of strings
       vector<string> text_file;
        ifstream ifs( input );
        string temp;
        while( getline( ifs, temp ) ) {
           if (temp.substr(0,1) == "#" ) {
              cout << "COMMENT: " << temp << endl;
              continue;
           }
           cout << temp << endl;
           text_file.push_back( temp );
        }
        // check: yes, vector has a size = number of lines
        cout << "text_file size " << text_file.size() << endl;

        // create Query object
        Query query = Query();
        // set the members, bool erl = true
        query.setoptions( "", erl, text_file );
        // call 2nd method         
        q2 = query.build_query();
È stato utile?

Soluzione

Non si può davvero dire che cosa sta succedendo senza il codice completo, ma ho il sospetto che si sta restituendo un oggetto query da query.build_query che non è una copia completa dell'oggetto query, se questo ha un senso? Si può includere il testo completo delle build_query?

Inoltre, mi piacerebbe fare il metodo build_query vuoto, e non cercare di assegnare un oggetto fresco di nuovo query per un secondo oggetto Query (Q2) a tutti (a meno che non si ha realmente bisogno per, ancora una volta, non si può davvero dire, senza la completo di codice), qualcosa di simile:

void Query::build_query( ) {
    std::cout << "kids size" << m_kids.size() << std::endl;
    std::cout << "m_qtext_where " << m_qtext_where << std::endl;
}

main
{
    ...
    Query query = Query();
    // set the members, bool erl = true
    query.setoptions( "", true, text_file );
    // call 2nd method         
    query.build_query();
}

Inoltre, solo di essere pedanti qui, ma dato che si sta fornendo args di default per tutte le opzioni, sarei incline a inizializzare loro nel costruttore in questo modo:

Query::Query() 
    : m_qtext_where("")
    , qtext_erl(true)
    , kids (std::vector<std::string>()
{}

E allora invece di un metodo setOptions, hanno setter per ogni singola variabile:

void setWhere(std::string qtext_where) {m_qtext_where = qtext_where ;}
void setErl(bool query_erl) { m_erl = query_erl; }
void setKids(std::vector<std::string> kids) { m_kids = kids; }

che si chiama solo quando è necessario ..

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top