Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > rvalue / lvalue operator[]

Reply
Thread Tools

rvalue / lvalue operator[]

 
 
Gonzalo Aguirre
Guest
Posts: n/a
 
      01-02-2004
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: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:op(){
// check code omited
return b[sp];
}


void
stack:ush(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
 
Reply With Quote
 
 
 
 
Ron Natalie
Guest
Posts: n/a
 
      01-02-2004

"Gonzalo Aguirre" <> wrote in message news:bt4422$31tgm$...
> 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.

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      01-02-2004
"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: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:op(){
> // check code omited
> return b[sp];


What happens to 'sp' here? Nothing?

> }
>
>
> void
> stack:ush(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


 
Reply With Quote
 
Gonzalo Aguirre
Guest
Posts: n/a
 
      01-02-2004
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 &
elementperator=(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: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:op(){
> // check if empty
> return v[sp];

sp--;
> }
>
>
> void
> stack:ush(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;
> }
>




 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      01-02-2004

"Gonzalo Aguirre" <> wrote in message news:bt4bgs$37n06$...
> > 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Giving an rvalue ref to a function taking an rvalue ref Juha Nieminen C++ 13 08-29-2012 09:25 PM
operators requiring lvalue/rvalue operands and resulting in rvalue/lvalue Kavya C Programming 9 10-28-2006 01:45 AM
const and non-const operator[] Vs. lvalue and rvalue use Mark Stijnman C++ 2 04-22-2005 02:32 PM
lvalue rvalue Denis Remezov C++ 12 10-18-2004 09:11 AM
++x returns lvalue but x++ return rvalue Chris Mantoulidis C++ 4 12-29-2003 07:00 AM



Advertisments