UObject derived class and copy constructor

Hi all. First time poster here (I think)

I’ve got a (hopefully) quick question: can you declare a copy constructor in a class that is derived from UObject? I’m certainly not able to. Visual studio tells me that the “member function already defined or declared, see declaration of …”. I read somewhere that you should use FORCEINLINE before declaration and definition, but in my experience it doesn’t change anything.

Here is an example that i found on stackoverflow on how you would normally do it :


class Base
{
protected:
    int m_nValue;

public:
    Base(int nValue)
        : m_nValue(nValue)
    {
    }

    const char* GetName() { return "Base"; }
    int GetValue() { return m_nValue; }
};

class Derived: public Base
{
public:
    Derived(int nValue)
        : Base(nValue)
    {
    }
    Derived( const Derived &d ){
        std::cout << "copy constructor
";
    }

    const char* GetName() { return "Derived"; }
    int GetValueDoubled() { return m_nValue * 2; }
};

As far as I am aware of, no you cannot declare a copy constructor. UE4 C++ programming is a bit different from your standard C++ method of programming. Most of the concepts are the same but UE4 defines a lot of additional functions and macros that you need to stay within. On any class that is exposed to the Unreal Engine Editor (that is to say any UClass) you do not use the new or delete operators. Depending on your base class you will either use NewObject for any UObject or UActorComponent or Spawn for anything that is derrived from AActor. Constructors are similar, effectively there are two constructors you can declare in UE4. Your standard constructor:


UMyObject()

or your initializer constructor


UMyObject(const FObjectInitializer * objectInitializer);

I have seen some classes use Destructors but in general you do not need to unless you are using a Non UClass object and need to clean up your memory allocations manually. UE4 UClasses are Garbage Collected automatically and so new and delete operations are not used on them.

For the Copy Constructor both NewObject and Spawn have a Template parameter. This is effectively how you do a copy the values from one object to another.

Declaration of NewObject:


T* NewObject<T>(UObject * outer, UClass * type, FName name, UObject const* Template)

usage:


UMyObject * newObject = NewObject<UMyObject>(someOtherObject, MyObject::StaticClass(), NAME_None, templateObject);