tags:

views:

39

answers:

3

If I change it to void setOutputFormat(ostream& out, int decimal_places), with a call by reference, it works. I don't understand why though? What is the difference between a struct and a class, besides struct members are by default public, and class members are by default private?

+3  A: 

You're right that there is no difference between class and struct, except the default private vs private.

The problem here is that ostream doesn't have a copy constructor, so you can't pass it by value.

ybungalobill
what is a copy constructor? Sorry, I came from c#, and this is kind-of backwards for me
cable729
sbi
+2  A: 

When you attempt to pass the ostream by value, you attempt to make a copy of the stream, which is not valid because stream objects are noncopyable, that is, they do not define a copy constructor. When you pass the stream by reference, however, the function receives a modifiable alias to the ostream instance. Take for instance:

void increment(int n) {
    // Increment local copy of value.
    ++n;
}

int x = 5;
increment(x);
// x is still 5.

Versus:

void increment(int& n) {
    // Increment value itself.
    ++n;
}

int x = 5;
increment(x);
// x is now 6.

So passing the stream by reference is the only way that makes sense, since you want setOutputFormat to modify the original stream in-place. Hope this clarifies the issue somewhat.

Jon Purdy
A: 

As other said, you're trying to create a copy of a noncopyable object (the stream), which results in that error.

In C++ when you pass a var as a parameter, you make a copy of it (opposed to C#, where, for reference types, you're always implicitly passing a reference to it).

By default C++ provides a bitwise copy constructor for every class, but often it's not what is required: think, for example, to a class that owns a resource handle: if you make a perfect clone of an object of that type you'll have two class who think to own such resource, and both will try to destroy it at their destruction, which clearly isn't nice.

Because of this, C++ lets you provide a copy constructor for each class, which is called when a copy of an object has to be created. Since for many objects (streams included) creating copies isn't desired (because it makes no sense, because it's not convenient or because the trouble isn't worth the work) often the copy constructor is disabled (by marking it as private or protected), and you can't create copies of such objects.

Moreover, in general you must be careful with assignments and copies by value with object belonging to complicated class hierarchies, because you may incur in object slicing and other subtle problems. Actually, it's common practice to block copy and assignment in classes intended to be base classes.

The solution, in most cases (including yours) is to pass such objects by reference, thus avoiding making copies at all; see @Jon Purdy's answer for an example.

By the way, often even with copyable objects (e.g. std::strings) it's better to just pass references, to avoid all the work associated with copying; if you're passing a reference just for the sake of efficiency but you don't want to have your object modified, the best solution usually is a const reference.

Copies are also used in some other places in C++; I advise you to have a look at wikipedia page about copy constructors to understand a bit better what's going on, but, over all, to grab a C++ book and read it: C# is different from C++ in a lot of ways, and there are many fake-similarities that may confuse you.

Matteo Italia