Expand|Select|Wrap|Line Numbers
- class class_name{
- public:
- class_name(); // Constructor
- // Member functions
- void function_1();
- void function_2(double *mat);
- void function_3();
- void function_2_re_def(double *mat);
- void function_3_re_def();
- public:
- int rows;
- int cols;
- double *v1;
- double *v2;
- };
Expand|Select|Wrap|Line Numbers
- void class_name::function_1()
- {
- v1=new(double[(rows+1)*(cols+1)]);
- double some_number;
- for(int i=1;i<=rows;i++){
- for(int j=1;j<=cols;j++){
- *(v1+i*(cols+1)+j)=some_number;
- }
- }
- }
Expand|Select|Wrap|Line Numbers
- void class_name::function_2(double *mat)
- {
- mat=new(double[(rows+1)*(cols+1)]);
- double some_number;
- for(int i=1;i<=rows;i++){
- for(int j=1;j<=cols;j++){
- *(mat+i*(cols+1)+j)=some_number;
- }
- }
- }
Expand|Select|Wrap|Line Numbers
- void class_name::function_3()
- {
- // perform the computation,
- function_1();
- // The data in v1 can be accessed inside this function
- // Print the data to screen
- for(int i=1;i<=rows;i++){
- for(int j=1;j<=cols;j++)
- cout<<*(v1+i*(cols+1)+j)<<" ";
- cout<<endl;
- }
- }
Expand|Select|Wrap|Line Numbers
- void class_name::function_3()
- {
- // perform the computation,
- function_2(v2);
- // The data in v2 cannot be accessed inside this function
- // Print the data to screen fails, program crashes
- for(int i=1;i<=rows;i++){
- for(int j=1;j<=cols;j++)
- cout<<*(v2+i*(cols+1)+j)<<" ";
- cout<<endl;
- }
- }
Expand|Select|Wrap|Line Numbers
- void class_name::function_2_re_def(double *mat)
- {
- double some_number;
- for(int i=1;i<=rows;i++){
- for(int j=1;j<=cols;j++){
- *(mat+i*(cols+1)+j)=some_number;
- }
- }
- }
Expand|Select|Wrap|Line Numbers
- void class_name::function_3_re_def()
- {
- // perform the computation,
- v2=new(double[(rows+1)*(cols+1)]);
- function_2(v2);
- // The data in v2 can now be accessed inside this function
- // Print the data to screen
- for(int i=1;i<=rows;i++){
- for(int j=1;j<=cols;j++)
- cout<<*(v2+i*(cols+1)+j)<<" ";
- cout<<endl;
- }
- }
Is there a scope issue that means that you must know the size of an array before passing arrays into class member functions?
Thanks for your help.