Special class

Pages: 12
Nov 15, 2013 at 12:24am
you can;t do that.

first:

bob (1, 2, 3) is a constructor call. You can't do that outside of a declaration of a new instance.

2nd:

overload operator=().

I'll give you the prototype:

bob& operator=(const int&, const int&, const int&); (I still am learning some of the nuances of returns, but I don't see any reason why not to return as reference on an operator=())

You can set the function to simply assign the values, or erase all of them and then assign the values (like initializing a new instance).

Another way you could accomplish this is by simply assigning it to a new instance:

bob = bob_class(1, 2, 3)
which would simply initialize a new class, then assign the constructed class to the object bob, and finally destroy the temporary instance.
Last edited on Nov 15, 2013 at 12:24am
Nov 15, 2013 at 12:36am
IWishIKnew wrote:
you can;t do that.

first:

bob (1, 2, 3) is a constructor call. You can't do that outside of a declaration of a new instance.
What do you mean?
http://ideone.com/wwmtp6
Nov 15, 2013 at 1:51am
That is creating a new instance.
Nov 15, 2013 at 2:25am
The word you he used was "declaration", not "creation", which is why I was confused.
Last edited on Nov 15, 2013 at 3:01pm
Nov 15, 2013 at 10:46am
That wasn't me :)

But I suppose if I were being picky about semantics, I might say that when creating a nameless temporary variable like that, you're both declaring and creating in the same statement.

Anyway, IWishIKnew's point was that you can't call a constructor on a variable that's already been constructed, which (as I'm sure you know) is correct.
Nov 15, 2013 at 3:01pm
You can call the functor operator, though.
Nov 15, 2013 at 3:08pm
True, but I'm not sure that's something that is going to help someone posting in the Beginners' forum ;)
Nov 16, 2013 at 4:31pm
if you define it with strong typing (bob is an instance, and bob_class is the class):

explicit bob_class(const int&, const int&, const int&);

then you can not just declare bob(1, 2, 3) anywhere in the code. Implicit conversions can yield unexpected results somtimes, but if you want to use them, then you can do that.

Also, I couldn't tell if bob was the classname or an instance (was not defined by his post). Tha's why I used bob_class.
Last edited on Nov 16, 2013 at 4:33pm
Topic archived. No new replies allowed.
Pages: 12