By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,758 Members | 1,225 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,758 IT Pros & Developers. It's quick & easy.

struct "overloading"

P: n/a
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 "overoloading" struct like

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

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

May 22 '06 #1
Share this Question
Share on Google+
10 Replies


P: n/a
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...

May 22 '06 #2

P: n/a
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

May 22 '06 #3

P: n/a
Pantokrator wrote:
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


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

May 22 '06 #4

P: n/a
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

May 22 '06 #5

P: n/a
Pantokrator posted:
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;
};
You haven't added anything -- you've changed the name of a member object.
I tried to use a namespace on the second "overoloading" struct like

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

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
May 22 '06 #6

P: n/a
Pantokrator wrote:
Thanks Michiel ,
In fact I did mention the structure of stA1 in my original post.
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.
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.
I don't see any reference to an error message. You just say "an error", but
nothing about the actual message you're getting.
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;
};
};
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
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? "..


It shouldn't.

May 22 '06 #7

P: n/a
Pantokrator wrote:

I apologise for reposting

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.
May 22 '06 #8

P: n/a
Pantokrator wrote:
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;
};

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.
May 22 '06 #9

P: n/a

"Pantokrator" <ad****@shotoku.co.uk> wrote in message
news:11**********************@y43g2000cwc.googlegr oups.com...
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 "overoloading" struct like

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

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


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?
May 22 '06 #10

P: n/a
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

May 22 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.