Inicjalizowanie pól obiektu - lista inicjalizacyjna

0
class macierz{
private:
	double **tab;								//wskaźniki
	int m_height;								//wysokość macierzy
	int m_width;								//szerokość macierzy
public:
	macierz();									//konstruktor domyślny
	~macierz(){ if(tab!=NULL) delete []tab; }	                       //destruktor
};
macierz::macierz():
m_height(1), m_width(1),
tab(new double*[1]),
{	
	tab[0] = new double[1];
	tab[0][0] = 1;
}

Czy istnieje taka możliwość, aby w jakiś sposób zainicjalizować tablicę w liście inicjalizacyjnej, tak aby konstruktor nie zawierał żadnej instrukcji?

0

Proponuję takie coś, ale na pewno istnieje bardziej optymalne rozwiązanie, które nie wymaga nawet użycia klasy std::vector.

#include <iostream>
#include <vector>


class macierz
{
public:
  macierz(int width = 1, int height = 1) 
    : width_(width), height_(height), tab_(width * height, 0) {}

  double &operator()(int x, int y)
  {
    return tab_[y * width_ + x];
  }

private:
  std::vector<double> tab_;
  int width_, height_;

};


int main(void)
{
  macierz obj(16, 12);
  obj(8, 3) = 1;

  for (int y = 0; y < 12; y++) {
    for (int x = 0; x < 16; x++) {
      std::cout << " " << obj(x, y);
    }
    std::cout << std::endl;
  }
  return 0;
}
1

Mniej więcej takie coś:

#include <iostream> 
using namespace std;
 
class matrix
  {
   private:
   unsigned height,width;
   double *tab;
   double *init(double v)
     {
      double *tmp=new double[height*width];
      for(unsigned i=0,y=0;y<height;++y) for(unsigned x=0;x<width;++x,++i) tmp[i]=v;
      return tmp;
     }
   public:
   unsigned Height()const { return height; }
   unsigned Width()const { return width; }
   matrix(unsigned height=0,unsigned width=0,double v=0):height(height),width(width),tab(init(v)) {}
   ~matrix() { delete[] tab; }
   double *operator[](unsigned y) { return tab+y*width; }
  };

int main()
  {
   matrix m(6,3);
   m[1][1]=7;
   for(unsigned y=0;y<m.Height();++y,cout<<endl) for(unsigned x=0;x<m.Width();++x) cout<<'\t'<<m[y][x];
   return 0;
  }
0

Dziękuję za propozycje.
Można jakoś obejść zastosowanie operatora:

 double *operator[](unsigned y) { return tab+y*width; }

Do tego etapu jeszcze nie doszedłem i nie za bardzo mogę go używać.

0
double &at(unsigned y,unsigned x) { return tab[y*width+x]; }
...
   m.at(1,1)=7;
   for(unsigned y=0;y<m.Height();++y,cout<<endl) for(unsigned x=0;x<m.Width();++x) cout<<'\t'<<m.at(y,x);
0

ok, niby zero błędów, program się kompiluje, ale kiedy włączam następuje DEBUG ERROR!

#include <iostream>
#include <time.h>
using namespace std;

class macierz{
	static const int DEFAULT = 1;
private:
	double **tab;								//wskaźniki
	int m_height;								//wysokość macierzy
	int m_width;								//szerokość macierzy
	static int z;								//ile obiektów utworzone
	static const int MAX;
	double **init(double v)
	{
		double **tmp=new double*[m_height*m_width];
		for(unsigned i=0,y=0;y<m_height;++y) 
			for(unsigned x=0;x<m_width;++x,++i) 
				tmp[i][i]=v;
		return tmp;
	}

public:
	//macierz();									//konstruktor domyślny
	~macierz(){ if(tab!=NULL) delete []tab; }	//destruktor
	//macierz(int height, int width);				//kontruktor z wymiarami macierzy
	//macierz(macierz& mk);						//konstruktor kopiujący
	void setmacierz(int i, int j);				//seter
	double getmacierz(int i, int j);			//geter
	unsigned Height()const { return m_height; }
	unsigned Width()const { return m_width; }
	macierz(unsigned height=0,unsigned width=0,double v=0):m_height(height),m_width(width),tab(init(v)) {}
	double &at(unsigned y,unsigned x) { return *tab[y*m_width+x]; }
};
int macierz::z = 0;
const int macierz::MAX = 5;						//maksymalny rozmiar macierzy
/*macierz::macierz():
m_height(1), m_width(1),
tab(new double*[1])
{	
tab[0] = new double[1];
tab[0][0] = 1;
z++;
printf("\nz = %d\n", z);
}*/
/*macierz::macierz(int height, int width){
	//sprawdzanie czy rozmiar jest w granicach, jeżeli nie to przytnij do MAX
	if (height <= MAX || width <= MAX){
		m_height = height;
		m_width = width;
		tab = new double*[m_width];
		for (int i = 0; i < m_width; i++)
			tab[i] = new double[m_height];
	}
	else{
		m_height = MAX;
		m_width = MAX;
		tab = new double*[m_width];
		for (int i = 0; i < 5; i++)
			tab[i] = new double[5];
	}
	//wpisanie wartości
	for (int i = 0; i < m_height; i++)
		for (int j = 0; j < m_width; j++)
			tab[i][j] = j+1;
}*/
/*macierz::macierz(macierz& mk) {
	tab = new double*[mk.m_width];
	m_width = mk.m_width;
	m_height = mk.m_height;
	for (int i = 0; i < mk.m_height; i++){
		tab[i] = new double[mk.m_height];
		tab[i] = mk.tab[i];
	}
}*/
void macierz::setmacierz(int i, int j){	
	if (i >= 0 || i < m_height){
		if (j >= 0 || j < m_width){
			srand((unsigned int)time(NULL));
			//wpisywanie wartości
			for (int i = 0; i < m_height; i++)	
				for (int j = 0; j < m_width; j++)
					tab[i][j] = (rand() % 3) - 1;
		}
	}
}
double macierz::getmacierz(int i, int j){
	if (i >= 0 && i < m_height){
		if (j >= 0 && j < m_width){
			return tab[i][j];
		}
		else{
			return 0;
		}
	}
	else{
		return 0;
	}
}
void main(){
	macierz m(6,3);
	m.at(1,1)=7;
	for(unsigned y=0;y<m.Height();++y,cout<<endl) 
		for(unsigned x=0;x<m.Width();++x) 
			cout<<'\t'<<m.at(y,x);

	/*int r1 = 5, r2 = 5;			//rozmiar macierzy użytkownika
	macierz a;
	macierz b;
	cout << "### OBIEKT 1 ###\n";
	printf("%2g   ", a.getmacierz(0,0));

	macierz mk(r1, r2); //39999 maks.
	mk.setmacierz(3,2);
	cout << "\n\n### OBIEKT 2 ###";
	for (int i = 0; i < r1; i++){
	cout <<"\n";
	for (int j = 0; j < r2; j++)	
	printf("%2g   ", mk.getmacierz(i,j));
	}
	macierz kopia_a(a);
	cout << "\n\n### OBIEKT 3 ###\n";
	printf("%2g   ", kopia_a.getmacierz(0,0));
	macierz mk2(mk);
	cout << "\n\n### OBIEKT 4 ###";
	for (int i = 0; i < r1; i++){
	cout <<"\n";
	for (int j = 0; j < r2; j++)	
	printf("%2g   ", mk2.getmacierz(i,j));
	}*/
	cout << "\n\nKoniec programu.\n";
}
0

bo pakujesz double* do double**

jak dla mnie nie powinieneś pracować z T**

ale jak już musisz

http://ideone.com/Q0EgPM

0

Dziękuję za pomoc.
Poradziłem sobie własnym sposobem :)

1 użytkowników online, w tym zalogowanych: 0, gości: 1