my compiler got dumb (aka gnomes are running the computers)

Started by
15 comments, last by bobofjoe 13 years, 11 months ago
Quote:Original post by Makaan
string q(); is a totally different story

Yes, because here you declare a function named q which takes no parameters and returns a string :)

I think you actually meant to write string q;
Advertisement
Quote:Original post by DevFred
I think you actually meant to write string q;


Yea , well, i usually write that way to make sure i see a constructor call there.

Quote:Original post by Makaan
Quote:Original post by DevFred
I think you actually meant to write string q;


Yea , well, i usually write that way to make sure i see a constructor call there.
Except I'm pretty sure string g(); declares a function named g that returns a string and takes 0 parameters.

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

Quote:Original post by nobodynews
Quote:Original post by Makaan
Quote:Original post by DevFred
I think you actually meant to write string q;


Yea , well, i usually write that way to make sure i see a constructor call there.
Except I'm pretty sure string g(); declares a function named g that returns a string and takes 0 parameters.


you're right, nobodynews.

As Stroustrup puts in pgs. 322 and 323 from PPP

string s1; //default value: the empty string ""string s1("Ike"); //String initialized to Ikestring s1 = string(); //default value: the empty string ""string s2(); //function taking no argument returning a string


As there is no such examples for built-in types, I got confused. But he is refering to the '()', the notation for initializers.
Why the hell would you want to use a construtor/destructor for basic compiler type like int or BYTE when all you have to do is use the '=' like this -> int i = 0; ??? I think it would just overcomplicate things for nothing, + it's easier to read this way imo.
Quote:Original post by Vortez
Why the hell would you want to use a construtor/destructor for basic compiler type like int or BYTE when all you have to do is use the '=' like this -> int i = 0; ??? I think it would just overcomplicate things for nothing, + it's easier to read this way imo.


ah, I would never use them this 'unusual' way. Made the post to understand how c++ treats built-in types as objects.

BTW (can't correctly recall) Java has a primitive type and an object type for int, double, etc. Which, IMHO, doesn't make any sense. If the language is object oriented, than things should be objects! As, by this post and Stroustrup book, it is in C++.
Quote:Original post by Vortez
Why the hell would you want to use a construtor/destructor for basic compiler type like int or BYTE when all you have to do is use the '=' like this -> int i = 0; ??? I think it would just overcomplicate things for nothing, + it's easier to read this way imo.


It lets you have a uniform treatment of types in a template. For instance, this (useless) template works on anything that is default constructible; including primitive types.

template<typename T>T default_construct(){    return T();}assert(default_construct<int>() == 0);assert(default_construct<std::string>() == "");

[size=1]Visit my website, rawrrawr.com

This topic is closed to new replies.

Advertisement