struct "overloading"

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

    struct "overloading"

    Hi,
    Is there any way to "overload" a struct?

    e.g.
    having already
    struct stA1
    {
    int i_ID;
    int i_Type;
    };

    and adding something like
    struct stA1
    {
    int i_ID;
    int i_Person;
    };

    I tried to use a namespace on the second "overoloadi ng" struct like

    namespace KPT1_1
    {
    struct stA1
    {
    int i_ID;
    int i_Person;
    };
    }

    with not much luck... any clever ideas?
    Thanks a lot
    Telis

  • Yazy

    #2
    Re: struct "overloadi ng"

    What do you mean "overload"?

    Your compiler reports no erroe while compling you codes above?

    If the first stA1 struct and the second one is in the different .cpp
    files, some compiler wouldn't report you errors, but if you instance an
    object like:

    stA1 a;

    the instance "a" may not be the one you expected, maybe "a" is an
    instance of the first stA1, maybe is the second, depends on what? I am
    also mess on it...

    Comment

    • Pantokrator

      #3
      Re: struct "overloadi ng"

      Thanks for replying.
      I mean that I would like to have 2 structs with the same name as stA1
      on the same header file. It compiles fine, but whenever I try to fill
      in the variable i_Person

      ....
      KPT1_1::stA1 *myStruct=0;
      myStruct->i_Person = 1; // that NEVER APPEARS. I get an error here

      instead the compiler recognises only myStruct->i_Type which is not what
      I want at that particular piece of code.

      Additionally, I tried something with an anonymous struct and it
      compiles fine:

      struct stA1
      {
      int i_ID;
      int i_Type;

      struct
      {
      int i_ID;
      int i_Person;
      };
      };

      and i am calling myStruct->i_Person = 1; without any problem
      Theoritically speaking does anybody knows how does an anonymous struct
      work within a named-struct from the compiler point of view?

      Thanks
      Telis

      Comment

      • Michiel.Salters@tomtom.com

        #4
        Re: struct "overloadi ng"

        Pantokrator wrote:[color=blue]
        > Thanks for replying.
        > I mean that I would like to have 2 structs with the same name as stA1
        > on the same header file. It compiles fine, but whenever I try to fill
        > in the variable i_Person
        >
        > ...
        > KPT1_1::stA1 *myStruct=0;
        > myStruct->i_Person = 1; // that NEVER APPEARS. I get an error here[/color]

        No wonder it doesn't appear, it's in a previous post ;)

        Seriously, please include enough context in every post, quoting the
        parts
        that are needed. E.g. the definition of stA1.

        That said, I guess that "NEVER APPEARS" refers to your IDE. This is
        quite
        common; many IDEs have problems with two structures with similar names
        in
        different namespaces. Jsut type in what should be there, ignoring IDE
        suggestions. The real compiler does know what members myStruct has.

        HTH,
        Michiel Salters

        Comment

        • Pantokrator

          #5
          Re: struct "overloadi ng"

          Thanks Michiel ,
          In fact I did mention the structure of stA1 in my original post.
          Yes you are right, probably I did not clarify very well..
          "NEVER APPEARS" refers to my IDE, which is not that important. What is
          important is the sentence next to it "I get an error here " which
          refers to the compiler and the error message I am getting. Sorry about
          that.

          Could anybody please explain to me the question rarised by my previous
          post?

          "..Additionally , I tried something with an anonymous struct and it
          compiles fine:

          struct stA1
          {
          int i_ID;
          int i_Type;

          struct
          {
          int i_ID;
          int i_Person;
          };
          };

          and i am calling myStruct->i_Person = 1; without any problem
          Theoritically speaking does anybody knows how does an anonymous struct
          work within a named-struct from the compiler point of view? "..

          I apologise for reposting
          Thanks
          Telis

          Comment

          • Tomás

            #6
            Re: struct "overloadi ng"

            Pantokrator posted:
            [color=blue]
            > Hi,
            > Is there any way to "overload" a struct?
            >
            > e.g.
            > having already
            > struct stA1
            > {
            > int i_ID;
            > int i_Type;
            > };
            >
            > and adding something like
            > struct stA1
            > {
            > int i_ID;
            > int i_Person;
            > };[/color]



            You haven't added anything -- you've changed the name of a member object.


            [color=blue]
            > I tried to use a namespace on the second "overoloadi ng" struct like
            >
            > namespace KPT1_1
            > {
            > struct stA1
            > {
            > int i_ID;
            > int i_Person;
            > };
            > }[/color]


            I haven't got a bull's notion of what you're trying to do.


            Are you trying to give a field two different names? If so:

            struct stA1
            {
            int i_ID;
            union {
            int i_Person;
            int i_Type;
            };
            }

            -Tomás

            Comment

            • Rolf Magnus

              #7
              Re: struct "overloadi ng"

              Pantokrator wrote:
              [color=blue]
              > Thanks Michiel ,
              > In fact I did mention the structure of stA1 in my original post.[/color]

              Most people don't want to read the whole thread just to make sense out of a
              single posting. Therefore, please quote the context you're refering to.
              [color=blue]
              > Yes you are right, probably I did not clarify very well..
              > "NEVER APPEARS" refers to my IDE, which is not that important. What is
              > important is the sentence next to it "I get an error here " which
              > refers to the compiler and the error message I am getting. Sorry about
              > that.[/color]

              I don't see any reference to an error message. You just say "an error", but
              nothing about the actual message you're getting.
              [color=blue]
              > Could anybody please explain to me the question rarised by my previous
              > post?
              >
              > "..Additionally , I tried something with an anonymous struct and it
              > compiles fine:
              >
              > struct stA1
              > {
              > int i_ID;
              > int i_Type;
              >
              > struct
              > {
              > int i_ID;
              > int i_Person;
              > };
              > };[/color]

              It doesn't make much sense, and according to my compiler, the C++ standard
              doesn't even allow it. You can't ever refer to that nameless struct
              [color=blue]
              > and i am calling myStruct->i_Person = 1; without any problem
              > Theoritically speaking does anybody knows how does an anonymous struct
              > work within a named-struct from the compiler point of view? "..[/color]

              It shouldn't.

              Comment

              • Default User

                #8
                Re: struct "overloadi ng"

                Pantokrator wrote:

                [color=blue]
                > I apologise for reposting[/color]


                You may find the information below to be helpful in quoting properly


                Brian
                --
                Please quote enough of the previous message for context. To do so from
                Google, click "show options" and use the Reply shown in the expanded
                header.

                Comment

                • Default User

                  #9
                  Re: struct "overloadi ng"

                  Pantokrator wrote:
                  [color=blue]
                  > Hi,
                  > Is there any way to "overload" a struct?
                  >
                  > e.g.
                  > having already
                  > struct stA1
                  > {
                  > int i_ID;
                  > int i_Type;
                  > };
                  >
                  > and adding something like
                  > struct stA1
                  > {
                  > int i_ID;
                  > int i_Person;
                  > };[/color]


                  What are you trying to accomplish? What problem does this solve? You're
                  much better off telling us that than just showing your broken solution
                  to the problem.



                  Brian

                  --
                  Please quote enough of the previous message for context. To do so from
                  Google, click "show options" and use the Reply shown in the expanded
                  header.

                  Comment

                  • Jim Langston

                    #10
                    Re: struct "overloadi ng"


                    "Pantokrato r" <adosis@shotoku .co.uk> wrote in message
                    news:1148291021 .234720.290380@ y43g2000cwc.goo glegroups.com.. .[color=blue]
                    > Hi,
                    > Is there any way to "overload" a struct?
                    >
                    > e.g.
                    > having already
                    > struct stA1
                    > {
                    > int i_ID;
                    > int i_Type;
                    > };
                    >
                    > and adding something like
                    > struct stA1
                    > {
                    > int i_ID;
                    > int i_Person;
                    > };
                    >
                    > I tried to use a namespace on the second "overoloadi ng" struct like
                    >
                    > namespace KPT1_1
                    > {
                    > struct stA1
                    > {
                    > int i_ID;
                    > int i_Person;
                    > };
                    > }
                    >
                    > with not much luck... any clever ideas?
                    > Thanks a lot
                    > Telis[/color]

                    It depends on what you are trying to do. Are you trying to refer to the
                    i_Type as an i_Type or an i_Person? Then why not just use a union? And if
                    so, why does it really matter what you call it?

                    Perhaps you actually want an additional value i_Person? Maybe you just
                    want stA1 to contain i_ID and have two derived classes, one with i_Type and
                    one with i_Person? What is it you are trying to do?


                    Comment

                    • Luke Meyers

                      #11
                      Re: struct &quot;overloadi ng&quot;

                      Pantokrator wrote:[color=blue]
                      > Hi,[/color]

                      Hi.
                      [color=blue]
                      > Is there any way to "overload" a struct?[/color]

                      Nope.
                      [color=blue]
                      > struct stA1
                      > {
                      > int i_ID;
                      > int i_Type;
                      > };
                      >
                      > struct stA1
                      > {
                      > int i_ID;
                      > int i_Person;
                      > };[/color]

                      This violates what is known as the ODR, or "One-Definition Rule."
                      You're only allowed to provide one definition for a given declaration
                      (or set of equivalent declarations). Every definition is a declaration
                      as well, so above you've got two declarations of a struct called stA1,
                      but different definitions.

                      Why won't the compiler allow this? Well, when you go to instantiate
                      something called a stA1 (what the heck kind of name is that, anyway?
                      I'm gonna call it "Fred" from here on in -- was the name "stA1" really
                      so good that you needed to not just use it, but use it more than
                      once?), it needs to know which type you're talking about. The
                      fully-qualified name of the struct (that is, the name plus associated
                      namespace information) is the full extent of the information available
                      to the compiler to determine which type you want. If the names are the
                      same, it can't do that -- it's an unresolvable ambiguity. It can't
                      wait until later when you try and use one field or the other; there are
                      a lot of reasons why that can't work. One is that it needs to allocate
                      the right amount of memory to store the struct, and it can't do that if
                      it doesn't know which struct it is and therefore what the size is.
                      [color=blue]
                      > I tried to use a namespace on the second "overoloadi ng" struct like
                      >
                      > namespace KPT1_1
                      > {
                      > struct stA1
                      > {
                      > int i_ID;
                      > int i_Person;
                      > };
                      > }
                      >
                      > with not much luck... any clever ideas?[/color]

                      This should work (you don't show enough code to show why it doesn't
                      work for you), but it's inaccurate to call it "overloadin g." The
                      fully-qualified name of the struct above is (sigh) "::KPT1_1::stA1 ".
                      The other one is just "::stA1". The leading "::" specifies that you're
                      starting from the very top-level namespace, and is generally omitted.
                      Namespaces essentially establish a context for symbols -- when you're
                      within a namespace, you don't need to refer to it because it's
                      implicit. Qualification allows you to refer explicitly to non-local
                      symbols, or disambiguate as needed.

                      The reason the compiler can handle overloaded functions, by way of
                      contrast, is that they have different signatures, which provides enough
                      information for the compiler to disambiguate. Structs don't take
                      arguments (constructors don't count), so there's no analogous mechanism
                      available.

                      Luke

                      Comment

                      Working...