Quote:Original post by vivendi
Now this means that i stored another copy of classA into classB right?? (Passed as value is what it's called i believe?)
Yup. FYI, classA's copy constructor is invoked on the object that you're passing to classB's constructor. The constructor, copy-constructor, assignment operator and destructor will automatically be created for a class if they're needed and if you didn't provide an implementation yourself. For complex classes (e.g. classes that manage resources such as dynamically allocated memory, file handles, that sort of stuff) you'll want to provide your own implementation (or, if copying such an object doesn't make sense, you can hide the copy-constructor and assignment operator by making them private).
Your code is incorrect, bytheway: new returns a pointer, so you'll have to store the result in a pointer: classB* classb = new classB(classa);
Quote:I think it's better to pass it as reference right?
Depends on what classB needs to do with that classA object. Sometimes a copy makes sense, sometimes using a reference is better, sometimes you'll have to pass a pointer around.
Quote:Would the constructor of classB be looking like this then?
classB( ClassA* classa );
No, that's a pointer. Now, a pointer is a sort of reference to an object, but in C++, a pointer can point to null, or to a non-existing object, stuff like that. If you want to make it clear that classB needs a valid, existing object, pass a reference instead: classB(classA& classa); This can be called as if you were passing a copy: classB* classb = new classB(classa); Either way, you should read up on the differences between pointers and references.
Quote:and in my main function i'd pass the instance to classB like so:
classB classb = new classb ( &classa );
With a pointer, that's right: the & operator here gives you the address of the classa object, e.g. a pointer to classa.
Quote:I still want to be able to change something in classA and the instance in classB must also know about it.
Then you'll need a pointer or reference indeed.
This raises some more issues however: who has ownership of that classA object? Can a classB object be sure that the classA object that it holds a pointer or reference to will always exist for as long as that classB object exists? Perhaps you'll want to use a smart pointer instead, for example a shared pointer that ensures the object is not destroyed for as long as some code still holds a shared pointer to it (e.g. a reference counted pointer)?
Just some (important) food for thought and for research. :)