Special class

Pages: 12
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
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
That is creating a new instance.
The word you he used was "declaration", not "creation", which is why I was confused.
Last edited on
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.
You can call the functor operator, though.
True, but I'm not sure that's something that is going to help someone posting in the Beginners' forum ;)
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
Topic archived. No new replies allowed.
Pages: 12