rvalue / lvalue operator[]

  • Thread starter Gonzalo Aguirre
  • Start date
G

Gonzalo Aguirre

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
 
R

Ron Natalie

Gonzalo Aguirre said:
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.
 
V

Victor Bazarov

Gonzalo Aguirre said:
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...

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
 
G

Gonzalo Aguirre

i have three classes

element ------- said:
/************ 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;
}
 
R

Ron Natalie

Gonzalo Aguirre said:
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.
 

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

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top