this returned destructor called...

This is a discussion on this returned destructor called... within the C++ Programming forums, part of the General Programming Boards category; Maybe do you absolutely have to have an example of the equal operator overloading. If so, then create a reference ...

  1. #16
    lyx
    lyx is offline
    Amateur
    Join Date
    Sep 2003
    Posts
    228
    Maybe do you absolutely have to have an example of the equal operator overloading. If so, then create a reference class and overload his = operator instead of doing so in the array class.

  2. #17
    Normal vector Carlos's Avatar
    Join Date
    Sep 2001
    Location
    Budapest
    Posts
    463
    Originally posted by FillYourBrain
    the return value isn't even used in this example:

    anArray[ index ] = value;


    so I have difficulty following you. returning *this accomplished nothing here.

    Not quite, maybe I could not make myself clear enough.

    The object MUST be of MyArray type, as operator = is redefined in this class. If operator [] would return an int, then operator = defined for int type would be called, not the one I WANTED to.

    Just try out both cases, you'll see what happens.

  3. #18
    lyx
    lyx is offline
    Amateur
    Join Date
    Sep 2003
    Posts
    228
    What about my suggestion, why not write your *own* int reference class then?

  4. #19
    Normal vector Carlos's Avatar
    Join Date
    Sep 2001
    Location
    Budapest
    Posts
    463
    Originally posted by lyx
    Maybe do you absolutely have to have an example of the equal operator overloading. If so, then create a reference class and overload his = operator instead of doing so in the array class.
    The example presents also an alternative way of handling some situations which can cause errors like over indexing.

  5. #20
    lyx
    lyx is offline
    Amateur
    Join Date
    Sep 2003
    Posts
    228
    Yes, but this is the task of your array class which returns a int reference class which task is to overload the = operator, and nothing more. Is that right with you?

  6. #21
    Normal vector Carlos's Avatar
    Join Date
    Sep 2001
    Location
    Budapest
    Posts
    463
    Originally posted by lyx
    What about my suggestion, why not write your *own* int reference class then?
    I'm planning to extend MyArray class to template class.

    Or should I write another template class for operator = for all the primary types (char, int, etc.) ?
    I rather live with this small inconvenience - that assignements like int x = anArray[ index ] won't be supported / compiled.

    I might change my mind
    Last edited by Carlos; 09-24-2003 at 09:53 AM.

  7. #22
    lyx
    lyx is offline
    Amateur
    Join Date
    Sep 2003
    Posts
    228
    Then write a template for the element class as well.

    [EDIT]Don't answer to my older posts, just to the newest ones.

  8. #23
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    wait, wait, wait. I see where the confusion lies. First of all..

    anArray[ index ] = value;

    is to set an "int" inside the array. you are supposed to return a reference to an int in operator[]. This would set the thing with value.

    I misunderstood but the example is still wrong. It makes no sense to return a *this because that's not what the array contains. You can simply return a reference to the corrected int instead. operator= on the array class doesn't make sense as a solution.

    in addition, setting the last element as an alternative to the incorrect one they asked for is not safe as it would overwrite their valid data.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  9. #24
    lyx
    lyx is offline
    Amateur
    Join Date
    Sep 2003
    Posts
    228
    But FYB (could I call you that? ^^), he *wants* to overload a = operator...

  10. #25
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    but...

    myArray = 4;

    what does that mean? When you set an array equal to 4. what is that? Overloading operators is there to make things more readable, not more difficult to understand. really, it'll work that way (starts to become a state engine for no reason though) but he's writing a chapter in a book on operator overloading. It's incorrect to make an equals operator that makes no logical sense like that. that's all I'm saying. Also, it's more correct to return a reference to the thing you're actually expecting.

    what would this give you?

    int i = myArray[3];

    it would return a myArray reference? not terribly helpful if you ask me!
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  11. #26
    lyx
    lyx is offline
    Amateur
    Join Date
    Sep 2003
    Posts
    228
    Read all what have been posted, I said that I agree with you, however, if he really want that much his overloaded = operator, he should make a reference class. And that if he wants a template class, he should make both templates, that's all.

  12. #27
    Normal vector Carlos's Avatar
    Join Date
    Sep 2001
    Location
    Budapest
    Posts
    463
    Originally posted by FillYourBrain
    wait, wait, wait. I see where the confusion lies. First of all..

    anArray[ index ] = value;

    is to set an "int" inside the array. you are supposed to return a reference to an int in operator[]. This would set the thing with value.

    I misunderstood but the example is still wrong. It makes no sense to return a *this because that's not what the array contains. You can simply return a reference to the corrected int instead. operator= on the array class doesn't make sense as a solution.
    Ok, FillYourBrain, I got it. There's no need to overload operator =...
    Code:
    int& MyArray::operator [] ( int index_in )
    {
    	// range check
    	if ( index_in > myLastIndex )
    	{
    		myIndex = myLastIndex;
    	}
    	else
    	{
    		myIndex = index_in;		
    	}
    	return myArray[myIndex];
    }
    in addition, setting the last element as an alternative to the incorrect one they asked for is not safe as it would overwrite their valid data.
    Of course, it's just a temporary workaround, I planned to do some error handling and logging here.

  13. #28
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    yeah, carlos. Sorry I misunderstood you for about a whole page. The proposal that you were making was strange and unexpected so I went off in another direction.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  14. #29
    lyx
    lyx is offline
    Amateur
    Join Date
    Sep 2003
    Posts
    228
    All those words for finally you accepting that the operator isn't needed...

    The object MUST be of MyArray type, as operator = is redefined in this class. If operator [] would return an int, then operator = defined for int type would be called, not the one I WANTED to.
    I believed you WANTED to write your own operator, didn't you?

  15. #30
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    he could certainly justify an operator=(MyArray & arr). It makes sense to set an array equal to an array. That would be a good example. Not that Carlos is looking for examples from me
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

Page 2 of 4 FirstFirst 1234 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What exactly is iterator returned by .end()?
    By pheres in forum C++ Programming
    Replies: 21
    Last Post: 12-09-2008, 09:50 AM
  2. Replies: 12
    Last Post: 10-16-2008, 03:49 PM
  3. How can the focus be returned to previous window?
    By wow in forum Windows Programming
    Replies: 5
    Last Post: 02-29-2008, 03:00 PM
  4. cannot find -lpcap collect2: ld returned 1 exit
    By nasim751 in forum C Programming
    Replies: 0
    Last Post: 02-12-2008, 12:37 AM
  5. Varible is different once returned.
    By epoch in forum C++ Programming
    Replies: 2
    Last Post: 01-04-2003, 02:45 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21