rvalue / lvalue operator[]

Discussion in 'C++' started by Gonzalo Aguirre, Jan 2, 2004.

  1. i have three classes like this



    class foo{
    attrib_1;
    ..
    ..
    public:
    ..


    };


    /************ BAR ***********/
    class bar{
    class foo *f;
    int position;
    ...
    public:
    foo(int);
    foo & operator[](int);

    };

    foo:foo(int elem){
    f = new foo[elem];
    ...
    }

    foo &
    bar::eek:perator[](int index){
    // check code omited
    return f[index];
    }



    /************** STACK ***********/
    class stack{
    class bar b; //i use bar as a vector
    int sp; //stack pointer
    public:
    stack();
    foo & pop();
    void push(foo &);
    };

    stack::stack{
    b(QUANT); // <--- create a vector of a constant value
    sp = 1;
    }

    foo &
    stack::pop(){
    // check code omited
    return b[sp];
    }


    void
    stack::push(foo & f){
    (2) b[++sp] = f; // <---- operator[] as lvalue ***** (2) *******
    }



    /************* MAIN *************/
    int
    main(void){

    foo f(1,2);
    bar b(5); // <-- vector of 5 elements (see foo:foo(int))
    stack s;

    (1) b[1] = f; // <-- which method invoke? ()


    s.push(f); // <-- this try to call operator[]

    return 0;
    }

    why if i assing in (2) as in (1), or looks like that, they don't call the
    same method??

    thank in advance
     
    Gonzalo Aguirre, Jan 2, 2004
    #1
    1. Advertising

  2. Gonzalo Aguirre

    Ron Natalie Guest

    "Gonzalo Aguirre" <> wrote in message news:bt4422$31tgm$-berlin.de...
    > stack::stack{
    > b(QUANT); // <--- create a vector of a constant value
    > sp = 1;
    > }


    stack::stack : b(QUANT), sp(1) { }
    >


    > (2) b[++sp] = f; // <---- operator[] as lvalue ***** (2) *******


    Fine... vector[] returns a reference to the object in the vector. Once you fix the size code
    to initialize properly things are fine.

    Of course you could avoid using either a stack pointer or presetting the vector size by just
    doing:
    b.push_back(f);
    And then
    b.back() will be the top of the stack.
     
    Ron Natalie, Jan 2, 2004
    #2
    1. Advertising

  3. "Gonzalo Aguirre" <> wrote...
    > i have three classes like this
    >
    >


    You really need to learn to format your source...

    >
    > class foo{
    > attrib_1;


    What is that supposed to be? If it doesn't matter for your
    example, just don't write it.

    > ..
    > ..
    > public:
    > ..
    >
    >
    > };
    >
    >
    > /************ BAR ***********/
    > class bar{
    > class foo *f;


    You can drop the word 'class' here. Just write

    foo *f;

    > int position;
    > ...
    > public:
    > foo(int);
    > foo & operator[](int);
    >
    > };
    >
    > foo:foo(int elem){
    > f = new foo[elem];
    > ...
    > }
    >
    > foo &
    > bar::eek:perator[](int index){
    > // check code omited
    > return f[index];
    > }
    >
    >
    >
    > /************** STACK ***********/
    > class stack{
    > class bar b; //i use bar as a vector
    > int sp; //stack pointer
    > public:
    > stack();
    > foo & pop();
    > void push(foo &);
    > };
    >
    > stack::stack{
    > b(QUANT); // <--- create a vector of a constant value


    What's 'QUANT'? I didn't see it declared anywhere...

    > sp = 1;


    Why not '0'?

    > }
    >
    > foo &
    > stack::pop(){
    > // check code omited
    > return b[sp];


    What happens to 'sp' here? Nothing?

    > }
    >
    >
    > void
    > stack::push(foo & f){
    > (2) b[++sp] = f; // <---- operator[] as lvalue ***** (2) *******


    So, you never use the values 0 and 1 of 'sp'. Why? Shouldn't this
    be

    b[sp++] = f;

    ?

    > }
    >
    >
    >
    > /************* MAIN *************/
    > int
    > main(void){
    >
    > foo f(1,2);
    > bar b(5); // <-- vector of 5 elements (see foo:foo(int))
    > stack s;
    >
    > (1) b[1] = f; // <-- which method invoke? ()


    What do you mean? The only member function you have here is
    the operator[]. It should be invoked.

    >
    >
    > s.push(f); // <-- this try to call operator[]
    >
    > return 0;
    > }
    >
    > why if i assing in (2) as in (1), or looks like that, they don't call the
    > same method??


    What do you mean? They should. In the source code you provided
    there is nothing else to call.

    Perhaps you should read FAQ 5.8.

    Victor
     
    Victor Bazarov, Jan 2, 2004
    #3
  4. Re: rvalue / lvalue operator[] (improved) (solved)

    On Fri, 02 Jan 2004 15:51:31 +0000, Gonzalo Aguirre wrote:

    > i have three classes


    element -------<> vector --------<> stack

    >
    >
    > /************ ELEMENT ***********/
    > class element{
    > attrib_1;
    > ..
    > public:

    element & operator=(element &);
    > ..
    > };


    element &
    element:eek:perator=(element &f){
    if( this != &f )
    {
    att_1 = f.att_1;
    ..
    }
    return *this;
    }

    >
    >
    > /************ VECTOR ***********/
    > class vector{
    > element *e;
    > int dimention;
    > ...
    > public:
    > vector(int);
    > element & operator[](int);
    >
    > };
    >
    > vector::vector(int elem){
    > f = new element[elem];
    > ...
    > }
    >
    > element &
    > vector::eek:perator[](int index){
    > // check code omited
    > return f[index];
    > }
    >
    >
    >
    > /************** STACK ***********/
    > class stack{
    > vector v; //i use vector as a vector
    > int sp; // pointer to next free position
    > public:
    > stack();
    > element & pop();
    > void push(element &);
    > };
    >
    > stack::stack(){ // create an empty stack of 10 elements
    > v (10);
    > sp = 0;
    > }


    stack::stack(): v(10), sp(0){} <-- this solve the problem

    i thought those both were the same.

    >
    > element &
    > stack::pop(){
    > // check if empty
    > return v[sp];

    sp--;
    > }
    >
    >
    > void
    > stack::push(element & f){

    // check if full
    > v[sp] = f;

    sp++;
    > }
    >
    >
    >
    > /************* MAIN *************/
    > int
    > main(void){
    >
    > element f(1,2);
    > vector b(5);
    > stack s;
    >
    > b[1] = f;
    >
    > s.push(f); // (see above)
    >
    > return 0;
    > }
    >
     
    Gonzalo Aguirre, Jan 2, 2004
    #4
  5. Gonzalo Aguirre

    Ron Natalie Guest

    Re: rvalue / lvalue operator[] (improved) (solved)

    "Gonzalo Aguirre" <> wrote in message news:bt4bgs$37n06$-berlin.de...
    > > stack::stack(){ // create an empty stack of 10 elements
    > > v (10);
    > > sp = 0;
    > > }

    >
    > stack::stack(): v(10), sp(0){} <-- this solve the problem
    >
    > i thought those both were the same.
    >


    Please learn to edit your posts.

    No they are not the same. In the first case, the v(10) isn't even valid syntax and sp=0 is assignment
    NOT initialization (not that it makes a whole lot of difference here). If you want to initialize v you have
    to do it in the initializer list.
     
    Ron Natalie, Jan 2, 2004
    #5
    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. Chris Mantoulidis

    ++x returns lvalue but x++ return rvalue

    Chris Mantoulidis, Dec 28, 2003, in forum: C++
    Replies:
    4
    Views:
    500
    Chris Mantoulidis
    Dec 29, 2003
  2. Denis Remezov

    lvalue rvalue

    Denis Remezov, Jul 16, 2004, in forum: C++
    Replies:
    12
    Views:
    869
  3. Mark Stijnman
    Replies:
    2
    Views:
    485
    =?ISO-8859-15?Q?Juli=E1n?= Albo
    Apr 22, 2005
  4. Kavya
    Replies:
    9
    Views:
    515
    Dik T. Winter
    Oct 28, 2006
  5. Juha Nieminen
    Replies:
    13
    Views:
    624
    Edek Pienkowski
    Aug 29, 2012
Loading...

Share This Page