Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I don't understand the purpose of constructors and how they function in C++. I understand that a constructor a is a function within a class that has the same name as the class but does not have a return type.
The purpose of a constructor in C++ is to reliably initialize raw memory, turning it into a useful object by establishing the chosen class invariant (what you can always assume about the object between calls of its member functions).
In contrast, an assignment has to change the value of an already initialized object, and may for example have to deallocate buffers that already have been established.
As long as you don't use very low level features of the language there is effectively a constructor call guarantee: that every object instantiated from a type T that has at least one user defined constructor, gets a single external call to a T constructor, which happens before anything else. Conversely, when you call a constructor by using the type name as a pseudo function name, T(), or with arguments, a T object is created. So the guarantee works both ways, and means that object creation involves constructor call and vice versa.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I know that we can directly assign same class objects in C++,but what actually happen behind the scene?
There's something called "default copy-constructor" and "default assignment-operator". Unless you overload these methods in the class, the default behavior is that all non-static members of the class are copied one-by-one from the source to the target class.
A little more: This includes pointers, btw. Which is why you generally should overload these operators and follow the rule of three if you have pointers as members.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
In C++, is it OK to call a non-member function (either a free function or a member of other objects) from within the destructor? I am calling a (non-virtual, although in this case it shouldn't really matter) method of an object of a different class. This method crashes on trying to access its members.
On the other hand, if this different object is a child of the destructed object (Qt), does this matter?
In general a destructor can call any function it needs to properly destroy the object. However, there are a couple of caveats:
If the function called from a destructor throws an exception, the exception must be caught and handled in the destructor.
The function called from a destructor must not unconditionally create and destroy objects of the type to which the destructor belongs (since this will result in infinite recursion).
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Why only default constructor only able to create vptr(Virtual Table Pointer) and vtable(Virtual Table)? Why parameter constructor not able to
First, vtables and vptrs are not specified by the C++ language standard.
They're an implementation detail, although as far as I know all extant C++ implementation use that technique to implement virtual function dispatch.
With such an implementation, all constructors for a class with virtual member functions, necessarily establish the object's vptr. Things wouldn't work without it. So …
” Why parameter constructor not able to
… is simply an incorrect assumption.
Assuming the implementation uses vtables (a pretty common implementation choice, as noted by "Cheers and hth - Alf"), the creation/population of vtables and invoking constructors are distinct operations.
However, all instances of a given (non-abstract) class will - once constructed fully - have the same set of vtables (a class that inherits from multiple bases may have more than one vtable). There will certainly not be a different vtable depending on how the object is constructed (e.g. what parameters are passed to constructors, which constructor is invoked, etc).
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
After asking C++ Error linking in consumer file caused by static data field, I tried two different declarations for an instance of StateConservator:
StateConservator cs(*pContainer, pDoc->GetConfiguration());
and
StateConservator(*pContainer, pDoc->GetConfiguration());
The first did what I want, it only passes on the destructor only after the end of the scope. The second passes on the destructor in the own line of the declaration itself.
Is the compiler behaving correctly? If it is the correct behavior what is the way to declare an anonymous variable in that line?
You cannot have "unnamed" objects in C++. In the second case, the object is created and destroyed instantaneously because it is not associated with a name. The association with a name (e.g., variable) gives an object scope which controls its lifetime. By not naming an object, it's lifetime is bound to the statement. If it is given a name, then it is bound to the scope that the name is declared in.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Or in a class method, call other class' method/free function and pass this pointer as the parameter. Is there any good practice for this? Or it is a bad practice and I should not doing so?
It usually makes no sense to return the this pointer from class meber functions.
(since the caller must be already holding a reference to the instance,, to call the method/operator in question, and you cannot change it returning di9fferent values anyway).
It makes sense to return *this from various types of member functions (specific operators in particular, e.g. operator=()), to enable chaining of operations on results.
NOTE:
Some of the overloadable operator signatures (as the mentioned assignment operator) require to implement such behavior to work correctly with the semantics defined by C++!