PLO. Application issues - page 15

 

The default constructor has a specific purpose for initialising an array of objects of its class. A constructor with all its parameters set to default isnot a default computer. Tut.

I highlighted the typo in red.

By the way, why is such a constructor not a constructor by default? Another crutch?

 
TheXpert:

A default constructor has a specific purpose for initialising an array of objects of its class. A constructor with all its parameters set to default isnot a default computer. Here

Interesting typo, thanks. Corrected.
 
Yedelkin:

"Invalid pointer access" =="Attempting to access an invalid pointer"?

Often a direct sign of a bad hand or lack of understanding of the basic principles of working with pointers.

Less often a sign of poorly documented class usage.

 
TheXpert:

By the way, why isn't this constructor a default constructor? Another crutch?

Quite the opposite. The crutch is having default parameters, as this is a source of hard-to-find bugs. This is where we are being strict.

If you write a constructor with parameters, you are able to write a parameterless constructor as well. And if you have a default constructor, you can't write a parametric constructor with all the default parameters.

 
stringo:

It is a crutch to have default parameters, as this is a source of hard to catch bugs.

Yes somehow I can't think of any such situations.

And if there is a default constructor, you can't write a parametric constructor with all the default parameters.

Ah, then it's normal and logical. Otherwise there was some sort of incomprehensible ambiguity.
 

I read aboutthe context resolution operation ( ::: ). I decided to try to use it in two unrelated classes:

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
class C_B;
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
class C_A
  {
public:
   C_B              *pointer;

                     C_A(void) {};
                    ~C_A(void) {};
   void funcA(int i) { Print("funcA(",i,")=",i); }
  };
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
class C_B
  {
public:
                     C_B(void) {};
                    ~C_B(void) {};
   void funcB(int j)
     {
      Print("funcB(",j,")=",j);
      C_A::funcA(j);
     }
  };
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnStart()
  {
   C_A object;
   object.pointer=new C_B();
   object.pointer.funcB(5);
   delete object.pointer;
  }

The compiler gives an error on the line C_A::funcA(j). If I comment it out, it seems to work. What is my mistake?

 
Yedelkin:

I read aboutthe context resolution operation ( ::: ). I decided to try to use it in two unrelated classes:

The compiler generates an error on the line C_A::funcA(j). If I comment it out, it seems to work. What is my mistake?

In your case, the context of class C_A is not available from class C_B.

It's only correct if:

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
class C_B : public C_A
  {
public:
                     C_B(void) {};
                    ~C_B(void) {};
   void funcB(int j)
     {
      Print("funcB(",j,")=",j);
      C_A::funcA(j);
     }
  };

But then it can:


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
class C_B : public C_A
  {
public:
                     C_B(void) {};
                    ~C_B(void) {};
   void funcB(int j)
     {
      Print("funcB(",j,")=",j);
      funcA(j);
     }
  };


In general, such calls are used for overloaded methods:


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
class C_B;
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
class C_A
  {
public:
   C_B              *pointer;

                     C_A(void) {};
                    ~C_A(void) {};
   void func(int i) { Print("C_A::func(",i,")=",i); }
  };
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
class C_B : public C_A
  {
public:
                     C_B(void) {};
                    ~C_B(void) {};
   void func(int j)
     {
      Print("C_B::func(",j,")=",j);
      C_A::func(j);
     }
  };
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnStart()
  {
   C_A object;
   object.pointer=new C_B();
   object.pointer.func(5);
   delete object.pointer;
  }

Approximately like this.

 
uncleVic:

In your case, the context of class C_A is not available from class C_B.

Thank you. So I was wrong that context resolution operation can be applied without inheritance.
 

I'm trying to pass member values from one class to another "by reference" and using a descriptor. Both variants seem to work. Here is the simplest scheme:

class B
  {
private:
   double            d1;
public:
                     B(double &d) {  d1=d; Print("class B, double d1=",d1);  };
                    ~B(void){};
  };
class A
  {
public:
   double            double_var;
   A                *pointerA;
 

                     A(void) : double_var(1.234567),pointerA(NULL) { SetPointer(); };
                    ~A(void) {if(CheckPointer(pointerA)==POINTER_DYNAMIC) delete pointerA; };
private:
   void SetPointer(void) { pointerA=GetPointer(this); }
  };
class C 
  {
public:
   A                *pointerA_C;
public:
                     C(void) : pointerA_C(NULL) {};
                    ~C(void) {if(CheckPointer(pointerA_C)==POINTER_DYNAMIC) delete pointerA_C;};
   void Printer() {if(CheckPointer(pointerA_C)!=POINTER_INVALID) Print("class C, double =",pointerA_C.double_var); };
  };
void OnStart()
  {
   A objA;
//---передаём значение double-переменной по ссылке
   B objB(objA.double_var);
//---передаём значение double-переменной через описатель
   C objC;
   objC.pointerA_C=objA.pointerA;
   objC.Printer();
  }

Which of these two methods works faster? Which one is more preferable?

 
Yedelkin:

I'm trying to pass member values from one class to another "by reference" and using a descriptor. Both options seem to work. Here is the simplest scheme:

Which of these two methods works faster? Which is more preferable?

Are the questions really heavy/incorrect?

For a long time I used the first way ("following the link"), but I really like the idea of descriptors. Just don't know if it's worth rewriting all classes because of it.