c++11 - C++ object instantiations -
this question has answer here:
- default constructor empty brackets 8 answers
this question object instantiations in c++. there several ways instantiate objects both on heap , on stack curious know subtle differences.
using namespace std; class raisin { private: int x; public: raisin():x(3){} raisin(int input):x(input){} void printvalue() { cout<< "hey deliciousness is: " << x <<endl; } };
basically raisin
simple class using demonstration:
int main() { raisin * a= new raisin; a->printvalue(); raisin * b= new raisin{}; b->printvalue(); raisin * c= new raisin(); c->printvalue(); raisin x; x.printvalue(); raisin y{}; y.printvalue(); raisin z(); z.printvalue(); //error: request member 'printvalue' in 'z', //which of non-class type 'raisin()' raisin alpha(12); alpha.printvalue(); raisin omega{12}; omega.printvalue(); return 0; }
i know why raisin * c
can instantiated empty parenthesis, raisin z
cannot be. (actually raisin z()
legal incorrect)
also understand there subtle difference between raisin (2)
, raisin{2}
. appreciate if can shed light on idiosyncrasies.
this line
raisin z();
declares function (with no argument) returning raisin object, not default-constructed raisin
Comments
Post a Comment