By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,171 Members | 1,004 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,171 IT Pros & Developers. It's quick & easy.

Constructors & iterators

P: n/a
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.

Dec 13 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
pedag...@gmail.com wrote:
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

Dec 13 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.