Pantokrator wrote:
Hi,
Hi.
Is there any way to "overload" a struct?
Nope.
struct stA1
{
int i_ID;
int i_Type;
};
struct stA1
{
int i_ID;
int i_Person;
};
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.
I tried to use a namespace on the second "overoloading" struct like
namespace KPT1_1
{
struct stA1
{
int i_ID;
int i_Person;
};
}
with not much luck... any clever ideas?
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 "overloading." 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