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; Originally posted by lyx All those words for finally you accepting that the operator isn't needed... I believed you WANTED ...

  1. #31
    Normal vector Carlos's Avatar
    Join Date
    Sep 2001
    Location
    Budapest
    Posts
    463
    Originally posted by lyx
    All those words for finally you accepting that the operator isn't needed...
    I believed you WANTED to write your own operator, didn't you?
    To correct myself: the operator = is not needed for that kind of assignements. However, I will surely redefine it, for cases like:

    1.) anArray = value;

    Originally posted by FillYourBrain
    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
    [/B]
    Thanks FYB!

    2.) anArray = anotherArray;

    etc...

  2. #32
    lyx
    lyx is offline
    Amateur
    Join Date
    Sep 2003
    Posts
    228
    Not that but, we were saying since the beginning that there were no need for such a redefinition. Remember, you said:
    myArray[num]

    should return a reference to the type stored in the array. NOT the array class itself.
    And I agreed.

    I feel like I wasted my time for something that wasn't worth it, that's all... A problem that did even never exist for someone who claims to need what he doesn't need. At least, one must know what he wants to ask when submitting a question.

  3. #33
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    lyx, take it easy man. no big deal here. don't get stressed. not good for the heart
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  4. #34
    Normal vector Carlos's Avatar
    Join Date
    Sep 2001
    Location
    Budapest
    Posts
    463
    Originally posted by lyx
    I feel like I wasted my time for something that wasn't worth it, that's all... A problem that did even never exist for someone who claims to need what he doesn't need. At least, one must know what he wants to ask when submitting a question.
    Look, the way I first made my class was redundant, and FillYourBrain lead me to the correct solution.


    P.s.
    Lyx, sorry if I caused you such a great problem, headache, maybe nearly brought you to suicide
    You just can ignore the topics and don't bother yourself if you are such a sensitive person.

    The problem is that things get clear while discussing it. To discuss something you need people who are able to DISCUSS, real team-players. If you don't feel like this, you will probably cause conflicts here in the (near) future.

    Sometomes people ask silly things, not only the newbies. Nobody is perfect.

  5. #35
    Normal vector Carlos's Avatar
    Join Date
    Sep 2001
    Location
    Budapest
    Posts
    463
    I extended my Class to dinamically allocate memory in case of over-indexing.
    Code:
    int& MyArray::operator [] ( unsigned int index_in )
    {
    	// range check
    	if ( index_in > myLastIndex )
    	{
    		unsigned int anArraySize;
    		// when the range is exceeded, elements of  myArray are copied to a temporary array
    		// myArray is deleted, then a new myArray of size index_in is created, and the previously saved
    		// data are restored
                                    // MAX_ARRAY_SIZE_VALUE can be set to infinite, error handling WILL be introduced if call to new(MAX_ARRAY_SIZE_VALUE) fails
    		if( index_in == 0 
    			|| index_in >= MAX_ARRAY_SIZE_VALUE )
    		{
    			anArraySize = MAX_ARRAY_SIZE_VALUE;
    			index_in = MAX_ARRAY_SIZE_VALUE - 1;
    		}
    		else
    		{
    			anArraySize = index_in + 1;
    		}
    		// create temp array
    		int* aTempArray = new ( int[myLastIndex + 1] );
    		// store myArray data
    		for( unsigned int i = 0; i <= myLastIndex; i++ )
    		{
    			aTempArray[i] = myArray[i];
    		}
    		// delete myArray
    		delete[] myArray;		
    		// create the new myArray (size increased), error handling will be introduced!
    		myArray = new ( int[anArraySize] );				
    		for( i = 0; i <= myLastIndex; i++ )
    		{
    			myArray[i] = aTempArray[i];
    		}
    		// initialize the rest
    		for( i = myLastIndex + 1; i <= index_in; i++ )
    		{
    			myArray[i] = 0;
    		}
    
    		myLastIndex = index_in;
    		myIndex = index_in;
    		delete[] aTempArray;
    	}
    	else
    	{
    		myIndex = index_in;		
    	}
    	return myArray[myIndex];
    }

  6. #36
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    how do you feel about memcpy instead of that copying for-loop?
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  7. #37
    Normal vector Carlos's Avatar
    Join Date
    Sep 2001
    Location
    Budapest
    Posts
    463
    Originally posted by FillYourBrain
    how do you feel about memcpy instead of that copying for-loop?
    I thought of that, but I tried to build first an easy-to-understand example. Later it will be optimized and error handling will be also introduced.
    Anyway thanks for the idea!

  8. #38
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    that's kinda what I thought.

    This is an interesting type of class. If you haven't already, you should take a look at the code for the STL vector. Its actually really fun how they use buffer copies, copy constructors and placement new to give it some speed. Such an exploration could be beneficial in your book.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  9. #39
    Normal vector Carlos's Avatar
    Join Date
    Sep 2001
    Location
    Budapest
    Posts
    463
    Thank you FillYourBrain!
    I really appreciate your help.

    By the way, STL will be also discussed in my book, originally I planned just a basic introduction, but you made me realize that I could eventually compare STL containers with some self-made classes.

  10. #40
    lyx
    lyx is offline
    Amateur
    Join Date
    Sep 2003
    Posts
    228
    First, I will talk about the code, then I will take out your comments.

    You're wasting execution time by allocating an extra array. aTempArray is completely useless the way you use it.

    Instead of:
    Code:
    allocate temporary array (old size)
    copy array into temporary array
    free array
    allocate array (new size)
    copy temporary array into array
    free temporary array
    You should be doing:
    Code:
    allocate temporary array (new size)
    copy array into temporary array
    free array
    assign temporary array to array
    Another thing is that you should put
    Code:
    myIndex = index_in;
    out of the conditionnal branches because it is done either way.

    And, you shouldn't be using myIndex as the index right now because myIndex has the value of index_in, so instead of using a member variable, use a local one.

    Lastly, the
    Code:
    if (index_in == 0)
    makes no sense as you use unsigned indexes and we have index_in > myLastIndex; thus, index_in cannot be equal to zero, even if myLastIndex's value were zero, index_in would be greater than zero.

    I thought of that, but I tried to build first an easy-to-understand example. Later it will be optimized and error handling will be also introduced.
    It sounds quite funny, you are teaching one of the most difficult point in the C++ language, though you bother with your students not understanding memcpy()?

    Lyx, sorry if I caused you such a great problem, headache, maybe nearly brought you to suicide
    You just can ignore the topics and don't bother yourself if you are such a sensitive person.
    Just don't make fun of me with your suicide thing, I knew someone who killed himself while ago and there's nothing to laugh about, even if I am only 15 and you are older and know much more than I do, it does not give you the right to play with me.

    The problem is that things get clear while discussing it. To discuss something you need people who are able to DISCUSS, real team-players. If you don't feel like this, you will probably cause conflicts here in the (near) future.
    Sorry but I discuss of the solutions and not of the problem, it is your task to know what you want us to solve. Besides, if you want me to leave, I will do, once I make sure that I cannot give to this place anything but ruination.

    lyx, take it easy man. no big deal here. don't get stressed. not good for the heart
    Thanks for your regards but it has been while since I forgot the true meaning of that. ^^

    [edit]Corrected some grammar mistakes.
    Last edited by lyx; 09-25-2003 at 12:10 PM.

  11. #41
    Normal vector Carlos's Avatar
    Join Date
    Sep 2001
    Location
    Budapest
    Posts
    463
    lyx, thank for your help, I made the code in a hurry, and it needed some optimization indeed.

    On the other side, I didn't try to make fun of you, just felt *really* offended by you. Maybe you had a bad day, I don't know, but cannot accept the "I_helped_you_therefore_I_am_authorized_to_kick_yo ur_head_off" style. Sorry!

  12. #42
    lyx
    lyx is offline
    Amateur
    Join Date
    Sep 2003
    Posts
    228
    On the other side, I didn't try to make fun of you, just felt *really* offended by you. Maybe you had a bad day, I don't know, but cannot accept the " I_helped_you_therefore_I_am_authorized_to_kick_you
    r_head_off" style. Sorry!
    In fact, I felt really irritated by you changing your mind that way after going that far into the problem, I just thought you would have done it sooner if you wanted. Besides, the "not_a_thank_to_those_who_helped_me_that_far" style isn't better, is it?
    Anyway, I could say that I'm sorry.
    Consider we are quits.

  13. #43
    Normal vector Carlos's Avatar
    Join Date
    Sep 2001
    Location
    Budapest
    Posts
    463
    Originally posted by lyx
    In fact, I felt really irritated by you changing your mind that way after going that far into the problem, I just thought you would have done it sooner if you wanted.
    In fact, I didn't realize that I overcomplicated my class for no reason. So I didn't changed my mind, just corrected the code.
    Anyway, this is how you react to people who dare to change their minds? Wow!

    Besides, the "not_a_thank_to_those_who_helped_me_that_far" style isn't better, is it?
    Have you just re-checked my answers? I just can't thank every answer, we were discussing things. As an alternative I could change my signature to "Thank you very much!", but I won't

    Originally posted by lyx

    Anyway, I could say that I'm sorry.
    Consider we are quits.
    I could say, too...

  14. #44
    lyx
    lyx is offline
    Amateur
    Join Date
    Sep 2003
    Posts
    228
    Have you just re-checked my answers? I just can't thank every answer, we were discussing things. As an alternative I could change my signature to "Thank you very much!", but I won't
    Yes, I checked them all again. ^^
    Yet, I don't ask for a thank for each post, only once everything is over. Besides, it was just a "plus" to make me irritated.

    Anyway, this is how you react to people who dare to change their minds? Wow!
    After an hour trying to solve the old problem, yes. ^^ It wouldn't had been so if you did sooner.
    Actually I'm not a very patient person and making me look for solutions to finaly take the first one doesn't do good. ^^

  15. #45
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    wow, lyx. You're nuts. I've never seen so much made over nothing.

    Carlos, imagine this guy as a moderator.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

Page 3 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, 08:50 AM
  2. Replies: 12
    Last Post: 10-16-2008, 02: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, 02:00 PM
  4. cannot find -lpcap collect2: ld returned 1 exit
    By nasim751 in forum C Programming
    Replies: 0
    Last Post: 02-11-2008, 11:37 PM
  5. Varible is different once returned.
    By epoch in forum C++ Programming
    Replies: 2
    Last Post: 01-04-2003, 01:45 AM

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