静态成员和继承 [英] Static Members and Inheritance

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

问题描述




假设我有一个抽象基类,我知道每个派生类

都有一个静态数据成员(称之为SharedInformation)

表示要在该

派生类的所有实例之间共享的信息。但是,对于每个

派生类,此信息将是不同的。


现在,如果我将SharedInformation定义为基础的静态成员

类,然后SharedInformation不能在不同的派生

类之间变化,因为所有派生类都将指向SharedInformation的相同静态
实例。但是,如果我没有在

基类中定义它,那么看基类的人就不会知道

每个派生类必须定义一个静态数据成员

SharedInformation。


如何解决这个问题?也就是说,让我的基础说,所有派生的

类将有一个名为SharedInformation的静态数据成员。但是

仍然可以为每个基类提供不同的SharedInformation静态实例




我知道我可能正在寻找在这个问题上错了。如果是这样的话,

我会对如何重新概念化它有任何建议。


谢谢,

cpp

Hi,

Say I have a abstract base class and I know that every derived class
will have a static data member (call it SharedInformation)
representing information to be shared across all instances of that
derived class. However, this information will be differenet for each
derived class.

Now, if I define SharedInformation as a static member of the base
class, then SharedInformation cannot vary across the different derived
classes, because all derived classes will pointing to the same static
instance of SharedInformation. However, if I don''t define it in the
base class, then someone looking at the base class won''t know that
every derived class must define a static data member
SharedInformation.

How can I solve this problem? That is, have my base say, "All derived
classes will have a static data member called SharedInformation" but
still be able to have different static instances of SharedInformation
for each base class.

I know I may be looking at this problem in the wrong light. If so,
I''d appreciate any suggestions on how to re-conceptualize it.

thanks,
cpp

推荐答案



CPP,


现在,我想你的问题陈述有点太模糊

提供解决方案。我可以想到几种方法基于

MS Windows编程中使用的一些编程模式:


1.使用类工厂创建对象,工厂有效地

提供

锚指向连接制造类的所有实例


2.提供生成成员函数/数据定义的宏

插入标题/实现中类,类似于

用于支持MSVC ++ / MFC中对象序列化的宏。


3.使用policy实现类类,这是一个模板化的

多个

继承方法,编织一个特殊的类来处理派生类的静态

注册。这将需要推导者。

你的

等级以同样的方式这样做,这可能是也可能是不可接受的。


我可以去如果我不理解

问题的背景,那可能会毫无结果。


dave


" cppaddict" <他*** @ hello.com>在消息中写道

news:qu ******************************** @ 4ax.com ...

CPP,

Right now, I think your problem statement is a bit too vague
to offer a solution. I can think of several approaches based upon
some programming patterns used in MS Windows programming:

1. Use a class factory to create your objects, the factory effectively
provides
the "anchor" point to connect all instances of a manufactured class

2. Provide macros which generates a member function/data definition
to insert in the header/implementation of the class, something akin to
the macros used to support serialization of objects in MSVC++/MFC.

3. Implement the class using "policy" classes, that is a templatized
multiple
inheritance approach, weave in a special class which handles the static
registration of the derived class. This will require the "deriver" of
your
class to do so in the same way, which might or might not be acceptable.

I could go on, but it might be fruitless if I don''t understand the context
of the
problem.

dave

"cppaddict" <he***@hello.com> wrote in message
news:qu********************************@4ax.com...


说我有一个抽象基类,我知道每个派生类
都有一个静态数据成员(称之为SharedInformation)
表示要在该派生类的所有实例之间共享的信息。但是,这些信息对于每个派生类都是不同的。

现在,如果我将SharedInformation定义为基类的静态成员,那么SharedInformation不能在不同的类中变化派生类,因为所有派生类都将指向同一个静态的SharedInformation实例。但是,如果我没有在基类中定义它,那么看基类的人就不会知道每个派生类都必须定义一个静态数据成员
SharedInformation 。

我该如何解决这个问题?也就是说,让我的基础说,所有派生的类都将有一个名为SharedInformation的静态数据成员。但是
仍然可以为每个基类提供不同的SharedInformation静态实例。

我知道我可能会以错误的方式看待这个问题。如果是的话,
我会很感激如何重新构思它。

谢谢,
cpp
Hi,

Say I have a abstract base class and I know that every derived class
will have a static data member (call it SharedInformation)
representing information to be shared across all instances of that
derived class. However, this information will be differenet for each
derived class.

Now, if I define SharedInformation as a static member of the base
class, then SharedInformation cannot vary across the different derived
classes, because all derived classes will pointing to the same static
instance of SharedInformation. However, if I don''t define it in the
base class, then someone looking at the base class won''t know that
every derived class must define a static data member
SharedInformation.

How can I solve this problem? That is, have my base say, "All derived
classes will have a static data member called SharedInformation" but
still be able to have different static instances of SharedInformation
for each base class.

I know I may be looking at this problem in the wrong light. If so,
I''d appreciate any suggestions on how to re-conceptualize it.

thanks,
cpp



我可以继续,但如果我不理解
问题的背景,那可能会毫无结果。

dave
I could go on, but it might be fruitless if I don''t understand the context
of the
problem.

dave




戴夫,


我会试着给你一个简单的例子。说我的基类叫做

Document。它有像getLineNumber(int),appendLine(string),

等方法。也许我会派出类似Letter,ResearchPaper,

文章等等。

现在让我们说每个文件必须使用某个

字符集(一组字符)来编写。让我们进一步假设Charset

对于特定派生类的不同实例总是相同的

(例如,所有Letter实例使用相同的Charset),但是Charset对于不同的派生类不一定是相同的(例如,一篇文章

可以使用不同的Charset来自一封信)。


这向我建议:


1. Charset应该是每个派生类的静态成员


2.文件class应该以某种方式声明所有派生类

必须有一个Charset。


很遗憾你不能让Charset成为Document的静态成员,
因为这会强制所有派生类的Document使用

相同的Charset。


这有意义吗?


感谢您提出任何其他建议,

cpp



Dave,

I''ll try to give you a simple example. Say my base class is called
Document. It has methods like getLineNumber(int), appendLine(string),
etc. Perhaps I will have derived classes like Letter, ResearchPaper,
Article, etc.

Now let''s say that every document must be written using a certain
Charset (a set of characters). Let''s further assume that the Charset
is always the same for different instances of a specific derived class
(eg, all Letter instances use the same Charset), but that Charset is
not necessarily the same for different derived classes (eg, an Article
could use a different Charset from a Letter).

This suggests to me that:

1. Charset should be a static member of each derived class

2. The Document class should somehow state that all derived classes
must have a Charset.

Unfortunately you can''t make Charset a static member of Document,
because that would force all derived classes of Document to use the
same Charset.

Does that make sense?

Thanks for any additional suggestions,
cpp




使用您的方法:

类VehicleClass

{


};

类CarClass:public VehicleClass

{


};


类MotorcycleClass:public VehicleClass

{

};


让's'说CarClass的SharedInformation是亨利福特出生的那一年,让我们说它是1902年。

让我们说摩托车级别的SharedInformation是摩托车上叉子的平均长度,让我们说它是120.我们走了:

class VehicleClass

{

public:

unsigned long int * pSharedInformation;


}


类CarClass:public VehicleClass

{

private:

static unsigned long int SharedInformation;


public:

CarClass(无效)

{

pSharedInformation =& SharedInformation;

}

};


类MotorcycleClass: public VehicleClass

{

private:

static unsigned long int SharedInformation;


public:< br $>
MotorcycleClass(无效)

{

pSharedInformation =& SharedInformation;

}

};


unsigned long int CarClass :: SharedInformation = 1902;


unsigned long int MotorcycleClass :: SharedInformation = 120;

void ServiceVehicle(VehicleClass *);


int main(无效)

{

CarClass Beamer;


MotorcycleClass Harley;

ServiceVehicle(& Beamer);

ServiceVehicle(& Harley);

}

void ServiceVehicle(VehicleClass * pVehicle)

{

cout<< pVehicle-> * pSharedInformation;


//是的,我知道我没有包含

//适当的头文件!


//以上将为一辆汽车提供1902,为一个

//摩托车提供120美元

}


这是做到这一点的一种方式。显然,带有返回

类型的unsigned long int的多态函数也可以工作,它们也会更容易。但是

然后我有我的座右铭,更容易并不总是更好,采取手动变速器

汽车!!

希望有所帮助。 br />
-JKop

Using your method:
class VehicleClass
{

};
class CarClass : public VehicleClass
{

};

class MotorcycleClass : public VehicleClass
{
};

Let''s say that the SharedInformation for CarClass is the year in which
Henry Ford was born, let''s say it''s 1902.
And let''s say that the SharedInformation for MotorcycleClass is the
average length of the forks on a motorcycle, let''s say it''s 120. Here we go:
class VehicleClass
{
public:
unsigned long int* pSharedInformation;

}

class CarClass : public VehicleClass
{
private:
static unsigned long int SharedInformation;

public:
CarClass(void)
{
pSharedInformation = &SharedInformation;
}
};

class MotorcycleClass : public VehicleClass
{
private:
static unsigned long int SharedInformation;

public:
MotorcycleClass(void)
{
pSharedInformation = &SharedInformation;
}
};

unsigned long int CarClass::SharedInformation = 1902;

unsigned long int MotorcycleClass::SharedInformation = 120;
void ServiceVehicle(VehicleClass*);

int main(void)
{
CarClass Beamer;

MotorcycleClass Harley;
ServiceVehicle(&Beamer);
ServiceVehicle(&Harley);
}
void ServiceVehicle(VehicleClass* pVehicle)
{
cout << pVehicle->*pSharedInformation;

//Yes, I''m aware that I haven''t include the
//appropriate header files!

//The above will yield 1902 for a car, and 120 for a
//motorcycle
}

That''s one way of doing it. Obviously polymorphic functions with a return
type of unsigned long int would also work, they would also be easier. But
then I have my motto, easier isn''t always better, take manual transmission
cars!!
Hope that helps.
-JKop


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

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