   passing this (updated)
  I need to pass an instance of the current class to a new instance of another class, so the other class can do operations on the one that it is currently in.ex:Class1::somefunction(){  vari *Class2 = new Class2(this);}Class2::Class2( const Class1& instance ){//Class1* instance_of_other_class; declared in class2 header  instance_of_other_class = instance;}How could I get something like that working? every combo I have tried gives me an error.should it be saved as a refrence in class2 as well (Class1& instance_of_other_class; declared in class2 header)?On a sidenote is there and diffrence between passing by refrence & or by pointer *?thanks[Edited by - biohaz on June 6, 2006 3:18:19 PM]
Quote:On a sidenote is there and diffrence between passing by refrence & or by pointer *?This is probably where your confusion is coming from.  This may help clear that up.But, as for your question, try dereferencing this (with the unary * operator).   ;  or alternatively changeClass2::Class2( const Class1& instance )toClass2::Class2( const Class1* instance )   ;Quote:Original post by biohazI tried the *this vari *Class2 = new Class2(*this);Class2::Class2( const Class1& instance )  instance_of_other_class = instance;cannot convert from 'const Class1' to 'Class1 *'References and pointers are not interchangable.  References are considered non-pointers by the compiler.  The type of instance_of_other_class should be a reference not a pointer.   ;  Additionally, if you actually want to modify the object referenced by the "instance" parameter (it sounds like you do, which seems rather nasty, but there are some situations where this is acceptable) you'll need to remove the const qualification on the parameter.   ;Ok, it still does not work for what I need so I will make the question non-generic and to what my problem is.I have a class, I call it an object class that loads a bunch of element classes. When the appropriate function is called in the object class it will instance to proper amount of elements (which are derived from a base element class). The elements need to call certain functions in the base class, such as setting messages the object should sent to them, if they should be rendered, and to get the directx pointer. I thought the easiest way would be to send, upon new() the object class that called them (there are an arbitrary number of objects and elements in the objects). I tried passing the reference and changing the pointer stored to a reference, but still no luck. Is it bad to send it into the ctor?  Can anyone help show me how to do this, I assumed passing this into the ctor would do it, (*this) with refrences. Anyone have any code to show how to do this, the hints above still are causing different errors.;  Define "does not work." A compile error? Unexpected runtime behavior? Post the code where you attempt to solve your problem.As I understand it, you have an Object class that is responsible for the construction of Element class instances. Its not clear to me from your description whether Element is derived from BaseElement or Object. I will assume BaseElement, however it shouldn't matter too much. The following rather contrived example will illustrate how to let an Element object 'know about' the Object that is creating it and store a pointer to that object that will allow modification (although I'm still not sure you even need to be doing this kind of ickiness):class Element : public BaseElement{  Object *mObject;  Element(Object *object)  : mObject(object)  {    // call methods on mObject here  }};class Object{  void create_elements()  {  Element *n = new Element(this);  }};;  Yea, im not at my computer now but it was compile problems stemming from me not doing: ": mObject(object) {}" in the header, or a conversion of ref to pointer.So, using a pointer is the best course of action? I thought so, but I have started moving to refrences since they are better (I have been passing pointers mostly), and I guess this is an example when not to use refrences.   ;  A reference could work just as well; it depends on exactly what you need to do with it and how you want to go about it.   