hey mates
We're writing a class "intArray_t" in c++ and we're supposed to forbid operations like:

intArray_t* t = new intArray_t();
intArray_t* t2 = new intArray_t();
//some code here, inserts and so on
t = t2;

and

intArray_t* t = new intArray_t();
//some code here, inserts and so on
intArray_t* t2 = t;

my code is supplied, and as you can see I simply overloaded operator= and the copy constructor in private, but some why I can copy in both ways...

// intArray_t.cpp : Defines the entry point for the application.


#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>

using namespace System;

class intArray_t {
private:
	int** arr;
	int cap, numel;
	void incCap(){
			cap+=16;
			int** newArr = new int*[cap];
			for(int j=0;j<numel;++j){
				newArr[j] = arr[j];
			}
			delete[] arr;
			arr = newArr;
		}

	intArray_t& operator=(const intArray_t& t){Console::Write("no copying\n");return *this;}
	intArray_t(const intArray_t& t){Console::Write("no copying\n");}
public:
	intArray_t(){arr = new int*[16];numel=0;cap=16;}
	intArray_t(const int init){arr = new int*[init];numel=0;cap=init;}
	~intArray_t(){
		remAndDelAll();
		delete[] arr;
	}

	int getNumel()const {return numel;}
	int getCap()const {return cap;}

	
	void insert(int* i){
		if(!cap) incCap();
		arr[numel++] = i;
		}
	

	int* getFirstEl()const {
		if(cap) {int* p = arr[0]; return p;}
		return 0;
	}

	int* getLastEl()const {
		if(cap) {int* p = arr[numel-1]; return p;}
		return 0;
	}
	int* find(const int i)const{
		int* p = 0;
		for (int j = 0; j<cap; ++j)
			if (*arr[j] == i) {p = arr[j]; return p;}
		return 0;}

	
	int* remove(const int i){
		int* p = 0;
		int ind,j;
		for(j=0;j<numel;++j){
			if(i == *arr[j]){
				p = arr[j];
				ind = j;
				break;
			}
		}
		for(j=ind;j<numel-1;++j)	{arr[j] = arr[j+1];}
		arr[j] = 0;
		--numel;
		return p;
	}
	
	void removeAll(){
	for(int j=0;j<numel;++j){arr[j] = 0;}
		numel = 0;	
	}


	void removeAndDelete(const int i){
		int j = 0;
		while(j<numel){
			if(*arr[j]==i){
				delete arr[j];
				for (int k=j; k<numel-1;++k)
					arr[k]=arr[k+1];
				arr[numel--]=0;
			}
			else ++j;
		}
	}

	void remAndDelAll(){
		for(int j=0;j<numel;++j){delete arr[j];}
		numel = 0;
		cap = 0;
	}

	// we don't allow append/prepend on empty lists
	int append(const int ind, int* i){//ind == 3 --> Will be inserted at arr[4]
		if((ind >= numel)||(ind<0))return 0;
		if(numel == cap)incCap();
		int j;
		for(j = numel;j>ind+1;--j){
			arr[j] = arr[j-1];
		}
		arr[j] = i;
		++numel;
		return 1;
	}
	int prepend(const int ind, int* i){
		if((ind > numel)||(ind<=0))return 0;
		return append(ind-1, i);
	}
	
	void print()const{
		for(int j=0;j<numel;++j){
			Console::Write(*arr[j]+ " , ");
		}
		Console::Write("\n");
		Console::WriteLine("number of elements: " + getNumel());
		Console::WriteLine("capacity: " + getCap() + "\n");
	}
};

int main()
{
    intArray_t* t = new intArray_t(0);
	intArray_t* t2 = new intArray_t();
	int* i1 = new int;int* i2 = new int;
	int* i3 = new int;int* i4 = new int;
	int* i5 = new int;int* i6 = new int;
	int* i7 = new int;int* i8 = new int;
	int* i9 = new int;int* i10 = new int;
	int a1,a2,a3,a4,a5,a6,a7,a8,a9,a10;
	
	a1 = 1;a2 = 2;a3 = 3;a4 = 3;
	a5 = 1;a6 = 2;a7 = 8;a8 = 1;
	a9 = 2; a10 =123;
	
	*i1 = a1;*i2 = a2;*i3 = a3;*i4 = a4;
	*i5 = a5;*i6 = a6;*i7 = a7;*i8 = a8;
	
	t->insert(i1);t->insert(i2);t->insert(i3);t->insert(i4);
	t->insert(i5);t->insert(i6);t->insert(i7);t->insert(i8);
	t->print();
	t->remove(2);
	t->print();
	t->removeAndDelete(3);
	t->print();
	t->removeAndDelete(1);
	t->print();	
	t->remAndDelAll();
	t->print();
	int* i11 = new int;
	*i9 = a9; *i10 = a10;*i11 = a1;
	t->insert(i11);
	t->append(0,i9);
	t->prepend(1,i10);
	t->append(2305,i9);
	t2 = t;
	t2->print();
	intArray_t* t3 = t;
	t3->print();
	t->~intArray_t();
	t->print();
	getchar();
    return 0;
}

thanks :)

when you do -

Object *obj = new Object

you get a pointer to an object. i.e. obj is a variable which holds the address of the allocated object. you are free to copy this as much as you want.

To test your non-copyability you need to dereference the pointers before trying to copy or assign them. eg.

*obj = *obj2;

This article has been dead for over six months. Start a new discussion instead.