I'm trying to convert My Console classes to .Net classes because I can't use std::vector or anything like that in WindowsFormsApplications. So I decided to convert all Vectors to Lists and I thought that the classes work the same but my code below throws a massive amount of errors and I have no clue why.. It's quite short and simple to read.

using namespace System;
using namespace System::Collections::Generic;

template <typename T>
public ref class CustomType
{
    private:
        List<T> TypeData;

    public:
        CustomType() {}
        CustomType(...array<T^> ^Types)          //Basically Variadic Templates.. As good as it gets for MS2010..
        {
            for each (T^ o in Types)
                TypeData.Add(o);
        }
        ~CustomType() {}

        int size() {return TypeData.size();}

        CustomType& operator [](int I)
        {
            assert(I >= 0 && !TypeData.empty());
            return TypeData[I];
        }

        const CustomType& operator [](int I) const
        {
            assert(I >= 0 && !TypeData.empty());
            return TypeData[I];
        }

        operator const List<T>& () const
        {
            return TypeData;
        }

        bool operator == (const CustomType &CT) const
        {
            return (TypeData == CT.TypeData);
        }
};

I use the above like this:

    private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
                 CustomType<int> Meh(0, 1, 2, 3, 4);
             }

But that does not work at all.. It's basically supposed to add Meh to the list within the class and I can access it at anytime to get the data.

Please help me. How do I overload in .Net and what is that Shevron? The ^ operator? I read that it's top level but have no clue what I read because I understood very little. Does ^ Replace the & operator?

Ok this is my new header but I cannot for the life of me get the operator == overloaded.. Help me please!

I just want to be able to do if (CustomtypeA == CustomtypeB).. But it keeps telling me that
error C2679: binary '==' : no operator found which takes a right-hand operand of type 'CustomType<T>' (or there is no acceptable conversion)

#ifndef TEMPLATES_H_INCLUDED
#define TEMPLATES_H_INCLUDED
using namespace System;
using namespace System::Collections::Generic;

template <typename T>
public ref class CustomType
{
    private:
        List<T> TypeData;

    public:
        CustomType() {}
        CustomType(...array<T^> ^Types)          //Basically Variadic Templates.. As good as it gets for MS2010..
        {
            for each (T^ t in Types)
            {
                TypeData.Add(*t);
            }
        }
        ~CustomType() {}

        int size()
        {
            return (TypeData.Count);
        }

        CustomType^ operator [](int I)
        {
            assert(I >= 0 && (TypeData.size() != 0));
            return TypeData[I];
        }

        void operator()(...array<T^> ^Types)
        {
            for each (T^ t in Types)
            {
                TypeData.Add(*t);
            }
        }

        operator const List<T>^ ()
        {
            return TypeData;
        }

        bool operator ==(CustomType ^CT)
        {
            return (this->TypeData == CT->TypeData);
        }

        bool operator !=(const CustomType ^CT)
        {
            return !(this->TypeData == CT.TypeData);
        }

        CustomType^ operator =(const CustomType ^CT)
        {
            if (this != &CT)
            {
                if (TypeData.size() > 0)
                    TypeData.Clear();

                for (size_t I = 0; I < CT.TypeData.size(); I++)
                    TypeData.Add(*CT.TypeData[I]);
            }
            return this;
        }

        CustomType^ operator << (const T^ t)
        {
            TypeData.Add(*t);
            return this;
        }

        virtual String^ ToString() override
        {
            String^ Str;
            if (this->size() != 0)
            {
                Str = "[";
                for (int I = 0; I < this->size() - 1; I++)
                {
                    Str += TypeData[I];
                    Str += ", ";
                }
                Str += TypeData[this->size() - 1];
                Str += "]";
            }
            else
            {
                Str += "[]";
            }
            return Str;
        }
};

#endif // TEMPLATES_H_INCLUDED

This class seems overly complicated.
What is it supposed to do?

One way to override the comparison operator is to do this:

virtual bool Equals(Object^ obj) override
{
  return this->ToString()->Equals(((CustomType^)obj)->ToString());
}

It might be the only way.
Two other overrides you will need will be that of GetHashCode() and ToString()

I've overridden ToString already. I'm not sure what the GetHashCode is for.

Also the class is actually so that I can make custom types. Like arrays of any types without having to worry about sizes and stuff. I can add to the array anytime and remove from it at any time. I can add any amount of parameters at once to the array. It can be of type string, int, bool, etc..

I use it like:

CustomType<int> IA(1, 2, 3, 4, 5, 6, 7...... ); //Any amount..
CustomType<string> SA("one", "two", "three", "four"...); //etc..
CustomType<string> SA2("meh", "bleh", "waw.");
CustomType<CustomType<int>> I2DA;  //A 2Dimensional Array of Integers.

SA = SA2;  //This would remove all elements from SA and copy the ones from SA2.
SA == SA2; //I compare the elements for equality.
MessageBox::Show(SA.ToString);  //This prints all elements in SA to look like:  ["A", "B", "C"]..

Also I know I can just do this with a List but it's more work the more I use it as every time I'd be defining and enumerable type and I can't easily add a large amount of elements as easily as the above.. Even though Lists have AddRange Properties. It's not the same. It's the ease of access.

Hmm that really is the only way to override the comparrison? Is that how I can override the assignment as well?

Edited 4 Years Ago by triumphost

Does ^ Replace the & operator?

No. The ^ is equivalent to *, while the % is equivalent to the &. Except that the ^ and % are for garbage-collected (or .NET) objects only. In other words, you allocate a GCed object with MyClass ^ obj = gcnew MyClass();. If you're going to do any C++/CLI, you should at least read up on the basics, the wiki page answers all the questions you asked so far.

Comments
Very Very helpful. Solved it

Also the class is actually so that I can make custom types

That has already been taken care of with Template/Generic classes.

Be careful not to drag old-school baggage with you as you learn CLI.
After C++ and MFC, I learned C#, THEN CLI.
I had originally tried CLI, but it seemed confusing (until I learned C#).
There are lots of things you no longer need to write (with CLI).

Comments
Very very helpful

Mike.. I love you bro. That tiny bit of information saved the day! And thines thank you for everything! Both +Rep that's for sure.

This is the completed class (Works exactly as it's supposed to) And it's templated Thines:

#ifndef TEMPLATES_H_INCLUDED
#define TEMPLATES_H_INCLUDED
using namespace System;
using namespace System::Collections::Generic;

template <typename T>
public ref class CustomType sealed
{
    private:
        List<T> TypeData;

    public:
        CustomType() {}
        CustomType(...array<T^> ^Types)          //Basically Variadic Templates.. As good as it gets for MS2010..
        {
            for each (T^ t in Types)
            {
                TypeData.Add(*t);
            }
        }
        ~CustomType() {}

        int size()
        {
            return (TypeData.Count);
        }

        T^ operator [](int I)
        {
            if (I < 0 && (this->size() == 0))
                throw gcnew System::ArgumentException("Parameter cannot be null");

            return TypeData[I];
        }

        void operator()(...array<T^> ^Types)
        {
            for each (T^ t in Types)
            {
                TypeData.Add(*t);
            }
        }

        operator const List<T>^ ()
        {
            return TypeData;
        }

        bool operator == (CustomType %CT)
        {
            for (int I = 0; I < CT.size(); I++)
            {
                if (this->TypeData[I] != CT.TypeData[I])
                    return false;
            }
            return true;
        }

        bool operator != (CustomType %CT)
        {
            for (int I = 0; I < CT.size(); I++)
            {
                if (this->TypeData[I] == CT.TypeData[I])
                    return false;
            }
            return true;
        }

        CustomType^ operator = (CustomType %CT)
        {
            if (this != %CT)
            {
                if (this->size() > 0)
                    TypeData.Clear();

                for (int I = 0; I < CT.size(); I++)
                    TypeData.Add(CT.TypeData[I]);
            }
            return this;
        }

        CustomType^ operator << (const T^ t)
        {
            TypeData.Add(*t);
            return this;
        }

        virtual String^ ToString() override
        {
            String^ Str;
            if (this->size() != 0)
            {
                Str = "[";
                for (int I = 0; I < this->size() - 1; I++)
                {
                    Str += TypeData[I];
                    Str += ", ";
                }
                Str += TypeData[this->size() - 1];
                Str += "]";
            }
            else
            {
                Str += "[]";
            }
            return Str;
        }
};

#endif // TEMPLATES_H_INCLUDED
This question has already been answered. Start a new discussion instead.