(everything seems to be an object???)
The difference is that you can never access an object directly in C#, only by reference. Objects also only get passed by reference, so if you're coming from C++, you have to get used to explicitly copying stuff if you need duplicate data.
Im not sure I follow all of that. Can you explain that a bit more, ie with a piece of code?
class X {
};
class Y {
X x;
};
class Z {
X &x;
};
public class X {
};
public class Y {
private X x;
};
Well, in C++, you could for example put a VECTOR3 into a vessel. when you create the vessel, memory for the VECTOR3 is automatically reserved together with the vessel.
Now, in C# or Java, objects are always references, so instead of reserving memory for the VECTOR3, you would reserve memory for a reference to an object of the class VECTOR3. You need to reserve memory for this class (or a subclass of it) manually (or let a factory class do that) and store a reference to that memory.
It is much like pointers or references in C++, but like Java, C# also has garbage collection: The references to a class can change and point to different memory locations, if needed, without the programmer having much control over it. When a class has no longer references on it, it is marked for disposal and will be removed from memory should the garbage collection clean up the next time. And contrary to C++, you can not prevent objects from being references (or pointers), they simply are by definition.
so in other words, everything not a primitive in C# is like an instance of std::unique_ptr? (save for that it only cleans itself up when the last scope to it is finished I think?)
Think of something also moving your classes in memory to free memory for larger objects.
Im not sure I follow all of that. Can you explain that a bit more, ie with a piece of code?
MyObject object_one(myargs);
MyObject object_two;
object_two = object_one;
object_two.Clear();
MyObject object_one = new MyObject(myargs); //note that you can't call the contructor directly without calling new. This is because this is in fact an uninitialised reference
MyObject object_two;
object_two = object_one;
object_two.Clear();
MyObject *object_one = new MyObject(myargs);
MyObject *object_two;
object_two = object_one;
object_two->Clear();
Well, that's basically an STL vector... :lol:
but if you put a pointer into it, you can expect it to remain constant.
I know of course... otherwise its use would be rather limited.
Yeah - but thats different inside C# or Java. In Java, you have thus no pointer math at all, in C# there is just another compromise to combine both worlds and drive you crazy.
public class BouyancyScript : MonoBehaviour {
public class BouyancyScript : MonoBehaviour, DiverPhysObject {
(actually, Bouyancy would be part of the phys, so wouldn't be inheriting DiverPhysObject, but you get the point...)Code:public class BouyancyScript : MonoBehaviour, DiverPhysObject {
public class BouyancyScript : MonoBehaviour, IDiverPhysObject {
public class BouyancyScript : DiverPhysObject, IBehaviour{
Be aware that C# does not allow multiple class inheritence, in contrast to C++. You can't derive your BouyancyScript from both the MonoBehaviour and the DiverPhysObject class (if they ARE classes, that is).
What you can do, though, is defining multiple interfaces your class can implement. Something like this would work (of course depending on what Unity really offers):
orCode:public class BouyancyScript : MonoBehaviour, IDiverPhysObject {
Code:public class BouyancyScript : DiverPhysObject, IBehaviour{
And I take it that an interface is a distinct concept from a class?
:nono: Guys, we don't have to program another simulator, why don't we just replace the Dg's model with a sub, change the textures of Titan and its atmosphere and use titan with orelux.:lol:
Yes and no. In C++, interfaces would be just classes, because C++ does not know interfaces.
But practically, it is a different concept and in many other languages also a special language element.
Interfaces only describe the signatures of functions and properties, that a class must provide to implement this interface. An interface does not implement any of these functions or properties - it leaves it to the class that implements it.
But you can treat an interface like a class for calling such functions. You can not create an instance of an interface (with "new"), because every instance is considered abstract but you can assign any object to a reference of that interface, that implements the interface.
so an interface is kinda like a virtual class?