结构“重载” [英] struct "overloading"

查看:89
本文介绍了结构“重载”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



有没有办法超载一个结构?


例如

已经

struct stA1

{

int i_ID;

int i_Type;

};


并添加类似
$ b $的东西b struct stA1

{

int i_ID;

int i_Person;

};


我试图在第二个overoloading上使用命名空间。结构如


名称空间KPT1_1

{

struct stA1

{

int i_ID;

int i_Person;

};

}


并不多运气......任何聪明的想法?

非常感谢

Telis

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

推荐答案

你的意思是什么?重载?


你的编译器报告你上面的代码时没有错误吗?


如果第一个stA1结构,第二个是在不同的.cpp

文件中,有些编译器不会报告错误,但是如果你实例是一个

对象,如:


stA1 a;


实例a可能不是你想要的那个,也许是a。是第一个stA1的

实例,也许是第二个,取决于什么?我是

也搞砸了......

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...


感谢您的回复。

我的意思是我希望在同一个头文件中有两个与stA1

同名的结构。编译很好,但每当我尝试在变量i_Person中填写




....

KPT1_1 :: stA1 * myStruct = 0;

myStruct-> i_Person = 1; //永远不会出现我在这里收到错误


而不是编译器只识别myStruct-> i_Type,这不是我想要的特定代码片段。


另外,我尝试了一个带有匿名结构的东西,并且

编译好了:


struct stA1

{

int i_ID;

int i_Type;


struct

{

int i_ID;

int i_Person;

};

};


我正在调用myStruct-> i_Person = 1;没有任何问题

理论上说,从编译器的角度来看,有人知道匿名结构如何在命名结构中工作吗?


谢谢

Telis

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


Pantokrator写道:
Pantokrator wrote:
感谢您的回复。 />我的意思是我希望在同一个头文件中有两个与stA1
同名的结构。它编译得很好,但每当我尝试填充变量i_Person

...
KPT1_1 :: stA1 * myStruct = 0;
myStruct-> i_Person = 1; //永远不会出现我在这里得到一个错误
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




难怪它没有出现,它在上一篇文章中;)


说真的,请在每个帖子中加入足够的上下文,引用所需的

部分

。例如。 stA1的定义。


那就是说,我想永远不会出现指的是你的IDE。这是

相当

普通;许多IDE在两个具有相似名称的结构中存在问题


不同的命名空间中。 Jsut类型应该在那里,忽略IDE

建议。真正的编译器确实知道myStruct有哪些成员。


HTH,

Michiel Salters



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


这篇关于结构“重载”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆