In C++, what is the difference between something like:
int i = 3;
and which is better?
Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga.
Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus.
Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
For intrinsic types, they're identical. Even for your own defined classes, they should result in the same code if no or an empty constructor is explicitly defined. However, consider this:
for(int i=0; i<100000; i++)
m_data += sqrtf(m_data);
A(const A &other)
m_data = other.m_data;
A &operator = (float val)
m_data = val;
It's nonsensical, but illustrates the point. In this case, if you do for example
myA = someOtherA;
you're executing the default constructor, which does a metric ton of work, only to then throw away that work by assigning via the = operator.
If instead you do
it uses the copy constructor, which simply assigns the m_data value of the object using m_data of someOtherA.
Even if you don't explicitly define a copy constructor, C++ will create one for you (called the implicitly defined copy constructor), that simply copies the value of each member variable over using their own copy constructor. There's a pitfall here, because if the member variables of a class are complex types themselves, they may have time intensive copy constructors that are invoked by the implicitly generated copy constructor of your class without you noticing.