0

In below code default constructor as well as parameterized constructor with int argument are creating temporary objects. But vector of string as a parameter to constructor is not even compiling. Can anybody explain this behavior?

#include <vector>
#include <string>

class C {                                                                                                                                                     
public:                                                                                                                                                       
    std::vector<std::string> v;                                                                                                                               
    int i;                                                                                                                                                    
    C() {                                                                                                                                                     

    }                                                                                                                                                         
    C(int j) {                                                                                                                                                
        i = j;                                                                                                                                                
    }                                                                                                                                                         

    C(std::vector<std::string> tmp) {                                                                                                                         
        v = tmp;                                                                                                                                              
    }                                                                                                                                                         
};

int main() {                                                                                                                                                  
    std::vector<std::string> v = {"Stack", "Overflow"};                                                                                                       
    C();                        // works..                                                                                                                    
    C(1);                       // works..                                                                                                                    
    C(v);                       // compilation fails..                                                                                                        
    return 0;                                                                                                                                                 
}

Below is the compilation output:

g++ -g constructor.cpp -std=c++1z
constructor.cpp: In function ‘int main()’:
constructor.cpp:25:8: error: conflicting declaration ‘C v’
     C(v);      // compilation fails..
        ^
constructor.cpp:22:27: note: previous declaration as ‘std::vector<std::__cxx11::basic_string<char> > v’
  std::vector<std::string> v = {"Stack", "Overflow"};
                           ^
UnSat
  • 1,347
  • 2
  • 14
  • 28

0 Answers0