c++ - Default constructor in template class with attribute of unknown type -


i need default constructor no argument. how can initialize attribute a of unknown type me.

template <typename type> class foo { public:     foo() : a(), b(0) {}  <---- here confusion  private:     type a;     int b; }; 

edit : answer has been given in comments below, there still don't understand. if have :

typedef enum {ab, cd} enumtype  template <typename type> class foo { public:     foo() {}  // <---- "member 'b' no initialized in constructor"  private:     type a;     enumtype b; }; 

my compiler gives me warning : member 'b' no initialized in constructor. why giving me warning b enum , not a ?

this correct long type type has default constructor. when declare template assuming things types, not every type can passed in constructor of specific template. here, fine standard types , have default constructor. if initialize class foo own type doesn't provide default constructor error.

to answer second issue:

if had defined variable @ namespace scope, value initialized 0.

enum someenum {       evalue1 = 1,       evalue2 = 4,   }; someenum e; // e 0 int i;      // 0  int main() {     cout << e << " " << i; //prints 0 0  } 

don't surprised e can have values different of someenum's enumerator values. each enumeration type has underlying integral type(such int, short, or long) , set of possible values of object of enumeration type set of values underlying integral type has. enum way conveniently name of values , create new type, don't restrict values of enumeration set of enumerators' values.

to zero-initialize object of type t means:
— if t scalar type (3.9), object set value of 0 (zero) converted t;

note enumerations scalar types.

to value-initialize object of type t means:
— if t class type blah blah
— if t non-union class type blah blah
— if t array type, blah blah — otherwise, object zero-initialized

typedef enum {a,b,c,d} enumtype;  template <typename type> class foo { public:     foo() {}  // <---- "member 'b' no initialized in constructor"  public:     type a;     enumtype b; };  /*  *   */ int main(int argc, char** argv) {      foo<int> fo;     std::cout<<std::endl<<"fo.a:"<<fo.a<<",fo.b:"<<fo.b<<std::endl;     enumtype e=d;     fo.b=d;     std::cout<<std::endl<<"fo.a:"<<fo.a<<",fo.b:"<<fo.b<<std::endl;      foo<int>* go=new foo<int>;     std::cout<<std::endl<<"go->a:"<<go->a<<",go->b:"<<go->b<<std::endl;     go->b=d;     std::cout<<std::endl<<"go->a:"<<go->a<<",go->b:"<<go->b<<std::endl; 

fo.a:-137090040,fo.b:32767

fo.a:-137090040,fo.b:3

go->a:-166889576,go->b:32767

go->a:-166889576,go->b:3

now:

    foo<int>* go=new foo<int>();     std::cout<<std::endl<<"go->a:"<<go->a<<",go->b:"<<go->b<<std::endl;     go->b=d;     std::cout<<std::endl<<"go->a:"<<go->a<<",go->b:"<<go->b<<std::endl; 

go->a:0,go->b:0

go->a:0,go->b:3


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -