Does[code]string a = string(); same with string a;[/code]?

Doesstring a = string(); same with string a;?
Or
Doesvalarray<int> a = valarray<int>(); same with valarray<int> a;?

I designed a class contain a string class and a valarray class, when I making the default constructor, I want to use the default constructor of string and valarray. Is it the right syntax?
 
student::student():name(std::string()),scores(std::valarray<double>()){}

is it equvalent with do nothing like below
 
student::student(){}//try to leave the job of calling constructors of string and valarray to the compiler. 
> Does string a = string(); same with string a;?
> Or valarray<int> a = valarray<int>(); same with valarray<int> a;?

Yes, the effect would be the same (default constructed).


> student::student(){}//try to leave the job of calling constructors of string and valarray to the compiler.

Yes.

Or don't declare it at all; let it be implicitly generated by the compiler:
1
2
3
4
5
struct student
{
      std::string s ;
      std::valarray<int> va ;
};


If we have other constructors, we can explicitly force the generation of a default constructor:
1
2
3
4
5
6
7
struct student
{
      student() = default ; // C++11
      explicit student( const char* name ) : s(name) {}
      std::string s ;
      std::valarray<int> va ;
};

OIC, thanks.
But why I bother explicitly offer a default constructor when have other constructors, anyway the compiler would generate one.
> why I bother explicitly offer a default constructor when have other constructors,
> anyway the compiler would generate one.

The compiler generates an implicitly declared default constructor only if there are no user-defined constructors.
(And base classes and non-static members are DefaultConstructible).
Topic archived. No new replies allowed.