copy constuctor not called?

Started by
3 comments, last by MaulingMonkey 19 years, 7 months ago
i have the following template function as part of my "array" tamplate:

inline unsigned int Insert(T& object)
	{
		if (size >= capacity)
			SetCapacity(capacity * 2);

		array[size] = object;
		return ++size;
	}

for come reason, the copy constuctor isnt being called during the assignment? copy constructor definition for the object being copied is as follows: Pass(Pass& copy) { //copy code here, not being called?! }
Advertisement
That's because you're assigning/evaluating instead of initializing.

operator= takes care of statements like:

MyClass myClass;
myClass = myOtherClass;

constructors take care of statements like:

MyClass myClass = myOtherClass; //the one exception
(MyClass) someStatement; //casting
MyClass myClass; //decleration
MyClass myClass( 4 ); //decleration
MyClass myClass( myOtherClass ); //decleration

Also, use std::vector. It does this, it expands by a power of 2 like you have. You don't need this. You also havn't checked to see if capacity == 0, in which case your new SetCapacity call will NOT allocate enough memory. Also, your SetCapacity function should be the one expanding capacity exponentially, not the calling code!!!:

void SetCapacity( size_t n ){   size_t new_capacity;   if ( n < capacity ) return;   if ( n > capacity * 2 ) new_capacity = n;   else new_capacity = capacity * 2;   //...implementation....}


I also completely fail to see why you'd need insert to return the size of the array in question - it will slow your code down (albiet slightly) when you don't need that value, and simply calling MyVector->size(); will return the new size anyways (most likely with no more of a performance hit than having insert return it directly). std::vector's insert does not return anything either.

Also, you may want to make Insert accept a CONST reference, like so:

Insert( const T & object )


which will allow you to pass by value/const. On your current code, this will fail:

CMyArray< int > MyArray;MyArray.Insert( 5 ); //ERROR: 5 is a const, cannot convert to non-const reference


whereas it will compile with the redefinition.

Also, your compiler is smart enough to decide for itself if a function should be inlined (or if it's not, it should be replaced). Inlining too large a function will result in a performance LOSS (due to your system having to load more data from memory into the processor cache, instead of just using what it allready has) and increased EXE size.

edit: edited quite a few times, to point out the numerous flaws, which underscore exactly why people should use std::vector - because it's been done, and it's been done RIGHT (er than those who don't allready have their own clone classes completely debugged).

To finally answer what you should use instead of:

array[size] = object;


Assuming array has simply been malloced or realloced, the correct way to initialize this point of memory would be placement new, which is implemented as so:

#include <new> //do this at the topnew (array+size) T( object ); //replace the line with this//also, the same thing:  new (& array[size]) T( object );


If you've never seen that syntax before, it is yet another hint that you really shouldn't create your own container.

[Edited by - MaulingMonkey on September 20, 2004 12:44:27 AM]
The array of objects is created beforehand, so class assignment operator gets called.

Kuphryn
Quote:Original post by MaulingMonkey
operator= takes care of statements like:

MyClass myClass = myOtherClass;

This statement is an exception. It's handled by the copy constructor, because you're initializing the variable on definition. This would use the assignment operator:
MyClass myClass;
myClass = myOtherClass;

Quote:Original post by Coder
Quote:Original post by MaulingMonkey
operator= takes care of statements like:

MyClass myClass = myOtherClass;

This statement is an exception. It's handled by the copy constructor, because you're initializing the variable on definition. This would use the assignment operator:
MyClass myClass;
myClass = myOtherClass;


Doh! Post modified.

This topic is closed to new replies.

Advertisement