一个班可以有两个名字吗? [英] Can one class have two names?

查看:90
本文介绍了一个班可以有两个名字吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

班级x,y

{

公开:

.....

私人:

};

解决方案

JustSomeGuy写道:

class x,y
{
公开:
....
私人:
};



不可以。你可以拥有一个基类包含常见的

的东西,并从中继承两个:

类基础

{

// .. 。

};


class x

:公共基地

{

// ...

};


class y

:公共基地

{

// ...

};


或者你可以有一个类的两个实例:

类普通

{

} x,y; //两个常见类变量。


或者你可以把常见的东西扔进

a模板。


-

Thomas Matthews


C ++新闻组欢迎辞:
http://www.slack.net/~shiva/welcome.txt

C ++常见问题: http://www.parashift.com/c++-faq-lite

C常见问题: http:// www.eskimo.com/~scs/c-faq/top.html

alt.comp.lang.learn.c-c ++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html

其他网站:
http:// www .josuttis.com - C ++ STL图书馆书


JustSomeGuy写道:

class x,y
公开:
....
私人:
};




不,你不能这样做,但你可以这样做:


class x {/ * ... * /};


typedef xy;


- Adam


-

反向回复域名。


JustSomeGuy写道:

class x,y
{
公开:
....
私人:
};



托马斯的帖子涵盖了大部分内容。 />

他遗漏的一件事是typedef。


class x

{

public :

.....

私人:

};


typedef xy;


现在,x和y完全相同。但是,y真的是x级。





void foo(y&);





void foo(x&);


是完全相同的声明。


class x,y
{
public:
.....
private:
};

解决方案

JustSomeGuy wrote:

class x,y
{
public:
....
private:
};


No. You can have one base class containing common
stuff and have the two inherit from it:
class base
{
// ...
};

class x
: public base
{
// ...
};

class y
: public base
{
// ...
};

Or you can have two instances of one class:
class common
{
} x, y; // two variable of class common.

Or you can throw the common stuff into
a template.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book


JustSomeGuy wrote:

class x,y
{
public:
....
private:
};



No, you can''t do that, but you could do this:

class x { /* ... */ };

typedef x y;

- Adam

--
Reverse domain name to reply.


JustSomeGuy wrote:

class x,y
{
public:
....
private:
};


Thomas''s post covers most of it.

One thing he left out was typedefs.

class x
{
public:
.....
private:
};

typedef x y;

Now, x and y are exactly the same. However, y is really class x.

i.e.

void foo( y & );

and

void foo( x & );

are exactly the same declaration.


这篇关于一个班可以有两个名字吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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