c++ - In the following code how can I initialize a class member without using dummy values? -
this question has reply here:
what weird colon-member (“ : ”) syntax in constructor? 11 answers// illustration program
#include <iostream> #include <vector> #include <array> class a{ public: a(int tempy){ y = tempy; } int y = 0; }; class b{ public: b(a tempz){ z = tempz.y; } z; }; int main() { int x = 1; obja(x); b objb(obja); std::cout << "y = " << objb.z << "!\n"; }
build at: http://cpp.sh/3yj2
there error in class b because haven't passed constructor parameter fellow member z. don't want initialize dummy value, there way utilize constructor parameters build fellow member z, , how tell z utilize b's constructor parameters?
if i'm missing fundamental aspect of c++ please allow me know i'm starting out.
you can utilize initializer lists this
class b{ public: b(a tempz) : z(tempz) { } z; };
this way z initialized new instance of created re-create constructor.
c++
No comments:
Post a Comment