Errors, bugs, questions - page 1874

 
Koldun Zloy:

If you are sure that the constructor must be private, there is no other way.

A smart pointer is an object that contains a simple pointer and ensures that it is removed in time.

This is the simplest option.

There are more complex smart pointers.

It's very well written about them in this book: https://rsdn.org/res/book/cpp/cpp_real_programmers.xml

Thank you! Tried different variants, but it doesn't work
template <typename T>
struct PTR
{
  T* Ptr;
  
  PTR( void )
  {
  }
  
  void operator =( T* &Value )
  {
    this.Ptr = Value;
  }
  
  ~PTR( void )
  {
    Print(__FUNCSIG__);
    delete this.Ptr;
  }
};

class CLASS
{
private:
  static CLASS* Tmp;
  static PTR<CLASS> Ptr;
  
  CLASS()
  {
    CLASS::Ptr = CLASS::Tmp;
  }
};

static CLASS* CLASS::Tmp = new CLASS;
static PTR<CLASS> CLASS::Ptr;

void OnStart()
{
}

As a matter of fact, it's logical that it doesn't work. You can't make the destructor of a smart pointer be called before the class destructor.

 

Instead of specifying the error and where it occurs, the compiler gives an unspecified internal error #112

class CLASS {};

template <typename T>
class CLASS2
{
public:
  static void Func() {}
};

void OnStart()
{
  CLASS2<CLASS>::Func;  
}
 
fxsaber:
Thank you! I tried different variants, but they do not work.

Actually, it's logical that it doesn't work. You can't make the destructor of a smart pointer be called before the class destructor.


Try the following way:

template < typename T >
struct PTR
{
  T* Ptr;
  
  PTR( T* Value ) : Ptr( Value )
  {
  }
  
  ~PTR()
  {
    Print(__FUNCSIG__);
    delete Ptr;
  }
};

class CLASS
{
private:
  static PTR< CLASS > Ptr;
  
public:
  CLASS()
  {
      Print(__FUNCSIG__);
  }
  ~CLASS()
  {
      Print(__FUNCSIG__);
  }
};

static PTR< CLASS > CLASS::Ptr( new CLASS );

void OnStart()
{
}

I don't know what you're doing, but in MQL making the constructor private makes sense only for the singleton.

 
Koldun Zloy:

Try it this way:

Thanks for the detailed example! Unfortunately, it doesn't have a closed constructor. It doesn't work like that with a closed one, of course.

What I need is for an object to exist that is hidden from everyone. At the same time, no other object of this type could be created in any way.

 
Is it correct in such a case
class CLASS
{
public:
  ~CLASS()
  {
    static bool FirstRun = true;
    
    if (FirstRun && CheckPointer(&this) == POINTER_DYNAMIC)
    {
      FirstRun = false;
      
      delete &this;
    }
    
    Print(__FUNCSIG__);
  }
};

void OnStart()
{
  CLASS* Class = new CLASS;
  delete Class;
}

to output this to the execution log?
void CLASS::~CLASS()
void CLASS::~CLASS()
delete invalid pointer


And is it correct that the __FUNCSIG__ of the constructor/destructor gives a void type?

 
Koldun Zloy:

Try it this way:

Why is the smart pointer destructor called before the class destructor in your version, but the opposite in mine (even though the constructor is public)?
 
fxsaber:

I need to have an object that is hidden from everyone. At the same time, another object of this type could not be created in any way.


That's what it's called:"Singleton".

Why do you say it's not suitable for you?

Here is an example of a singleton.

template < typename T >
struct PTR
{
   T* Ptr;
   
   PTR() : Ptr( NULL ){}
   PTR( T* ptr ) : Ptr( ptr )
   {
   }
   
   ~PTR()
   {
      Print(__FUNCSIG__);
      if( Ptr )delete Ptr;
   }
   
   void Set( T* ptr )
   {
      Ptr = ptr;
   }
};

class CLASS
{
   static PTR< CLASS > sPtr;
   
   CLASS()
   {
      Print(__FUNCSIG__);
   }
   
public:
   ~CLASS()
   {
      Print(__FUNCSIG__);
   }
   
   static CLASS* GetPtr()
   {
      if( !sPtr.Ptr ){
         sPtr.Set( new CLASS );
      }
      return sPtr.Ptr;
   }
};

static PTR< CLASS > CLASS::sPtr;

void OnStart()
{
   CLASS* ptr = CLASS::GetPtr();
}
 
fxsaber:
Is it correct in such a case
to output this to the execution log?


And is it correct that the __FUNCSIG__ of the constructor/destructor gives a void type?


Never do

delete &this;
 
fxsaber:
Is it correct to output this to the execution log in such a case?

And is it correct that the __FUNCSIG__ of the constructor/destructor outputs void-type?


Yes, it is.
 
Koldun Zloy:


That's what it's called:"Singleton".

Why do you say it doesn't suit you?

Here's an example of a singleton.

Thank you very much, it works!

template < typename T >
struct PTR
{
   T* Ptr;
   
   PTR( void ) : Ptr(NULL)   
   {
   }
   
   PTR( T* ptr ) : Ptr(ptr)
   {
   }
   
   ~PTR( void )
   {
     if (this.Ptr)
       delete this.Ptr;
   }
   
   bool Set( T* ptr )
   {
      this.Ptr = ptr;
      
      return(true);
   }
   
   void operator =( bool )
   {
   }
};

class CLASS
{
private:   
   static PTR<CLASS> sPtr;
   
   CLASS()
   {
   }
   
public:
  static bool Set()
  {
    return(CLASS::sPtr.Ptr ? false : CLASS::sPtr.Set(new CLASS));
  }
};

static PTR<CLASS> CLASS::sPtr = CLASS::Set();

void OnStart()
{
}

But it still remains a mystery.

Forum on trading, automated trading systems & strategy testing

Bugs, bugs, questions

fxsaber, 2017.04.25 10:34

Why is the smart pointer destructor called before the class destructor in your version and the opposite in mine (even if the constructor is public)?
Reason: