继承思想 [英] inheritance ideas

查看:90
本文介绍了继承思想的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好。我想知道如何使用虚函数和类

继承实现特定的

功能......我不知道它是否可能,但我想

我会试一试。


我想要的是拥有一个特定成员的基类

数据和一个将由

继承类重载的虚函数。


类CBaseClass

{

public:

virtual void OverLoadMe(void){}

int DataFieldA;

};


在继承的类中,我将重载函数,但是我想要引入新的成员数据:


class CInheritedClassA:public CBaseClass

{

void OverLoadMe(void){//现在开心吗? } $ / $
浮动DataFieldB;

};


现在如果我定义了这样的变量:


CBaseClass * MyClass = new CInheritedClassA;


我希望能够访问DataFieldB。当然,我

不能,但是我正在寻找这样的功能的原因

是这样我可以容纳一组继承的类A-Z

实例在一个列表中而不必有单独的列表

用于单独的类型。


所以,虽然我确实知道这不是'工作,而且

此外我也意识到为什么这不是也不应该b
工作,我想知道我是否可以绕不去

完全改变我到目前为止的设计。


非常感谢,

Mike

解决方案

" Mike Frayn" <再********** @ hotmail.com>写道......

你好。我想知道如何使用虚函数和类继承来实现特定的功能......我不知道它是否可能,但我想我会给它一个镜头。

我想要的是拥有一个具有特定成员数据的基类和一个将由
继承类重载的虚函数。

class CBaseClass
{
公开:
虚拟空间OverLoadMe(无效){}
int DataFieldA;
};

继承的类,我会重载函数但是我想引入新的成员数据:

类CInheritedClassA:public CBaseClass
{
void OverLoadMe(void) { //现在开心? }
float DataFieldB;
};

现在如果我定义了一个这样的变量:

CBaseClass * MyClass = new CInheritedClassA;

我希望能够访问DataFieldB。


那么,为什么不在CBaseClass中使用纯粹的虚函数来支持
" access"你的派生类数据成员?为什么你需要访问

呢?

当然,我不能,但我之所以在寻找这样的功能
是这样我可以在一个列表中包含一组继承的类A-Z
实例,而不必为单独的类型提供单独的列表。


因此,访问特定于派生类的数据成员的能力

与您创建
$的原因无关b $ b层次结构,对吧?那么你为什么要尝试使用基类来获得它不被创造的东西?

所以,虽然我确实知道这不起作用而且
此外我也意识到为什么这不是也不应该工作,我想知道我是否可以绕过它而不是完全改变我的设计到目前为止。




没有理由彻底改变设计。但是,

强烈需要你意识到WRT访问数据成员

你有_no_design_无论如何,所以没有什么可以改变的。如果你发现你的秘密为什么需要这些数据(以及公众也是如此),我们

可能会提出几个解决方案。


V


Mike Frayn写道:

你好。我想知道如何使用虚函数和类继承来实现特定的功能......我不知道它是否可能,但我想我会给它一枪。
[snip] class CBaseClass
{
public void OverLoadMe(void){}
int DataFieldA;
};
[snip]

class CInheritedClassA:public CBaseClass
{
void OverLoadMe(void){//现在开心吗? }
float DataFieldB;
};
[snip]


非常感谢,
Mike




顺便说一句,有没有理由在你的课程中加上字母''C''的所有字母前缀。这是一个微软的命名约定。


参见:
http://www.jelovic.com/articles/stupid_naming.htm

-

Thomas Matthews


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

C ++常见问题: 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

其他网站:
http://www.josuttis.com - C ++ STL图书馆书籍
http://www.sgi.com/tech/stl - 标准模板库


Mike Frayn <再********** @ hotmail.com>在消息中写道

新闻:40 ********************* @ lovejoy.zen.co.uk ...

[snip]


我希望能够访问DataFieldB。当然,我不能,但是我正在寻找这样的功能的原因是,我可以将一组继承的类A-Z
实例放在一个列表中,而不必对于不同类型的单独列表




你可能会看看

www.boost.org


-

Cy
http://home.rochester.rr。 com / cyhome /


Hi there. I''m wondering how to achieve a particular
functionality using virtual functions and class
inheritance... I don''t know if its possible, but I figured
that I would give it a shot.

What I want is to have a base class with particular member
data and a virtual function that will be overloaded by the
inheriting class.

class CBaseClass
{
public:
virtual void OverLoadMe(void) {}
int DataFieldA;
};

In the inherited class, I will overload the function but I
would like to introduce new member data:

class CInheritedClassA : public CBaseClass
{
void OverLoadMe(void) { //happy now? }
float DataFieldB;
};

Now if I defined a variable like so:

CBaseClass* MyClass = new CInheritedClassA;

I would like to be able to access DataFieldB. Of course, I
can''t, however the reason I''m looking for such functionality
is so that I can house a group of inheritedclassA-Z
instances in one list without having to have separate lists
for separate types.

So, while I do realize that this doesn''t work, and
furthermore I also realize WHY this doesn''t and shouldn''t
work, I''m wondering if I can get around it without
COMPLETELY changing the design of what I have so far.

Thanks so much,
Mike

解决方案

"Mike Frayn" <re**********@hotmail.com> wrote...

Hi there. I''m wondering how to achieve a particular
functionality using virtual functions and class
inheritance... I don''t know if its possible, but I figured
that I would give it a shot.

What I want is to have a base class with particular member
data and a virtual function that will be overloaded by the
inheriting class.

class CBaseClass
{
public:
virtual void OverLoadMe(void) {}
int DataFieldA;
};

In the inherited class, I will overload the function but I
would like to introduce new member data:

class CInheritedClassA : public CBaseClass
{
void OverLoadMe(void) { //happy now? }
float DataFieldB;
};

Now if I defined a variable like so:

CBaseClass* MyClass = new CInheritedClassA;

I would like to be able to access DataFieldB.
So, why not have a pure virtual function in CBaseClass that would
"access" your derived class data member? Why do you need to access
it, anyway?
Of course, I
can''t, however the reason I''m looking for such functionality
is so that I can house a group of inheritedclassA-Z
instances in one list without having to have separate lists
for separate types.
So, the ability to access data members specific to derived classes
has really nothing to do with the reasons for which you created your
hierarchy, right? Then why are you trying to use the base class for
something it wasn''t created to do?
So, while I do realize that this doesn''t work, and
furthermore I also realize WHY this doesn''t and shouldn''t
work, I''m wondering if I can get around it without
COMPLETELY changing the design of what I have so far.



There is no reason to completely change the design. However, there
is a strong need for you to realise that WRT accessing data members
you have _no_design_ whatsoever, so there is nothing to change. If
you uncover your secret why you need that data (and public too), we
could probably suggest a couple of solutions.

V


Mike Frayn wrote:

Hi there. I''m wondering how to achieve a particular
functionality using virtual functions and class
inheritance... I don''t know if its possible, but I figured
that I would give it a shot. [snip] class CBaseClass
{
public:
virtual void OverLoadMe(void) {}
int DataFieldA;
}; [snip]
class CInheritedClassA : public CBaseClass
{
void OverLoadMe(void) { //happy now? }
float DataFieldB;
}; [snip]

Thanks so much,
Mike



By the way, there is no reason to prefix all of
your classes with the letter ''C''. This is a
Microsoft naming convention.

See:
http://www.jelovic.com/articles/stupid_naming.htm
--
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
http://www.sgi.com/tech/stl -- Standard Template Library


"Mike Frayn" <re**********@hotmail.com> wrote in message
news:40*********************@lovejoy.zen.co.uk...
[snip]


I would like to be able to access DataFieldB. Of course, I
can''t, however the reason I''m looking for such functionality
is so that I can house a group of inheritedclassA-Z
instances in one list without having to have separate lists
for separate types.



You might have a look at boost::any at

www.boost.org

--
Cy
http://home.rochester.rr.com/cyhome/


这篇关于继承思想的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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