473,320 Members | 2,112 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 "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
10 2696
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
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
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
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
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
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
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
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

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Ann Huxtable | last post by:
I have the following code segment - which compiles fine. I'm just worried I may get run time probs - because it looks like the functions are being overloaded by the return types?. Is this Ok: ? ...
0
by: Leszek Taratuta | last post by:
Hello, I have the following code snippet that overloads the "-" operator: // unary public static Vector operator -( Vector v ) { return new Vector( -v.X, -v.Y ); }
20
by: Andreas Griesmayer | last post by:
Hi, following piece of code causes a strange behavior when compiled with GCC (Linux, Gentoo 3.3.5.20050130-r1). read() is called twice although only called once, the first time before the first...
2
by: Colonel | last post by:
It seems that the problems have something to do with the overloading of istream operator ">>", but I just can't find the exact problem. // the declaration friend std::istream &...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.