c++ - Constructor called on an already created object -


if call constructor on constructed object or struct, allocate new space, or use existing space? first object allocation more resource intensive? this:

struct f {     int a,b,c,d;     f(int _a, int _b) {a = _a; b = _b};      void a(int _a, int _b) {a = _a; b = _b}; };    //first constructor call f f = f(5, 6);  //second constructor call on constructed object f = f(7, 8);  //third constructor call on constructed object f(7, 8);  //is constructor call more res. intesive, call function same?  f.a(9, 0) 

is constructor call more resource intesive, call function same (void a(...))?

does destructor gets called, when call constructor on created object?

first off, [c] tag inappropriate since constructors c++-only feature. i'll assume code snippet provided in fact c++ , not weird dialect of c. c++ , c different languages; not tag questions both since different answers each.

second, constructor definition wrong. constructors must have exact same name class itself. f() should've been f(). yes, case-sensitivity matters in c++! i'll assume meant rest of code snippet. op made typo.

before explain rest of code does, must understand classes (and structs) in c++ have special member functions automatically generated compiler if don't provide them. is, code snippet same as:

struct f {     f(int _a, int _b) {a = _a; b = _b};  // constructor     ~f() {}                              // destructor     f(const f& rhs)                      // copy constructor         : a(rhs.a)         , b(rhs.b)         , c(rhs.c)         , d(rhs.d)     {}     f& operator=(const f& a)             // copy assignment operator     {         = rhs.a;         b = rhs.b;         c = rhs.c;         d = rhs.d;         return *this;     }      void a(int _a, int _b) {a = _a; b = _b};   // 1 of functions      int a;     int b;     int c;     int d; }; 

if not define copy constructor, copy assignment operator, or destructor, compiler generate them you. also, if don't provide other constructor, compiler generate default constructor. there no default constructor class f since there's (non-copy) constructor accepts 2 arguments.

the default implementation of copy constructor , copy assignment operator copies each data member.

the reason why special member functions exist because c++ generalizes notion of copying primitive types user defined objects. consider this:

int = 42; int b = 13; b = a; 

with primitive types ints, can copy around value that. c++ generalizes copy semantics objects can this:

f f(10, 20); // calls first constructor f g(30, 40); // calls first constructor g = f;       // calls g's copy-assignment operator. 

now can see how applies code:

f f = f(5,6);  

the line above constructs temporary f object, copies temporary f via copy constructor. temporary f object destructed.

f = f(7,8); 

the line above constructs temporary f object, assigns temporary f via copy assignment operator. temporary f object destructed. original f object not destructed.

f.a(9,0)    

the line above normal function call on object called f.

for code snippet, assuming compilers not optimize away temporaries (they in fact do), calling function a "less resource intensive" since no temporaries made in case. however, first constructor call, can this:

f f(5,6); // constructor called; no temporaries made 

understand constructors used for: they used create objects. if have object, don't need call constructor.

as have recommended many times, please pick good c++ book , read it. special member functions , quite fundamental c++.


Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -