return value of constructor

Started by
29 comments, last by Conner McCloud 18 years, 1 month ago
Quote:Original post by Conner McCloud
I think one would be justified in saying that a user never directly calls a constructor, they simply create objects and the constructors get called at the appropriate time.


Using placement new to create objects/arrays from a pre-allocated memory buffer is the perfect reason to call the constructor manually. It's really the quickest and cleanest way I know to be able to construct an array of objects using any number (and combination) of constructors to initialize the data.

C++ FAQ Lite -- section 11.10
Advertisement
Quote:Original post by Programmer16
Quote:Original post by Conner McCloud
I think one would be justified in saying that a user never directly calls a constructor, they simply create objects and the constructors get called at the appropriate time.


I agree with everything you said except this line. What about:

*** Source Snippet Removed ***

I was just trying to prove a point, it really doesn't matter.

CM
Programmer16, I have made a correction to your code

class Base{    int* m_pData;public:    Base(int nAmount)    {        m_pData = new int[nAmount]; // What happens if you have no memory                                    // You need to make sure that you are aware    }};class Derived : Base{public:    Derived() : Base (10)  // This is how you call a parents constructor    {    }    Derived(int nAmount) : Base (nAmount)    {    }};// And just in case you were talking about calling just the classe's // constructor. Wouldn't the following be directly calling it?Derived MyDerived(21);

Quote:Original post by Adam Hamilton
Programmer16, I have made a correction to your code

*** Source Snippet Removed ***


While technically you are correct, since my code didn't work as it is, but I had made a mistake. I forgot to add a default constructor. If I had done so, my code would work.

@ Conner McCloud: Sorry, I wasn't trying to be rude or anything.
Quote:Original post by Programmer16
@ Conner McCloud: Sorry, I wasn't trying to be rude or anything.

No worries, I didn't take it as rude. I just don't feel like justifying my position. Too late for that [grin]

CM
Quote:Original post by Programmer16
While technically you are correct, since my code didn't work as it is, but I had made a mistake. I forgot to add a default constructor. If I had done so, my code would work.

It would have worked, but would still have created local variables of type Base, not called the base-class constructor with a parameter.

Quote:Original post by taby
Using placement new to create objects/arrays from a pre-allocated memory buffer is the perfect reason to call the constructor manually. It's really the quickest and cleanest way I know to be able to construct an array of objects using any number (and combination) of constructors to initialize the data.

You find it quicker and cleaner to correctly construct an array in an exception-safe manner using placement new than to use vector or another standard container [wow]?

Σnigma
Quote:Original post by Enigma
You find it quicker and cleaner to correctly construct an array in an exception-safe manner using placement new than to use vector or another standard container [wow]?

Σnigma


Don't get me wrong, I rarely ever use new/delete, and have successfully replaced this method of dynamic allocation by relying on the vector's inherent contiguous data layout / address-of operator to interface with functions requiring a pointer.

Yes, one could definitely use the reserve function to preallocate the buffer, then use the [] or .at or some iterator to initialize/assign the elements by using a temporary object. I find however, that it can be extremely prohibitive in terms of CPU time to create temporary objects to initialize arrays as such.

I see your point, but I don't always find it to be the most optimal solution in terms of speed or plain code reuse -- I am the epitome of the programmer who consistently forgets what they were attempting to code only hours ago. :)

On one final note, exceptions to me are similar (but more than just) debug assertions. They should theoretically only occur in production code when there is a catastrophic system failure, like an out of memory state, or if there is a bug in the code which still needs to be resolved.

At that point, I rarely care what the behaviour of the application is. I either need to fix it and re-release the production code (if it's a bug), or ignore it completely since the system's whole state is way beyond the control of my app/myself.
Quote:Original post by Conner McCloud
If it were a function returning void, that would suggest you could make it a function returning int. But you can't, so you might as well treat it like a special function from the ground up.


I don't want to sound like I'm arguing for a return value, but I don't find that convincing because main() is required to return int (or something "implementation defined"). I would be interested to see the discussion (if any) the standardizing committee had on the issue.

In the end, I'm guessing it just comes down to "what makes more sense to you". Having a return value like every other function because it's still a function, or having no return value because it's unlike any other function.

Does anyone know what Cfront had the constructors return after compilation to C?

Quote:
I seriously doubt it. For instance...when you overload operator new, you don't call the constructor. The system takes care of that for you. I think one would be justified in saying that a user never directly calls a constructor, they simply create objects and the constructors get called at the appropriate time. With this in mind, the compiler is the only thing that'll care if the constructor returns a value or not...and if it did, the compiler can just stealth in whatever return value it wants.


I see, thanks for the information!
Constructors can have outputs just as they can have inputs. One can argue about how good a programming practice that is, but the method is to use references or pointers.
object :: object ( int& Result ){    Result = SUCCESS;}// or...object :: object ( int* Result ){    *Result = SUCCESS;}

~CGameProgrammer( );Developer Image Exchange -- New Features: Upload screenshots of your games (size is unlimited) and upload the game itself (up to 10MB). Free. No registration needed.
Quote:Original post by Way Walker
In the end, I'm guessing it just comes down to "what makes more sense to you". Having a return value like every other function because it's still a function, or having no return value because it's unlike any other function.


The answer is that functions can return values. Constructors and destructors are not functions. They cannot return values. It's just unfortunate that their definitions use a syntax apparently similar to that of functions. Don't confuse syntax with semantics.

Stephen M. Webb
Professional Free Software Developer

This topic is closed to new replies.

Advertisement