We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,716 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

C++ Push to stack using assignment operator?

I want to push a value to a stack using the assignment operator, I need some help with this.. Thanks

Stack s;
float x = 5;

s = x;

class Stack
{
public:
Stack();
float push(float value);
float pop();
int Size();
float operator = (const float &right);
private:
float values[30];
int top;
int size;
};



float Stack::push(float value)
{
top++;
size++;
values[top] = value;
return value;
}

float Stack::pop()
{
size--;
return values[top--];
}

int Stack::Size()
{
return size;
}

float Stack::operator= (const float &right)
{
float temp_val = right;
Stack temp;
return temp.push(temp_val);
}
5
Contributors
5
Replies
13 Hours
Discussion Span
1 Year Ago
Last Updated
6
Views
bobanderson90
Newbie Poster
3 posts since May 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

In operator= (lines 41 - 46) you create a temporary stack which gets deleted as soon as the function exits. This is surely not what you intend, I imagine you want to push onto the current stack don't you?

You are already in side a member of stack so you can just use the this pointer to access the current object to execute the push operation return this->push(right);

Banfa
Practically a Master Poster
695 posts since Mar 2010
Reputation Points: 508
Solved Threads: 109
Skill Endorsements: 5

Ahh yes, I completely forgot about the "this" keyword. Thank you so much.

bobanderson90
Newbie Poster
3 posts since May 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

ok one more question. What if i want to do it the other way around. Pop a value into a float varialble.

Stack s;
float x;
x = s;

this is what i have so far..

float Stack::operator= (const Stack &right)
{
    return right.pop();
}
bobanderson90
Newbie Poster
3 posts since May 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

Your problem here is that you are assigning to a float variable

float f;
Stack MyStack;

// Code to populate stack

f = MyStack;

The left hand side is a float but it is the left hand side that determines the class used when calling a member function; clearly you can not set the operator= on the float class, it is built-in and not a class.

Another way to work out that there is a problem is to look what your current method works on, it operates on the current object this and another Stack object which is passed in, that is 2 Stack object which is not what you are after.

The answer is not to use a method at all but rather to use a helper function external to the class, something like this

float operator=(float& left, const Stack& right)
{
    left = right.pop();
    return left;
}

For symetry you could also implement you previous assignment operator as a function rather than a method, noting it is normal to return what was assigned to from the assignment operator

const Stack& operator=(Stack& left, const float& right)
{
    left.push(right);
    return left;
}
Banfa
Practically a Master Poster
695 posts since Mar 2010
Reputation Points: 508
Solved Threads: 109
Skill Endorsements: 5

Correction on Banfa's last post. You cannot make the assignment operator into a free function, it must be a class member function. So, Banfa's code will not work, although he is very correct in suggesting the free function operator overloading instead of as class member functions, this is generally preferrable (for the operators that are allowed to be overloaded as free functions).

I have a problem with your use of the assignment operator for push-pop operations. The problem is that you shouldn't drastically change the semantics of an operator when creating an overloaded version of it. All programmers generally see the assignment operation as exactly that, an assignment of the contents of the right-hand-side to the variable on the left-hand-side. Overloading it to mean pushing a value on the left-hand-side stack is a bad and confusing idea.

If you absolutely need to use a set of operators for push-pop (btw, what's the problem with just having a "push" and "pop" function?), then you should use the << and >> operators instead (with semantics and chaining behaviour that mimics the iostreams). And those are allowed to be free functions, so you can do this:

Stack& operator<<(Stack& left, float right) {
  left.push(right);
  return left;
};

Stack& operator>>(Stack& left, float& right) {
  right = left.pop();
  return left;
};

This will allow you to create code like this:

int main() {
  float values[] = {0.0,1.0,2.0,3.0};
  for(int i = 0; i < 4; ++i)
    std::cout << values[i] << " ";
  std::cout << std::endl;

  // inverting the array by stacking them one way and popping them the other way:
  Stack s;
  s << values[0] << values[1] << values[2] << values[3];
  s >> values[0] >> values[1] >> values[2] >> values[3];

  for(int i = 0; i < 4; ++i)
    std::cout << values[i] << " ";
  std::cout << std::endl;
  return 0;
};
mike_2000_17
21st Century Viking
Moderator
3,135 posts since Jul 2010
Reputation Points: 2,050
Solved Threads: 625
Skill Endorsements: 41

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.1039 seconds using 2.71MB