#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    7
    Rep Power
    0

    Angry Overloading Operators in C


    Is it possible? I haven't found anything stating that it is.
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,625
    Rep Power
    4247
    Nope, operator overloading was introduced in C++.
  4. #3
  5. jasondoucette.com
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    Canada
    Posts
    378
    Rep Power
    12
    You can use operator overloading without getting into classes, though. So, you can use C, and as long as you are using a C++ compiler, you can add operator overloading to the C program (making it a C++ program, but you get the point).
    Last edited by Jason Doucette; March 27th, 2003 at 06:36 AM.
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    7
    Rep Power
    0
    Originally posted by Jason Doucette
    You can use operator overloading without getting into classes, though. So, you can use C, and as long as you are using a C++ compiler, you can add operator overloading to the C program (making it a C++ program, but you get the pointer).
    How? Everytime I declare an operator, it asks for a class.
  8. #5
  9. jasondoucette.com
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    Canada
    Posts
    378
    Rep Power
    12
    If you are programming in C, then you are not using classes. Yet, you are looking to use Operator Overloading. Can you please give me an example of where you are trying to use operator overloading in C without using classes?

    I cannot find any information on operator overloading without classes from the top of my head, but I was fairly certain it was possible. Perhaps if you give me your example, I'll remember more...
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2001
    Location
    Long Beach, California
    Posts
    86
    Rep Power
    13
    Code:
    #include <stdio.h>
    
    int someFunction(int, float);
    void someFunction(char, char *);
    
    int main()
    {
    	someFunction(10, 10.5);
    	someFunction('c', "String");
    
    	return 0;
    }
    
    int someFunction(int x, float y)
    {
    	// y truncates to 10, but thats not the point
    	return x + (int)y;
    }
    
    void someFunction(char x, char *string)
    {
    	printf("%c - %s\n", x, string);
    }
    This code compiles fine in VC++. I'm using function overloading without classes. Of course, function overloading is a C++ only feature, so you have to make sure the file is a .cpp (or .cc or .C) for it to compile correctly.
  12. #7
  13. jasondoucette.com
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    Canada
    Posts
    378
    Rep Power
    12
    Perhaps I was thinking about function overloading when I was talking about operator overloading. Because operator overloading has to be done on a type... and a class is a type. If you wish to do operator overloading on a program that does not have classes, then you are wishing to do it on existing classes (i.e. basic C types). I do not know why anyone would want to do this, to be honest...
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2001
    Location
    Long Beach, California
    Posts
    86
    Rep Power
    13
    Oops, you guys are talking about operator overloading. :o Don't mind me or my post.
  16. #9
  17. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,625
    Rep Power
    4247
    Well, operator overloading was introduced in C++, but it is possible to do operator overloading WITHOUT classes. Strictly speaking, in C++, a struct and a class are very similar, except that class members are private by default and struct members are public by default. So structs in C++ can also have member functions. However, the important word here is C++. You cannot have member functions for structs in C.

    However, for argument's sake, it is also possible to do operator overloading in C++, WITHOUT structs or classes. For example, you could overload the += operator to concatenate an char * and an int. Be prepared to be appalled and please promise not to laugh at my (mis)use of certain C++ features for demo purposes :D:
    Code:
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    using namespace std;
    
    enum dummy {foo};
    
    void operator +=(char *s, dummy y);
    
    void operator +=(char *s, dummy y) {
      char buf[20];
      sprintf(buf, "%d", y);
      strcat(s, buf);
    }
    
    int main(void){
      dummy x;
      char buf[100] = "The answer to life, the universe and everything is ";
    
      cout << "Before addition: " << buf << endl;
      x = (dummy)42;
      buf += x;
      cout << "After addition: " << buf << endl;
    
      return 0;
    }
    The above code compiles for me with NO warnings and works just fine with g++ version 2.95.3.
    [edit]Also confirmed that this code works with Visual C++ 6.0[/edit]
    Last edited by Scorpions4ever; March 27th, 2003 at 08:04 PM.
  18. #10
  19. jasondoucette.com
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    Canada
    Posts
    378
    Rep Power
    12
    Originally posted by Scorpions4ever
    Well, operator overloading was introduced in C++, but it is possible to do operator overloading WITHOUT classes. ... it is also possible to do operator overloading in C++, WITHOUT structs or classes.
    Thanks for the code. I was almost positive this was possible. ;)
  20. #11
  21. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    scorpions<< any clue as to what im doing wrong here?
    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define AMAX 23
    
    int operator [](int x, int y);
    
    int operator [](int x, int y){
            if( x < AMAX)
                    return x;
            else
                    return AMAX;
    }
    
    struct myarray{
            int     len;
            char    bar[AMAX + 1];
    } foo;
    
    int
    main(void)
    {
            int     x = 0;
            
            strcpy(foo.bar, "bla bla bla\n");
    
            for(x = 0; x < 100; x++)
                    putchar(foo.bar[x]);
            
            return 0;
    }
    gives: redef.cc:6: `int operator[](int, int)' must be a nonstatic member function
    redef.cc:8: `int operator[](int, int)' must be a nonstatic member function
    on version 3.2

    is it changed to only allow class functions to use it?
  22. #12
  23. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,625
    Rep Power
    4247
    IIRC operators for =, [], () and -> (I might be missing one or two more here) can only be defined as members of a class, not as global functions.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo

IMN logo majestic logo threadwatch logo seochat tools logo