I have some questions, some might seem kind of dumb, but we're studying overloading operators and inheritance/polymorphing. I'm having trouble finding some references on some things to show me how to do this stuff or why I'm doing it a certain way.

what kind/type of binary operators can I use if I want the left operand to be a primitive type and the right operand to be a class? I've been through three books and several websites, I cannot find a direct reference to this

When I'm writing an inherited class (one of my current labs) is the constructor inherited from the base class or will I need to do something in the derived class for it?

In this same lab, it says to use a temporary variable when I use an operator that returns it's own class type. Is there a good reason for this other than she just wants it that way?

Again in this inheritance lab, I'm trying to derive several classes, can I derive them from the same base or will I need to make one of my derived classes a "base" class for the rest?

I'm also having some trouble with my vector class, but I don't have enough code to post yet, I'm still trying to understand the "container" concept.

Thanks for anyone's input on these, even if it's a reference I can look up myself, I just want to know because these seem to be two very big issues with classes that I'm having trouble wrapping my head around

Recommended Answers

All 3 Replies

> what kind/type of binary operators can I use if I want the left operand to be a primitive type and the right operand to be a class?
Any binary operator that you can overload will support this, but you have to make sure that it's a non-member function so you have control over both operands:

#include <iostream>
#include <ostream>

class Foo {
  int _base;
public:
  Foo(int base): _base(base) {}

  friend std::ostream& operator<<(std::ostream& os, const Foo& foo);
  friend Foo operator+(int lhs, const Foo& rhs);
};

std::ostream& operator<<(std::ostream& os, const Foo& foo)
{
  return os << foo._base;
}

Foo operator+(int lhs, const Foo& rhs)
{
  Foo result(lhs);

  result._base += rhs._base;

  return result;
}

int main()
{
  Foo test(10);

  test = 10 + test;

  std::cout << test << '\n';
}

> is the constructor inherited from the base class
Constructors are not inherited.

> Is there a good reason for this other than she just wants it that way?
If she means that you should be returning a copy, that makes sense. The usual advice when overloading operators is to do as the ints do. If your class follows the same rules as int for the operator, all is well. That means returning a copy most of the time. But a temporary isn't always needed depending on how you do the work in the operator.

> can I derive them from the same base or will I need to make one of my derived classes a "base" class for the rest?
Inheritance isn't really restricted in what you can do. Either of those solutions would work, but it's generally better practice to keep your hierarchies wide and short (the first one) than thin and deep (the second). It's easier to wrap your head around wide and short. ;)

So, to follow up and make sure I understand:

I can derive from the same base class more than once

Constructors are not inherited

On overloading you say non member function, and I see that you used "friend" in the class, so by non member you mean to either use a free standing function that calls the class getter or use friend in order to manipulate it the way I want to outside of the class?

On the temporary variable, you say if I'm returning a copy that makes sense. I'm missing the link between it returning it's own class type and returning a copy and why that's important

> I can derive from the same base class more than once
Right.

> Constructors are not inherited
Also right.

> by non member you mean to either use a free standing function that calls the class getter or use friend
Exactly. :)

> I'm missing the link between it returning it's own class type and returning a copy and why that's important
Logical temporaries are all over the place. In Ed's operator+, result is an explicit temporary. There's also an implicit temporary when the copy constructor is called to copy result into the return value. Because operator+ returns Foo and not Foo& or Foo*, a copy has to be made.

As for why it's important, just remember the ints. :) If you have two ints and you add them using the + operator, you get a third int. A copy of the result is made and the two original ints don't change their value. You can think of the temporary and the copy as being the same thing if it makes the concept simpler.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.