views:

80

answers:

5

I dont mean a variable in a class but a default value for the class as a whole.

struct Scalar {
    unsigned int value;

    void toThePowerOf(int power);
    // etc.
}

I'd like to be able to do something like

Scaler foo;
foo.value = 2;
foo.toThePowerOf(2);

std::cout << foo << std::endl; // Outputs 4!

Is that possible in C++?

A: 

No, you cannot but you can overload operator << for your class and ostream to get the desired effect

std::ostream& operator << (std::ostream& out, const Scaler& foo)
{
   return out << foo.value;
}

Your code will now work and produce the desires result

Armen Tsirunyan
+2  A: 

No, classes do not have a value in the way you're thinking.

What you probably mean to do is to overload the << operator:

ostream& operator<<(ostream& output, const Scalar& val)
{
  output << val.value;
  return output;
}
Sydius
-1: your code results in infinite recursion :)
Armen Tsirunyan
Hasty typo; already fixed.
Sydius
Removed the downvote
Armen Tsirunyan
I take it ostream should be friended?
PhilCK
@PhilCK: no. All we're doing is calling the version of `operator<<` that `ostream` has for the type of `val.value` (in this case `unsigned int`). It doesn't need to be a friend of us, and we don't need to be a friend of it, because neither of us is accessing anything private in the other.
Steve Jessop
A: 

Yes. it is possible. Just initialize all the values in the constructor of the class. Use class instead of struct.

Alex James
Using struct is fine, even preferred by some.
Roger Pate
A: 

Use a default value in the ctor. Make the ctor explicit if you don't want implicit conversions.

struct Scalar {
  unsigned int value;

  Scalar(int value=0) : value (value) {}

  void toThePowerOf(int power) {
    // naive implementation just for show
    int new_value = 1;
    assert(power >= 0);  // or other error checking
    for (; power > 0; --power) {
      new_value *= value;
    }
    value = new_value;
  }

  friend std::ostream& operator<<(std::ostream &out, Scalar const &x) {
    out << x.value;
    return out;
  }
};
Roger Pate
+1  A: 

I meant a default value for the class, so if you called that object by just its name foo it would return foo.value by default.

It is actually possible to define an implicit conversion from Scalar to int:

struct Scalar
{
    unsigned int value;

    operator int() const
    {
        return value;
    }
};

int main()
{
    Scalar foo = {2};
    std::cout << foo << std::endl;
}

But implicit conversions are generally frowned upon in the C++ community, because it can make code quite hard to read. (I guess this is why noone mentioned conversion operators yet.)

FredOverflow
ooo looks interesting I'll give it shot.
PhilCK