Constructors & iterators

P

pedagani

Hello Comp.lang.c++ members,
Consider the " begin " routine for a class with an interator.
Iterator begin() {
return( Iterator( _class_element ) );
}
where the constructor for the iterator is defined as
Iterator::Iterator( Node* example) :_iter_element ( example ) {};

what confuses me is the way the constructor is used ( Iterator(
_class_element ) ).
Correct me if I'm wrong, does a constructor implicitly return an
object?
I had never seen a constructor used in this fashion in regular
textbooks. I had understood the constructor as something that is
invoked at the *declaration* of a variable (eg. classname eg (arg) ).
eg is the class object and the chunck of memory that is created by the
constructor is assigned to variable "eg".
However, in the iterator case, how is the memory created in the process
of invoking the constructor? If I understand correctly, a chuck of
memory is created on the fly with no variable tag assigned which is in
contrary to the basic idea of a programming language?
Thanks.
 
M

mlimber

Hello Comp.lang.c++ members,
Consider the " begin " routine for a class with an interator.
Iterator begin() {
return( Iterator( _class_element ) );
}
where the constructor for the iterator is defined as
Iterator::Iterator( Node* example) :_iter_element ( example ) {};

what confuses me is the way the constructor is used ( Iterator(
_class_element ) ).
Correct me if I'm wrong, does a constructor implicitly return an
object?
I had never seen a constructor used in this fashion in regular
textbooks. I had understood the constructor as something that is
invoked at the *declaration* of a variable (eg. classname eg (arg) ).
eg is the class object and the chunck of memory that is created by the
constructor is assigned to variable "eg".
However, in the iterator case, how is the memory created in the process
of invoking the constructor? If I understand correctly, a chuck of
memory is created on the fly with no variable tag assigned which is in
contrary to the basic idea of a programming language?
Thanks.

That syntax creates a temporary object, which can be copied and
manipulated until it goes out of scope. In your case, it is copied as
the return value for the function and then is destroyed.

Cheers! --M
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top