1

I'm playing with operator overloading in C++, specifically the assignment operator "=".

So, at a time, I'm able to do this:

MyClass var1;
var1 = "string";

But, it gives me an error when I try to do this:

MyClass var2 = "string";

Somebody knows why? And how can I make it possible?

  • Why the upvotes, it was asked like thousand times before... – Griwes Jun 10 '12 at 21:02
  • 3
    You should look at the [FAQ on operator overloading.](http://stackoverflow.com/q/4421706/677667) – Chris A. Jun 10 '12 at 21:02
  • 1
    @Griwes, It's sort of a tie between research and a clear, concise question. – chris Jun 10 '12 at 21:05
  • 1
    @chris, this question shows that OP did NO research on his own, and did NOT use Google before asking. That makes him deserve no upvote. – Griwes Jun 10 '12 at 21:17
  • 1
    possible duplicate of [Assignment vs Initialization in C++](http://stackoverflow.com/questions/2303087/assignment-vs-initialization-in-c) – jscs Jun 10 '12 at 21:19
  • @Griwes Yes, I searched on a lot of places before posting here. The fact is it wasn't clear for me looking only at the results. The people here helped me and I'm grateful for it. – Igor Borges Jun 10 '12 at 21:22
  • It should be also described in your C++ book. – Griwes Jun 10 '12 at 21:27
  • @Griwes, "It is clear" - Check. "show research effort" - your point holds in the opposite direction. Whichever people felt won influenced their vote. I do agree with both sides in those regards. It just boils down to personal opinion when ultimately voting for or against. – chris Jun 10 '12 at 21:28
  • @chris, notice I wrote "why the upvotes", not "why don't you all downvote this" - I think that makes difference, doesn't it? – Griwes Jun 10 '12 at 21:29
  • wow, I didn't know a simple question would have that kind repercussion about upvotes and things. I'm new here, didn't want to hurt anyone. – Igor Borges Jun 10 '12 at 21:33
  • @Igor Borges: Invest in *Effective C++* and *More Effective C++* books by Scott Meyers. He goes into a more detailed explanation. – Thomas Matthews Jun 11 '12 at 02:24

2 Answers2

5

The second example isn't calling operator=, it's calling a conversion constructor for const char [], or whatever you'd be using it for internally, as long as it can convert from that (e.g. std::string), which doesn't exist as of yet. You can see one implemented in std''OrgnlDave's answer. It's almost identical to

MyClass var2 ("string");

The latter, though, is explicit, whereas the former is implicit. To see the difference, make a constructor and mark it explicit. The code here will work, but yours won't. This can save confusion when you, for example, pass a string by accident instead of a MyClass, and it gets implicitly converted when it isn't even meant to be a MyClass in the first place.

chris
  • 60,560
  • 13
  • 143
  • 205
  • +1 I believe this is the first answer _(as in not a comment)_ I see from you ;) – K-ballo Jun 10 '12 at 21:06
  • Thanks! I did think would call the operator= first, then the constructor. – Igor Borges Jun 10 '12 at 21:11
  • @IgorBorges, It is a bit of a misleading statement, seeing as how it resembles an assignment. – chris Jun 10 '12 at 21:12
  • I'm confused why this was accepted, you didn't make it possible for him to do MyClass var2 = "string"; like he asked – std''OrgnlDave Jun 10 '12 at 21:20
  • @std''OrgnlDave, I did mention the conversion constructor, which is the key component. I didn't put the code in for it because it was already present in your answer. I explained the difference between using an implicit vs. explicit conversion. – chris Jun 10 '12 at 21:21
  • Ah, a co-answer. Usually people say "look at OrgnlDave's answer for how to make the conversion constructor" or some such :-P – std''OrgnlDave Jun 10 '12 at 21:22
  • @std''OrgnlDave, That's true. I didn't want to leech your answer into mine, as it's kind of rude. Putting the necessary code did elicit an upvote from me. I stuck in a reference now. – chris Jun 10 '12 at 21:24
  • I knew the code I needed, but didn't know the theory behind it. – Igor Borges Jun 10 '12 at 21:26
  • 1
    @IgorBorges, It's not just you. Other people may very well find this and it will help them too. They might not know the code. Also, it certainly does make a more fulfilling answer than the, albeit still correct in identifying the problem and solution, one line of explanation in the other answer. Code is a backup that speaks for itself. – chris Jun 10 '12 at 21:31
  • 2
    uhm... *it's calling a conversion constructor for `std::string`* I wonder where `std::string` is in the whole question... – David Rodríguez - dribeas Jun 10 '12 at 21:47
  • @DavidRodríguez-dribeas, Wow! That's really sad. I'll edit it. – chris Jun 10 '12 at 22:01
3

You need to make a constructor for your class, the second example is calling the constructor.

class MyClass {
  public:
    MyClass(const std::string& what) {  } // copy string
};
std''OrgnlDave
  • 3,912
  • 1
  • 25
  • 34
  • That's a conversion constructor. You should make it `explicit` – Captain Obvlious Jun 10 '12 at 21:06
  • @ChetSimpson That's the *point*. He wants a conversion constructor. If I made it explicit he wouldn't be able to use = on it. – std''OrgnlDave Jun 10 '12 at 21:06
  • Still, `explicit` should be a default unless you have a reason not to. I don't see a problem with using `MyClass var2 ("string");` to possibly save confusion later. – chris Jun 10 '12 at 21:08
  • @std''OrgnlDave This allows an implicit conversion to occur everywhere, not just with the assignment. That may not be the behavior he is looking for (I hope it's not). – Captain Obvlious Jun 10 '12 at 21:09
  • @std''OrgnlDave Not to mention that won't even work with rvalues. const `what`? – Captain Obvlious Jun 10 '12 at 21:11
  • @chris I didn't pay attention either ;) – Captain Obvlious Jun 10 '12 at 21:17
  • @chris his reason not to make it explicit is because he wants to know how to do MyClass var2 = "string"; not how to make it explicit and call MyClass var2("String"); – std''OrgnlDave Jun 10 '12 at 21:19
  • @std''OrgnlDave, It's just a good practice to do it. There's no harm in explaining the benefits of it. The OP is still free to choose, but at least the knowledge of the other side is there to make an appropriate decision. – chris Jun 10 '12 at 21:22
  • @std''OrgnlDave The behavior of the conversion constructor extends beyond the OP's query about the assignment. I think it should at least be noted. – Captain Obvlious Jun 10 '12 at 21:24
  • Another answer that mentions std::string. Where do you see std::string in the question? There is no std::string, there is a MyClass, and a string literal (const char [N]) which in most cases will decay to const char* when matching a constructor... but no std::string in the code there... – David Rodríguez - dribeas Jun 10 '12 at 21:57