Class conctructor dilema :)

To add comments, please log in or register
Marcin Rutkowski
317
Marcin Rutkowski  

Is this even possible  ...I'd like to know witch class created what (to exchange the ID ) but this is creating loop, and I'm not sure if this even possible , and I have to use third class ...I'm still  far from the expert level but the algorithm I'm working on will need to be able to do similar thing

is there a way to condition  if (classDemoA == created) then  include constructor classDemoB(classDemoA &objA) in  classDemoB  ??   ...is there a way to #ifDef ??  fo first class A wave not incluseproblematic constructor ?? but after is included  ... where to find this info ??


class classDemoA
  {
public:
                     classDemoA(void);
                     
                     classDemoA(classDemoB &objB);   //  <<  problem becouse this do not exsist yet :)   , but it will in the future :)
                    ~classDemoA(void);
  };


class classDemoB
  {
public:
                     classDemoB(void);
                     classDemoB(classDemoA &objA)
                     
                    ~classDemoB(void);
  };


Apologise if I'm asking stupid question but I'm despred for the answer to be able to move on :)

Seng Joo Thio
746
Seng Joo Thio  
Marcin Rutkowski:

Is this even possible  ...I'd like to know witch class created what (to exchange the ID ) but this is creating loop, and I'm not sure if this even possible , and I have to use third class ...I'm still  far from the expert level but the algorithm I'm working on will need to be able to do similar thing

is there a way to condition  if (classDemoA == created) then  include constructor classDemoB(classDemoA &objA) in  classDemoB  ??   ...is there a way to #ifDef ??  fo first class A wave not incluseproblematic constructor ?? but after is included  ... where to find this info ??

Apologise if I'm asking stupid question but I'm despred for the answer to be able to move on :)

I will do it this way:

class Root
{
   private:
      string Name;
   public:
      Root() { SetName("Root"); };
      ~Root() { Name = NULL; };
      string GetName() { return (Name); };
      void SetName(string NewName) { Name = NewName; };
};

class classDemoA : public Root
  {
public:
                     classDemoA(void) { SetName("classDemoA"); };
                     
                     classDemoA(Root &objB) { SetName(objB.GetName()); };
                    ~classDemoA(void) {  };
  };


class classDemoB : public Root
  {
public:
                     classDemoB(void) { SetName("classDemoB"); };
                     classDemoB(Root &objA) { SetName(objA.GetName()); };
                     
                    ~classDemoB(void) {  };
  };

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
   Print ("================================================================");
   classDemoA *ObjA = new classDemoA();
   classDemoB *ObjB = new classDemoB();
   
   Print (ObjA.GetName(), ", ", ObjB.GetName());
   
   delete ObjA;
   delete ObjB;
   
   ObjA = new classDemoA();
   ObjB = new classDemoB(ObjA);

   Print (ObjA.GetName(), ", ", ObjB.GetName());
   
   delete ObjA;
   delete ObjB;
  }
//+------------------------------------------------------------------+

There will not be compilation warnings/errors, and the output will be:

2019.05.21 13:48:48.603 ClassTest (EURUSD,H1)   ================================================================
2019.05.21 13:48:48.603 ClassTest (EURUSD,H1)   classDemoA, classDemoB
2019.05.21 13:48:48.603 ClassTest (EURUSD,H1)   classDemoA, classDemoA
Marcin Rutkowski
317
Marcin Rutkowski  
Seng Joo Thio:

I will do it this way:

There will not be compilation warnings/errors, and the output will be:

Thank You very much for the reply, I was very much avoiding to create third class but looks like this is necesery   ...Just to show You my latest try  ...I did  something like this as below but now I know that preprocessors directives are used only once from the top to the bottom and if the same class is created second time  the directives are not changed even is they where directed to be changed  (my current level of understanding this)   ...this code compiles ok if only A class is initialised using (void) constructor, if later I use (classB) constructor, even is # define is defined   this is not taken under consideration unfortunately   ...code below

//+------------------------------------------------------------------+
class classDemoA
  {
public:
                     classDemoA(void){};
#ifdef classBcreated
                     classDemoA(classDemoB &objB){};
#endif
                    ~classDemoA(void){};
  };
//+------------------------------------------------------------------+
class classDemoB
  {
public:
                     classDemoB(void){};
                     classDemoB(classDemoA &objA){};
                    ~classDemoB(void){};
  };

#define   classBcreated
//..............................                         >>    >>>>    >>    >>>>    >>
int OnInit()
  {
   classDemoA classA();
   classDemoB classB();
  
//   classDemoA classAA(classB);      //   preprocessor directives have only single run ??? or I did something wrong ??
   return(INIT_SUCCEEDED);
  }
nicholi shen
2380
nicholi shen  

It's hard to give advice because it is very unclear what you're hoping to accomplish, but what I can tell you is that you need to be using forward declarations. 

class Manager;

class Worker {
   Manager *m_manager;
public:
   Worker(Manager *m):m_manager(m){}
};

class Manager {
   Worker *m_worker;
public:
   Manager() {
      m_worker = new Worker(&this);
   }
   ~Manager() {
      delete m_worker;
   }
};

void OnStart(){
   Manager m;
}
Marcin Rutkowski
317
Marcin Rutkowski  
nicholi shen:

It's hard to give advice because it is very unclear what you're hoping to accomplish, but what I can tell you is that you need to be using forward declarations. 

WOW :)  ..You just read my mind  ...I didn't know this  was possible   ...You have no idea how many problems this post solved  ...thank You , thank You, thank You :)  ..sometimes is very hard to explain  what I'm looking for   "forward declarations"   I will remember this :)  
Marcin Rutkowski
317
Marcin Rutkowski  
nicholi shen:

It's hard to give advice because it is very unclear what you're hoping to accomplish, but what I can tell you is that you need to be using forward declarations. 

Last question   ...will this affect the performance if I create separate file and "forward declare" all my classes or I should just use it for "necessary scenarios" ??

Seng Joo Thio
746
Seng Joo Thio  
Marcin Rutkowski:

Last question   ...will this affect the performance if I create separate file and "forward declare" all my classes or I should just use it for "necessary scenarios" ??

No, forward declarations has no run-time performance impact.

Marcin Rutkowski
317
Marcin Rutkowski  
Seng Joo Thio:

No, forward declarations has no run-time performance impact.

Thank You   ...I double checked and this works beautifully    ...but is not listed in documentation yet :)   . Regards

To add comments, please log in or register