![]() |
![]() |
| Paypal | FTP | CCD | Buscar | Trucos | Trabajo | Foros |
|
|||||||
| Registrarse | FAQ | Miembros | Calendario | Guía de estilo | Buscar | Temas de Hoy | Marcar Foros Como Leídos |
![]() |
|
|
Herramientas | Buscar en Tema | Desplegado |
|
|
|
#1
|
|||
|
|||
|
Buenas... Teniendo encuenta lo dicho anteriormente he implemetado una clase Matriz de la forma
Código:
class TMatriz
{
private:
**Matriz;
public:
TMatriz(int Nfilas,int NColumnas); // Constructor
TMatriz (const TMatriz &m); //Constructor de Copia
const TMatriz &m operator = (const TMatriz &m) //Sobrecarga asignacion =
}
Tmatriz::Tmatriz(int Nfilas, Int NColumnas)
{
Matriz = new char *[NColumnas]
for (int i=0;i<NColumnas;i++)
{
Matriz[i] = new char [Numfilas];
}
}
He intentado implementar la funcion constructor de copia (reservando memoria con memcpy) y la funcion sobrecarga asignacion y no lo consigo...me podeis ayudar Gracias |
|
#2
|
||||
|
||||
|
Básicamente la cosa sería algo así:
Código:
class TMatriz
{
private:
char **Matriz;
int Filas;
int Columnas;
public:
TMatriz();
TMatriz(int Nfilas, int NColumnas); // Constructor
TMatriz (const TMatriz&); //Constructor de Copia
TMatriz &operator=(const TMatriz&);
};
// Constructor por defecto
TMatriz::TMatriz(): Filas(0), Columnas(0), Matriz(0)
{
}
TMatriz::TMatriz(int Nfilas, int NColumnas): Filas(Nfilas), Columnas(NColumnas),
Matriz(0)
{
Matriz = new char *[NColumnas];
for(int i=0; i<NColumnas; i++)
Matriz[i] = new char [Nfilas];
}
// Constructor copia
TMatriz::TMatriz(const TMatriz& E): Filas(0), Columnas(0), Matriz(0)
{
*this = E;
}
// Sobrecarga del operador asignación
TMatriz& TMatriz::operator=(const TMatriz &E)
{
// Libero mamoria
if(Matriz){
for(int i=0; i<Columnas; i++) delete [] Matriz[i];
delete [] Matriz;
}
// Copio la Matriz
Columnas = E.Columnas;
Filas = E.Filas;
Matriz = new char *[Columnas];
for(int i=0; i<Columnas; i++){
Matriz[i] = new char [Filas];
CopyMemory(Matriz[i], E.Matriz[i], Filas);
}
return *this;
}
Código:
TMatriz Mt1(10,10); TMatriz Mt2(Mt1); // Con el constructor copia TMatriz Mt3 = Mt1; // Asignando Saludos. |
|
#3
|
||||
|
||||
|
No te olvides de implementar un destructor.
![]() Saludos. |
|
#4
|
|||
|
|||
|
Muchisimas Gracias........ eres un Crack.
Tendre que seguir trabajando con el tema de los punteros, no acabo de entenderlos del todo Gracias otra vez |
![]() |
| Herramientas | Buscar en Tema |
| Desplegado | |
|
|
Temas Similares
|
||||
| Tema | Autor | Foro | Respuestas | Último mensaje |
| Matriz dinamica | BC++ | C++ Builder | 7 | 24-10-2011 19:04:29 |
| Copia de tablas como copia de seguridad | Mathom | Varios | 4 | 04-01-2006 09:19:57 |
| matriz dinámica de string | triniti | OOP | 4 | 20-07-2004 13:31:52 |
| como hago q un TDBLookupComboBox tenga un valor seleccionado por defecto | aram2r | Conexión con bases de datos | 1 | 03-06-2004 11:36:26 |
| Como hago referencia al valor de una celda de un DBGrid? | Sóstrato | OOP | 3 | 12-06-2003 00:32:06 |
|