What I'm trying to imitate with GObject
Here's a simple C++ example:
#include <iostream>
using std::cout;
using std::endl;
class Matrix {
public:
Matrix (int rows, int cols) {
this->rows = rows;
this->cols = cols;
this->coeffs = 0;
};
int rows, cols;
double *coeffs;
};
class SymmetricMatrix final : public Matrix {
public:
SymmetricMatrix (int size) : Matrix (size, size) {
this->size = size;
this->nb_coeffs = size*(size-1)/2;
this->coeffs = new double[this->nb_coeffs]();
};
int size, nb_coeffs;
void print_self () {
for (int i=0; i<this->nb_coeffs; i++) {
cout << this->coeffs[i] << " ";
}
cout << endl;
};
};
int main () {
SymmetricMatrix S (6);
S.print_self ();
cout << S.cols << endl; // Access parent's public member variable !
return 0;
}
What I've got for now
I want to copy the C++ class structure but using GObject.
The problem is: I don't understand how I can define and inherit public variables for a derivable base class. Indeed, since the G_DECLARE_DERIVABLE_TYPE already defines one, I can't redefine it... Why is it one should not be able to do this ?
Here's what I have for now[^1]. As far as I understand, it isn't possible because GObject is not C++ and the general paradigms aren't the same.
tmatrix.h
#ifndef TMATRIX_H
#define TMATRIX_H
#include <glib-object.h>
#define T_TYPE_MATRIX t_matrix_get_type()
G_DECLARE_DERIVABLE_TYPE (TMatrix, t_matrix, T, MATRIX, GObject)
struct _TMatrixClass {
GObjectClass parent_class;
};
// struct _TMatrix {
// GObject parent_instance;
// gint nrows;
// gint ncols;
// gdouble *coeffs;
// };
/* Public function */
TMatrix *t_matrix_new (int ncols, int nrows);
#endif
tmatrix.c
#include "tmatrix.h"
G_DEFINE_TYPE (TMatrix, t_matrix, G_TYPE_OBJECT)
void t_matrix_class_init (TMatrixClass *class) {}
void t_matrix_init (TMatrix *instance) {}
TMatrix *t_matrix_new (gint ncols, gint nrows) {
TMatrix *matrix = g_object_new (T_TYPE_MATRIX, NULL);
// matrix->cols = ncols;
// matrix->rows = nrows
return matrix;
}
tsymmetricmatrix.h
#ifndef TSYMETRICMATRIX_H
#define TSYMETRICMATRIX_H
#include "tmatrix.h"
#include <glib-object.h>
struct _TSymmetricMatrix {
TMatrix parent_object;
// Public members
gint size;
};
#define T_TYPE_SYMMETRIC_MATRIX t_symmetric_matrix_get_type ()
G_DECLARE_FINAL_TYPE (TSymmetricMatrix, t_symmetric_matrix, T, SYMMETRIC_MATRIX, TMatrix)
/*Public class methods*/
void t_symmetric_matrix_print (TSymmetricMatrix *self);
/*Namespace public functions*/
TSymmetricMatrix *t_symmetric_matrix_new (gint size);
#endif
tsymmetricmatrix.c
#include "tsymmetricmatrix.h"
G_DEFINE_FINAL_TYPE (TSymmetricMatrix, t_symmetric_matrix, T_TYPE_MATRIX)
void t_symmetric_matrix_class_init (TSymmetricMatrixClass *class) {}
void t_symmetric_matrix_init (TSymmetricMatrix *instance) {}
void t_symmetric_matrix_print (TSymmetricMatrix *self) {
// print code
}
TSymmetricMatrix *t_symmetric_matrix_new (gint size) {
TSymmetricMatrix *matrix = g_object_new (T_TYPE_SYMMETRIC_MATRIX, NULL);
//
// I don't know what to do next ...
return matrix;
}
main.c
#include "tmatrix.h"
#include "tsymmetricmatrix.h"
int main () {
TSymmetricMatrix *A = t_symmetric_matrix_new (3);
t_symmetric_matrix_print(A);
return 0;
}
[^1]:Note: I know I should be using properties for setting/getting class variables. This is really just for the sake of learning.