Share a pointer

Started by
9 comments, last by _WeirdCat_ 5 years, 6 months ago

so I have 3 classes

main class which stores a pointer


struct Main {

int * p;

A subclass1, subclass2;

void init()

{

subclass1.init(p);

subclass2.init(p);

}

};



and two subclasses

struct A {

int * p;

void init(int * ap)

{

p = ap;

}

};

Now I would like to create a pointer from subclass1 or subclass2 that will actually create a pointer in Main class, so subclasses can use the same int from main class

heres what im trying to achieve

in first scenario you see same windows so you see that on phone1 and phone2 theres a blue wireframed object that im trying to share between multiple windows, but when I change projection window I cant see it, so I need a shared pointer

https://youtu.be/9HLDsrm7R2w

 

I already messed with int ** p; for subclasses but im missing something (maybe because int * p in main class is 0)

and I cant assign it by int ** subp = (*mainclass.p);

 

 

if someone tells me that I could use shared ptr then I ask him to provide some example code for me cause I never used it and have no xp with it.

However pointer to pointer c style would be the best approach for me thus theres something wrong in the code I dont fully understand so far, int * p in main class has to be 0 until its not created by subclass...

maybe I somewhere use it where I shouldnt, well I just changed the code from * to ** and just switched from if (p ==0) return;

to if ( (*p) == 0) return; etc.

but theres few thousands lines I need to check so a short brief of handling such types would be appreciated..

Advertisement

First it's always very helpful when you indent your code. You can use the code box button <> to help you out.

Second subclass usually means a derived class. So for instance:

 


class A
{
	......
};

class B : public A
{
    .....
};

So here B is a subclass of A.

What you have are two instances of class A which are members of class Main. Of course you can name them subclass1 and subclass2 if you want but there is probably a better choice of names.

Finally, I'm not sure where you intend to put the actual integer. All I can see is pointers to integers. You could put the integer in Main itself, like this:


struct A
{
   int* p;

   void init(int * ap)
   {
      p = ap;
   }
}; 

struct Main 
{
   int iInt;

   A subclass1, subclass2;

   void init()
   {
      subclass1.init(&iInt);
      subclass2.init(&iInt);
   }

};

Now you can actually have a place for your number.  Also you might want to use constructors instead of init functions. Sometimes init functions come in handy in odd cases but here you really don't need them. So that gives us:


#include <iostream>

struct A
{
   int* p;

   A(int * ap)
   {
      p = ap;
   }
}; 

struct Main 
{
   int iInt;

   A member1, member2;

   Main(int iVal) : iInt(iVal), member1(&iInt), member2(&iInt)
   {
   }

};


int main()
{
   Main clMain(666);

   std::cout << "member1 value = " << *clMain.member1.p << std::endl;
   std::cout << "member2 value = " << *clMain.member2.p << std::endl << std::endl;

   clMain.iInt = 444;

   std::cout << "member1 value = " << *clMain.member1.p << std::endl;
   std::cout << "member2 value = " << *clMain.member2.p << std::endl;

   return 0;
}

Which outputs:

member1 value = 666
member2 value = 666

member1 value = 444
member2 value = 444

I'm not really commenting your design here. There might be some better way of doing what you are trying to do.

Well, welll.... hmmm.............

Not to mention i cant properly edit my post above so ill leave it like it is now...

But subclass wont be derived from mainclass, thats not going to happen, i only made an example which is not the exact tthing i have if you really need to know main class is TMainFramework class which is of class TGLForm which is of class TVCLComponent which is of class TPhysicalElement<float, float, float, float, float>

And it stores a list of forms 6 side projection forms and one perspective projection forms which are the type of TGLForm which is of type of... and so on so its irrevelant.

Anyway in main class i used an int * p; description which is actually model3f * face_model; anyway this doesnt matter

Since i would like to create a pointer in main class from the subclass i call it

So if subclass has int ** p;

And during initialization (in subclass space) i call (*subclass.p) = mainclass.p;

Where before anything happens like initialization mainclass int * p is set to be 0.

I would rather want to stay like it is unless i will have no chocie and have to change it.

So (*subclass.p) = mainclass.p; for me it seems like i dereference an unknown address and try to make it 0;

However in some other subclass code i would like to do something like

(*p) = new int();//new model3f();

So int * p in main class points to that newly created var,

And int **p in that other subclass point to int * p from main class which is now not a 0 butnsomething created.. i hope i described that

 

What about using conversion ? https://en.cppreference.com/w/cpp/language/cast_operator

http://9tawan.net/en/

Well just add a level of de-referencing and you would get:


#include <iostream>

struct A
{
   int **p;

   A(int **ap)
   {
      p = ap;
   }
}; 

struct Main 
{
   int* p;

   A member1, member2;

   Main() : p(0), member1(&p), member2(&p)
   {
   }

   void SetVal(int *pVal)
   {
      p = pVal;
   }

};


int main()
{
  
   Main clMain;

   int *pVal1 = new int(666);

   clMain.SetVal(pVal1);

   std::cout << "member1 value = " << **clMain.member1.p << std::endl;
   std::cout << "member2 value = " << **clMain.member2.p << std::endl << std::endl;

   int *pVal2 = new int(444);

   clMain.SetVal(pVal2);

   std::cout << "member1 value = " << **clMain.member1.p << std::endl;
   std::cout << "member2 value = " << **clMain.member2.p << std::endl;

   delete pVal1;
   delete pVal2;

   return 0;
}

 

You can and probably should use smart pointers for this.  However I loath the C++ standard library shared_ptr because it creates extra objects you woudn't need if you simply implement a reference counted object base class. This can cause a major performance hit.

as for that conversion thing i dont know yet, but for the post above, i cant call main class from a subclass due to circular reference thing, i could obviously bypass that, but thats not the point, still searching for legit solution, i want to change the pointer in main class from the subclass itself which has a pointer to that mainclass pointer which is 0 at start and (*face_model) = aface_model;  cracks the app where face_model is model3f ** and aface_model is model3f * that is set to 0

 

 

And yesterday i thought that was an easy to solve problem lol

5 minutes ago, _WeirdCat_ said:

as for that conversion thing i dont know yet, but for the post above, i cant call main class from a subclass due to circular reference thing, i could obviously bypass that, but thats not the point, still searching for legit solution

It this case you don't need to. You have a pointer in each of the instances of A  to the int *p in Main. I thought that was the whole point.  Also you can in fact call members of Main in class A. You just need to break things up into .h and .cpp files.

I think i found it hahhah huehue

Just make it a global var and don't care !:0 about any pointers to pointers

 

 

Genius ; )

Many times when there is communications it is best to work on layers.

From your description where you have two windows that need to share data, one option is to have each window draw any data that is given to it through a pointer. Then a different system can provide both windows with a pointer. That separate system can also respond to input events and notify windows when there is something new or different to draw.

This type of composition is common in user interfaces and handled with ownership.  An individual button, an individual text box, an individual icon, each of them knows how to draw one thing. They are put into a bigger container control that tells each button and text box what to do.  That bigger control might be placed inside another window, controlled by the application. The individual icons might be reused in many places, and the application might forward update events to any number of windows and to any number of UI controls, but the individual UI controls don't need to know or care about others that use the data.

It looks like you also have discovered the critical issue of understanding object lifetimes. Controlling object lifetimes is a critical part of software engineering, and doing it wrong results in crashes (when something isn't there any more) and resource leaks (when it wasn't properly cleaned up).  Having a reliable hierarchy helps with lifetimes, since a parent can assure a child object that it will not assign the value until after it is valid, and it will notify the child before modifying or destroying objects that have been assigned.

Yeah, i get you i already have such things, but i had to make a break from writing to realize that i dont even have a valid pointer.... Too much writing and one way thinking burns the head, i definetly spend too much time programming...

This topic is closed to new replies.

Advertisement