overloading in C

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • uday

    overloading in C

    Does C supports overloading. I am thinking no. But some people are
    saying yes. If yes how. Please answer with examples.

    Also in c++ size of empty class is one. why. and what are the
    default methods created in a class. These questions are asked in
    interviews. so please explain.

  • Richard Bos

    #2
    Re: overloading in C

    "uday" <kariudaykiran@ yahoo.co.in> wrote:
    [color=blue]
    > Does C supports overloading. I am thinking no. But some people are
    > saying yes. If yes how.[/color]

    No. In C99, some of the generic math functions are essentially
    overloaded by the Standard, but (thank heavens) there is no mechanism
    for creating overloaded functions, or worse, operators, yourself.
    [color=blue]
    > Please answer with examples.[/color]

    Do your own homework.
    [color=blue]
    > Also in c++[/color]

    C++ is off-topic in comp.lang.c.

    Richard

    Comment

    • S.Tobias

      #3
      Re: overloading in C

      Richard Bos <rlb@hoekstra-uitgeverij.nl> wrote:[color=blue]
      > "uday" <kariudaykiran@ yahoo.co.in> wrote:[/color]
      [color=blue][color=green]
      > > Does C supports overloading. I am thinking no. But some people are
      > > saying yes. If yes how.[/color][/color]
      [color=blue]
      > No. In C99, some of the generic math functions are essentially
      > overloaded by the Standard, but (thank heavens) there is no mechanism
      > for creating overloaded functions, or worse, operators, yourself.[/color]

      Some people (including me) consider C operators as overloaded:
      1 + 2
      1u + 2
      1l + 2
      1. + 2
      mean entirely different operations, and which ones is resolved
      based on the types of the arguments (ie. operands).

      --
      Stan Tobias
      mailx `echo siXtY@FamOuS.Be dBuG.pAlS.INVALID | sed s/[[:upper:]]//g`

      Comment

      • Dan Pop

        #4
        Re: overloading in C

        In <30lsinF31hg75U 2@uni-berlin.de> "S.Tobias" <siXtY@FamOuS.B edBuG.pAlS.INVA LID> writes:
        [color=blue]
        >Richard Bos <rlb@hoekstra-uitgeverij.nl> wrote:[color=green]
        >> "uday" <kariudaykiran@ yahoo.co.in> wrote:[/color]
        >[color=green][color=darkred]
        >> > Does C supports overloading. I am thinking no. But some people are
        >> > saying yes. If yes how.[/color][/color]
        >[color=green]
        >> No. In C99, some of the generic math functions are essentially
        >> overloaded by the Standard, but (thank heavens) there is no mechanism
        >> for creating overloaded functions, or worse, operators, yourself.[/color]
        >
        >Some people (including me) consider C operators as overloaded:
        > 1 + 2
        > 1u + 2
        > 1l + 2
        > 1. + 2
        >mean entirely different operations, and which ones is resolved
        >based on the types of the arguments (ie. operands).[/color]

        They *are* overloaded, but there is no way to extend this overloading in
        your code. Which is exactly what people mean when they ask about operator
        overloading in C.

        A small amount of type-generic stuff (not exactly the same thing as
        function overloading) can be done with function-like macros.

        Dan
        --
        Dan Pop
        DESY Zeuthen, RZ group
        Email: Dan.Pop@ifh.de
        Currently looking for a job in the European Union

        Comment

        • Mark McIntyre

          #5
          Re: overloading in C

          On 25 Nov 2004 11:04:55 GMT, in comp.lang.c , "S.Tobias"
          <siXtY@FamOuS.B edBuG.pAlS.INVA LID> wrote:
          [color=blue]
          >Some people (including me) consider C operators as overloaded:[/color]

          Pardon? In all cases + adds two numbers together. There's nothing 'entirely
          different' about that.
          This compares to C++ where operator+ could be defined to mean absolutely
          anything, at the users' discretion.
          [color=blue]
          >and which ones is resolved based on the types of the arguments (ie. operands).[/color]

          And printf does different things based on its argument types. So what?

          --
          Mark McIntyre
          CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
          CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt >

          Comment

          • S.Tobias

            #6
            Re: overloading in C

            Mark McIntyre <markmcintyre@s pamcop.net> wrote:[color=blue]
            > On 25 Nov 2004 11:04:55 GMT, in comp.lang.c , "S.Tobias"
            > <siXtY@FamOuS.B edBuG.pAlS.INVA LID> wrote:[/color]
            [color=blue][color=green]
            > >Some people (including me) consider C operators as overloaded:[/color][/color]
            [color=blue]
            > Pardon? In all cases + adds two numbers together.[/color]

            No, the C `+' operators do not add two numbers, are not the same as
            Mathematics `+' operation for Real numbers. Most visibly they differ
            in their domain. For other example, C `+' for unsigned implements
            clock arithmetic - equivalent of addition in Rings. For floats again
            C `+' does not add numbers: 1e99 + 1e-99 == 1e99.
            C `+' operator is of course meant to "reasonably " add things, but
            these operations have their own rules and can't be thought of
            as just "addition". IMHO.
            [color=blue]
            > There's nothing 'entirely
            > different' about that.[/color]

            (unsigned)-1 + 1
            (unsigned long)(unsigned)-1 + 1
            obviously differ in value.
            They will differ in machine instructions; the difference is even
            more obvious for integer and float types.

            The compiler decides which instructions to emit depending on
            what the arguments are. What do you call it, if not "overloadin g"?
            [color=blue]
            > This compares to C++ where operator+ could be defined to mean absolutely
            > anything, at the users' discretion.[/color]

            Not exactly: a user cannot redefine operator+ for built-in types.
            But apart of that:
            const Int operator+(const Int& , const Int&);
            const Double operator+(const Double&, const Double&);
            (for some classes Int and Double) are to each other in terms
            of concept of overloading as are:
            (int) + (int)
            (double) + (double)
            - they have different return types, and invoke different instructions.
            You cannot tell what
            a + b
            will do until you know the types of `a' and `b'.
            [color=blue][color=green]
            > >and which ones is resolved based on the types of the arguments (ie. operands).[/color][/color]

            Qed.
            [color=blue]
            > And printf does different things based on its argument types. So what?[/color]

            No, after the arguments have been prepared (read: thrown on the stack;
            ellipsis is a language feature, there's no function or operator call
            yet so we can't speak of overloading here) the sequence of instructions
            issued by the compiler is the same each time. I would say that printf
            is a data-driven function.

            --
            Stan Tobias
            mailx `echo siXtY@FamOuS.Be dBuG.pAlS.INVALID | sed s/[[:upper:]]//g`

            Comment

            • jacob navia

              #7
              Re: overloading in C

              S.Tobias wrote:[color=blue]
              > Mark McIntyre <markmcintyre@s pamcop.net> wrote:
              >[color=green]
              >>On 25 Nov 2004 11:04:55 GMT, in comp.lang.c , "S.Tobias"
              >><siXtY@FamOuS .BedBuG.pAlS.IN VALID> wrote:[/color]
              >
              >[color=green][color=darkred]
              >>>Some people (including me) consider C operators as overloaded:[/color][/color]
              >
              >[color=green]
              >>Pardon? In all cases + adds two numbers together.[/color]
              >
              >
              > No, the C `+' operators do not add two numbers, are not the same as
              > Mathematics `+' operation for Real numbers. Most visibly they differ
              > in their domain. For other example, C `+' for unsigned implements
              > clock arithmetic - equivalent of addition in Rings. For floats again
              > C `+' does not add numbers: 1e99 + 1e-99 == 1e99.
              > C `+' operator is of course meant to "reasonably " add things, but
              > these operations have their own rules and can't be thought of
              > as just "addition". IMHO.
              >[/color]

              Exactly.

              This fact, and the fact that the C99 comitee introduced overloaded
              functions like sqrt, sin, pow, etc, led me to implement operator
              overloading within a C context. This has been very useful to implement
              all kind of numbers just extending the C99 overloaded functions to them.

              For instance the qfloat type (350 bits) has been implemented this way.

              To avoid gratuituos incompatibiliti es with C++ I use the same syntax.

              qfloat operator+(qfloa t &,qfloat b);

              This has been atacked ad nauseum by the conservatives in this group,
              and I do not want to rehash this polemic for the 1000th time.

              The rationale for this is as follows:

              If the designers of C99 needed this feature and proposed an overloaded
              sqrt/pow/sin/ etc functions why shouldn't the user be able to solve this
              in his/her context as well?

              If the implementor of a language needs a feature, the user needs it as
              well and should be allowed to have it.

              jacob


              Comment

              • Michael Mair

                #8
                Re: overloading in C

                jacob navia wrote:
                [color=blue]
                > S.Tobias wrote:
                >[color=green]
                >> Mark McIntyre <markmcintyre@s pamcop.net> wrote:
                >>[color=darkred]
                >>> On 25 Nov 2004 11:04:55 GMT, in comp.lang.c , "S.Tobias"
                >>> <siXtY@FamOuS.B edBuG.pAlS.INVA LID> wrote:[/color]
                >>[color=darkred]
                >>>> Some people (including me) consider C operators as overloaded:[/color]
                >>[color=darkred]
                >>> Pardon? In all cases + adds two numbers together.[/color]
                >>
                >> No, the C `+' operators do not add two numbers, are not the same as
                >> Mathematics `+' operation for Real numbers. Most visibly they differ
                >> in their domain. For other example, C `+' for unsigned implements
                >> clock arithmetic - equivalent of addition in Rings. For floats again
                >> C `+' does not add numbers: 1e99 + 1e-99 == 1e99.
                >> C `+' operator is of course meant to "reasonably " add things, but
                >> these operations have their own rules and can't be thought of
                >> as just "addition". IMHO.[/color]
                >
                > Exactly.
                >
                > This fact, and the fact that the C99 comitee introduced overloaded
                > functions like sqrt, sin, pow, etc, led me to implement operator
                > overloading within a C context. This has been very useful to implement
                > all kind of numbers just extending the C99 overloaded functions to them.
                >
                > For instance the qfloat type (350 bits) has been implemented this way.
                >
                > To avoid gratuituos incompatibiliti es with C++ I use the same syntax.
                >
                > qfloat operator+(qfloa t &,qfloat b);
                >
                > This has been atacked ad nauseum by the conservatives in this group,[/color]
                ad nause_a_m[color=blue]
                > and I do not want to rehash this polemic for the 1000th time.[/color]

                Well, keep the advertising of non-standard solutions clearly separated
                from your standard C answers. I, for one, am interested in a look
                behind the scenes but not necessarily in this newsgroup.

                [color=blue]
                > The rationale for this is as follows:
                >
                > If the designers of C99 needed this feature and proposed an overloaded
                > sqrt/pow/sin/ etc functions why shouldn't the user be able to solve this
                > in his/her context as well?
                >
                > If the implementor of a language needs a feature, the user needs it as
                > well and should be allowed to have it.[/color]

                I think this is a "dangerous" argument as this would mean that I
                should be able to do the same things all standard library functions
                do within the scope of the language. And portably.
                The repercussions of this are IMO very clear and not very nice.

                Operator overloading would be a nice feature in more than one setting
                but may complicate things in a way that we end up with people
                doing subsetting where it is not necessary _now_.

                As long as you clearly keep everything separate and full
                conformance to the original language is kept, that is fine with
                me, though.


                BTW: Are you going to go for full C99 conformance? I would be
                more interested in a "real" C99 compiler rather than have C++
                features wander over. (And no, I do not need anyone slandering
                Jacob and his compiler along the vein of "He cannot provide even
                C89".)


                Cheers
                Michael
                --
                E-Mail: Mine is an /at/ gmx /dot/ de address.

                Comment

                • dandelion

                  #9
                  Re: overloading in C


                  "Michael Mair" <Michael.Mair@i nvalid.invalid> wrote in message
                  news:30o8ppF2rc lbuU1@uni-berlin.de...[color=blue]
                  > jacob navia wrote:
                  >[color=green]
                  > > S.Tobias wrote:
                  > >[color=darkred]
                  > >> Mark McIntyre <markmcintyre@s pamcop.net> wrote:
                  > >>
                  > >>> On 25 Nov 2004 11:04:55 GMT, in comp.lang.c , "S.Tobias"
                  > >>> <siXtY@FamOuS.B edBuG.pAlS.INVA LID> wrote:
                  > >>
                  > >>>> Some people (including me) consider C operators as overloaded:
                  > >>
                  > >>> Pardon? In all cases + adds two numbers together.
                  > >>
                  > >> No, the C `+' operators do not add two numbers, are not the same as
                  > >> Mathematics `+' operation for Real numbers. Most visibly they differ
                  > >> in their domain. For other example, C `+' for unsigned implements
                  > >> clock arithmetic - equivalent of addition in Rings. For floats again
                  > >> C `+' does not add numbers: 1e99 + 1e-99 == 1e99.
                  > >> C `+' operator is of course meant to "reasonably " add things, but
                  > >> these operations have their own rules and can't be thought of
                  > >> as just "addition". IMHO.[/color]
                  > >
                  > > Exactly.
                  > >
                  > > This fact, and the fact that the C99 comitee introduced overloaded
                  > > functions like sqrt, sin, pow, etc, led me to implement operator
                  > > overloading within a C context. This has been very useful to implement
                  > > all kind of numbers just extending the C99 overloaded functions to them.
                  > >
                  > > For instance the qfloat type (350 bits) has been implemented this way.
                  > >
                  > > To avoid gratuituos incompatibiliti es with C++ I use the same syntax.
                  > >
                  > > qfloat operator+(qfloa t &,qfloat b);
                  > >
                  > > This has been atacked ad nauseum by the conservatives in this group,[/color]
                  > ad nause_a_m[color=green]
                  > > and I do not want to rehash this polemic for the 1000th time.[/color]
                  >
                  > Well, keep the advertising of non-standard solutions clearly separated
                  > from your standard C answers. I, for one, am interested in a look
                  > behind the scenes but not necessarily in this newsgroup.
                  >
                  >[color=green]
                  > > The rationale for this is as follows:
                  > >
                  > > If the designers of C99 needed this feature and proposed an overloaded
                  > > sqrt/pow/sin/ etc functions why shouldn't the user be able to solve this
                  > > in his/her context as well?
                  > >
                  > > If the implementor of a language needs a feature, the user needs it as
                  > > well and should be allowed to have it.[/color]
                  >
                  > I think this is a "dangerous" argument as this would mean that I
                  > should be able to do the same things all standard library functions
                  > do within the scope of the language. And portably.
                  > The repercussions of this are IMO very clear and not very nice.[/color]

                  My major concern is that 'crreeping featurism' will make 'C' a lot 'fatter'
                  and less usable on very small platforms. As it is, i have trouble enough
                  with memory-footprints and the thing I like about 'C' is that good compilers
                  tend to churn out nice and small code.

                  Operator overloading, IMHO is one of the nicer features of C++, allthough it
                  does have the potential of blatant misuse. OTOH, almost any language feature
                  has that.

                  <snip>
                  [color=blue]
                  > (And no, I do not need anyone slandering
                  > Jacob and his compiler along the vein of "He cannot provide even
                  > C89".)[/color]

                  Expect no slander from me. Anyone who succeeds in implementing serious
                  compilers (in contrast to toy-languages) has my full admiration. The attempt
                  alone will yield my applause and best wishes.

                  dandelion


                  Comment

                  • jacob navia

                    #10
                    Re: overloading in C

                    Michael Mair wrote:[color=blue]
                    > BTW: Are you going to go for full C99 conformance? I would be
                    > more interested in a "real" C99 compiler rather than have C++
                    > features wander over. (And no, I do not need anyone slandering
                    > Jacob and his compiler along the vein of "He cannot provide even
                    > C89".)[/color]

                    The big part of this is the writing of a C99 compliant C library.

                    It is done for the math.h, but I have still some problems with complex
                    numbers. I have implemented all complex numbers a long double _Complex
                    to avoid the incredible proliferation of library functions needed
                    to support all combinations of complex functions:

                    double _Complex casin(double _Complex)
                    float _Complex casinf(float _Complex)
                    long double _Complex casinl(long double _Complex)

                    etc!

                    What language features is concerned I support already variable
                    length arrays, dynamically allocate data in the stack, and actually
                    most features.

                    Missing are:

                    named arguments initialization
                    variable arguments macros

                    I support already anonymous literals, and all other features.

                    jacob

                    Comment

                    • Chris Croughton

                      #11
                      Re: overloading in C

                      On Fri, 26 Nov 2004 08:18:58 +0100, jacob navia
                      <jacob@jacob.re mcomp.fr> wrote:
                      [color=blue]
                      > This fact, and the fact that the C99 comitee introduced overloaded
                      > functions like sqrt, sin, pow, etc, led me to implement operator
                      > overloading within a C context. This has been very useful to implement
                      > all kind of numbers just extending the C99 overloaded functions to them.
                      >
                      > For instance the qfloat type (350 bits) has been implemented this way.
                      >
                      > To avoid gratuituos incompatibiliti es with C++ I use the same syntax.
                      >
                      > qfloat operator+(qfloa t &,qfloat b);[/color]

                      How did you implement it in standard C? That is not valid C syntax, it
                      will give an error on any conforming C compiler.

                      If you mean that you wrote a compiler for some language C(extended)
                      which is neither C nor C++ but which contains some features from both,
                      that is your privilege but it has nothing to do with either C or C++.
                      [color=blue]
                      > If the designers of C99 needed this feature and proposed an overloaded
                      > sqrt/pow/sin/ etc functions why shouldn't the user be able to solve this
                      > in his/her context as well?[/color]

                      Because it isn't in the standard, therefore programs using it won't be
                      portable. If you want to suggest it for the standard, join the
                      appropriate standards organisations and make your point there.
                      [color=blue]
                      > If the implementor of a language needs a feature, the user needs it as
                      > well and should be allowed to have it.[/color]

                      In the case of typeof() (which is needed -- or some similar mechanism --
                      to implement the overloaded maths functions), I agree that they should
                      have put it into the language. And/or left out the weird overloaded
                      functions (which would be my preference, I see no point in them and a
                      number of potential bugs).

                      Chris C

                      Comment

                      • jacob navia

                        #12
                        Re: overloading in C

                        The memory footprint of the generated code doesn't change
                        at all.

                        It is the same code size if I write:

                        qfloat c = AddQfloat(a,b);

                        or if I write:

                        qfloat c = a+b;

                        Note that IN C, there isn't any constructors/destructors
                        or other implicit function calls. The code generated will
                        be a call to the user defined function.

                        Besides, this is completely an optional feature:
                        you do not use it?

                        You do NOT pay for it. Yes, the compiler will be slightly larger
                        but the generated code STAYS 100% the same!

                        This is just syntatic sugar for function calls,
                        allowing for a more rational notation.

                        Operator overloading is best used for numbers, numbers of
                        any kind.

                        Example 1:
                        ----------
                        Within many embedded systems, fixed point numbers are common.
                        The problem is that all software must be rewritten to use them:

                        For this code:

                        double c = 2.3;
                        c /= (c*49.876);

                        You have to rewrite:

                        FixedPoint c = DoubleConstantT oFixed(2.3);
                        c = DivFixed(c,Mult Fixed(c,DoubleC onstantToFixed( 49.876)));

                        It would be MUCH better to be able to just port the code like this

                        FixedPoint c = 2.3;
                        c /= (c*49.876);

                        isn't it?

                        With operator overloading you just supply the conversion functions!

                        This allows you to easily port floating point software to a platform
                        without FPU without adding a HUGE floating point emulator and with a lot
                        of less effort.

                        There are some standadization attempts to be able to use fixed point
                        in embedded systems, but they are much less general and much less easy
                        to use than this simple enhancement of the basic language.

                        Example 2
                        ---------
                        A similar problem appears when you try to implement numbers that support
                        clamped addition (MMX in the x86). For instance

                        Current c:

                        unsigned char c = 254;
                        unsigned char d = 3;

                        char r = c+d; // r is now 1

                        Clamped addition:
                        r = c+d ; // r is now 255

                        To be able to do this you have to rewrite all code using
                        function calls.

                        r = ClampledAdd(c,d );

                        Clamped addition is necessary when you are adding intensities
                        for instance, to avoid that adding a bit of white to an
                        almost white color results in a black pixel!

                        jacob

                        Comment

                        • Dan Pop

                          #13
                          Re: overloading in C

                          In <30o8ppF2rclbuU 1@uni-berlin.de> Michael Mair <Michael.Mair@i nvalid.invalid> writes:
                          [color=blue]
                          >BTW: Are you going to go for full C99 conformance? I would be
                          >more interested in a "real" C99 compiler rather than have C++
                          >features wander over. (And no, I do not need anyone slandering
                          >Jacob and his compiler along the vein of "He cannot provide even
                          >C89".)[/color]

                          I'm entirely convinced that Jacob *can* provide full C89 conformance,
                          my criticism is that he doesn't do it when the compiler is invoked in
                          ANSI conforming mode. More precisely, that his ANSI conforming mode
                          doesn't conform to any known C standard. Which is completely
                          disqualifying it from being advertised in this newsgroup.

                          Dan
                          --
                          Dan Pop
                          DESY Zeuthen, RZ group
                          Email: Dan.Pop@ifh.de
                          Currently looking for a job in the European Union

                          Comment

                          • Dan Pop

                            #14
                            Re: overloading in C

                            In <41a6f020$0$308 47$e4fe514c@dre ader7.news.xs4a ll.nl> "dandelion" <dandelion@mead ow.net> writes:
                            [color=blue]
                            >Expect no slander from me. Anyone who succeeds in implementing serious
                            >compilers (in contrast to toy-languages) has my full admiration. The attempt
                            >alone will yield my applause and best wishes.[/color]

                            The compiler was written by other people. Jacob merely "extended" it.

                            Dan
                            --
                            Dan Pop
                            DESY Zeuthen, RZ group
                            Email: Dan.Pop@ifh.de
                            Currently looking for a job in the European Union

                            Comment

                            • Dan Pop

                              #15
                              Re: overloading in C

                              In <41a6d8e2$0$906 7$8fcfb975@news .wanadoo.fr> jacob navia <jacob@jacob.re mcomp.fr> writes:
                              [color=blue]
                              >The rationale for this is as follows:
                              >
                              >If the designers of C99 needed this feature and proposed an overloaded
                              >sqrt/pow/sin/ etc functions[/color]

                              They're merely type-generic *macros*, not overloaded functions. They're
                              modelled after FORTRAN 77's generic functions and not after *any* C++
                              feature. And the only language extension needed to implement them is
                              something like gcc's __builtin_types _compatible_p() .
                              [color=blue]
                              >why shouldn't the user be able to solve this
                              >in his/her context as well?[/color]

                              Because the user knows where to find C++ if he needs this feature.

                              One of the *fundamental* properties of C code is that it can be taken at
                              face value (very little happens behind the scenes). Operator and
                              function overloading destroy this property, so they cannot be considered
                              as harmless extensions to the language.

                              A C standard introducing support for function/operator overloading would
                              be even less popular among the C community than C99.

                              C89 did a fairly good job of adopting *all* the C++ features that could be
                              integrated into C without changing the fundamental nature of the language.

                              I don't mind new features into the language (all GNU C extensions are OK)
                              as long as the spirit of the language designed by Dennis Ritchie is
                              preserved.

                              Dan
                              --
                              Dan Pop
                              DESY Zeuthen, RZ group
                              Email: Dan.Pop@ifh.de
                              Currently looking for a job in the European Union

                              Comment

                              Working...