Thread: Generic Operator Overloading

  1. #1
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246

    Question Generic Operator Overloading

    Is it possible to make a generic overloaded operator in a class or interface?
    [EDIT]It gives me an error now.
    Code:
      public static Cat<T> operator +(Cat<T> cat, Cat<T> cat2)
        {
            return (new Cat<T>(cat.name + cat2.name, 0, 1));
        }
    [/EDIT]
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  2. #2
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246

    Take a look at my generic class please:

    Code:
    class Cat<T>
    {
        string name;
        T age;
        T weight;
    
        public Cat(string its_name, T its_age, T its_weight)
        {
            name = its_name;
            age = its_age;
            weight = its_weight;
    
        }
    
        public void Wash()
        {
            Console.WriteLine("Washed successfuly.");
    
        }
    
        public Cat<T> Marry(Cat<T> cat)
        {
            return (this + cat);
    
    
        }
    
        public static Cat<T> operator +(Cat<T> cat, Cat<T> cat2)
        {
            return (new Cat<T>(cat.name + cat2.name, 0, 1));
        }
    
    
    
    
        public T Age
        {
            get { return age; }
        }
    
        public T Weight
        {
            get { return weight; }
        }
    
    
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
    
        public override string ToString()
        {
            return "Cat Name: " + name;
        }
    
        public override bool Equals(object obj)
        {
            return base.Equals(obj);
        }
    
        public override int GetHashCode()
        {
            return base.GetHashCode() + age - weight;
        }
    
        ~Cat()
        {
    
            Console.WriteLine("{0} died.", name);
        }
    
    
    }
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Generic function
    By vin_pll in forum C++ Programming
    Replies: 26
    Last Post: 02-04-2009, 07:36 AM
  2. Problem with generic function
    By jlbfunes in forum C++ Programming
    Replies: 23
    Last Post: 08-17-2008, 05:13 PM
  3. generic printing preferences dialog box
    By stanlvw in forum Windows Programming
    Replies: 8
    Last Post: 06-27-2008, 02:20 AM
  4. Generic Host Proccess Shutdown
    By Tommaso in forum Tech Board
    Replies: 8
    Last Post: 08-14-2003, 11:18 AM
  5. help generic list and temlate
    By rip1968 in forum C++ Programming
    Replies: 1
    Last Post: 04-09-2002, 06:28 PM