Constructors & iterators

Discussion in 'C++' started by pedagani@gmail.com, Dec 13, 2005.

  1. Guest

    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, 2005
    #1
    1. Advertising

  2. mlimber Guest

    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
     
    mlimber, Dec 13, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dave Rudolf
    Replies:
    12
    Views:
    8,303
    Martijn Lievaart
    Feb 6, 2004
  2. Jeremy Smith
    Replies:
    2
    Views:
    589
    Jeremy Smith
    Aug 3, 2006
  3. Jess
    Replies:
    5
    Views:
    608
    Ron Natalie
    Jun 7, 2007
  4. Peng Yu
    Replies:
    5
    Views:
    399
    Juha Nieminen
    Sep 19, 2008
  5. srp113
    Replies:
    3
    Views:
    470
Loading...

Share This Page