c++ - How to include a declaration in the comma operator? -


i have 2 simple testing lines:

cout<<(cout<<"ok"<<endl, 8)<<endl;  cout<<(int i(8), 8)<<endl; 

the first line worked, second failed compilation with

error: expected primary-expression before 'int' 

for reason, need declaration in comma operator. more specific, want declare variables, obtain values, , assign them constant class members initialization list of class constructor. following shows intentions. if not achievable using comma operator, suggestions?

#include <iostream> #include <fstream> #include <string> #include <sstream> #include <cstdlib>  using namespace std;  void readfile(const string & filename, int & a, int & b) {     fstream fin(filename.c_str());     if (!fin.good()) {cerr<<"file not found!"<<endl; exit(1);}     string line;     getline(fin, line);     stringstream ss(line);     try {ss>>a>>b;}     catch (...) {cerr<<"the first 2 entries in file "<<filename<<" have numbers!"<<endl; exit(1);}     fin.close(); }  class {     private:         const int _a;          const int _b;      public:         a(const string & filename)             :                _a((int a, int b, readfile(filename,a,b), a)),             _b((int a, int b, readfile(filename,a,b), b))          {                /*               int a, b;             readfile(filename,a,b);             _a = a;_b = b;             */         }             void show(){cout<<_a<<" "<<_b<<endl;} };  int main() {     a("a.txt");     a.show(); } 

you cannot. unpossible in c++. fact trying code smell. something's not right here.

i want declare variables, obtain values, , assign them constant class members initialization list of class constructor. not sure how achieve this.

you didn't intended these variables declare after you've used values, i'm guessing once you've finished values, you've finished variables. in other words, temporary.

your edited example suggests assumption correct. confirms code smell. based on (intended) code, going read file twice.

i'd straightforward way use intermediary, kind of factory class. has benefit of being able read file once, opposed twice doing now.

void readfile (const std::string& filename, int& a, int& b) {     // magic     = 42;     b = 314; }  class filereader { public:     filereader (const std::string filename)     :         mfilename (filename),         ma (42),         mb (314)     {         // happens reading file     }      int geta () const     {         return ma;     }     int getb () const     {         return mb;     } private:     int ma;     int mb;     std::string mfilename; };  class { private:     const int ma;     const int mb; public:     (const filereader& reader)     :         ma (reader.geta()),         mb (reader.getb())     {     } }; 

using filereader simple:

int main() {     mya (filereader ("somefile.txt")); } 

Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -