一般C ++问题 [英] General C++ Questions

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

问题描述

大家好,


我偶然在一个网站上遇到过这两个问题,而且我无法想象或得到一个回答这两个问题。


1)多重继承 - 对象包含多少继承的

祖先?


2)你怎么能找到一个班级的实例?


任何帮助都将不胜感激。


谢谢你/>
Muralidhar
mu **** ****************@yahoo.com

Hi all,

I happened to come across these two questions in a site, and I couldn''t
think or get hold of an answer to these two questions.

1) Multiple inheritance - objects contain how many multiply inherited
ancestor?

2) How would you find out the no of instance of a class?

Any help would be greatly appreciated.

Thanks
Muralidhar
mu********************@yahoo.com

推荐答案



你好


我不明白你的第一个问题。

Hello

I don''t understand your first question.
2)你怎么会发现没有实例的一个班级?
2) How would you find out the no of instance of a class?




例如:


A级

{

public:

A();

~A();


unsigned int getInstances();


私人:

static unsigned int instances;

}


A :: A()

{

++实例;

}


A :: ~A()

{

- 实例;

}


A :: getInstances()

{

返回实例;

}


unsigned int A :: instances = 0;

最好的问候

Tobias



Example:

class A
{
public:
A();
~A();

unsigned int getInstances();

private:
static unsigned int instances;
}

A::A()
{
++instances;
}

A::~A()
{
--instances;
}

A::getInstances()
{
return instances;
}

unsigned int A::instances = 0;
best regards
Tobias


Murali发布:
Murali posted:
2)你怎么能找到一个班级的实例?
2) How would you find out the no of instance of a class?



你不能。

你必须如此即兴发挥:

class Kata

{

受保护:

static unsigned short int amount_objects;


public:


Kata(无效)

{

++ amount_objects;

}


~Kata(无效)

{

--amount_objects;

}


};


unsigned short int Kata :: amount_obje cts = 0;

或者也许那里有某种模板可以让你在任何课程上做到这一点



-JKop


You can''t.
You''d have to improvise as so:
class Kata
{
protected:
static unsigned short int amount_objects;

public:

Kata(void)
{
++amount_objects;
}

~Kata(void)
{
--amount_objects;
}

};

unsigned short int Kata::amount_objects = 0;
Or perhaps there''s some sort of template out there that''ll allow you to do
this with any class.
-JKop




" Tobias Erbsland" <是ne *********** @ tracker.drzoom.ch>在消息中写道

新闻:ca ********** @ atlas.ip-plus.net ...

"Tobias Erbsland" <ne***********@tracker.drzoom.ch> wrote in message
news:ca**********@atlas.ip-plus.net...

你好

我不明白你的第一个问题。

Hello

I don''t understand your first question.
2)你怎么能找到一个类的实例?
2) How would you find out the no of instance of a class?


例如:

A级
{
公开:
A();
~A();

unsigned int getInstances();

private:
static unsigned int instances;


A :: A()
{
++实例;
}
A :: ~A()
{
- 实例;
}

A :: getInstances()
{
返回实例;
}

unsigned int A :: instances = 0;



Example:

class A
{
public:
A();
~A();

unsigned int getInstances();

private:
static unsigned int instances;
}

A::A()
{
++instances;
}

A::~A()
{
--instances;
}

A::getInstances()
{
return instances;
}

unsigned int A::instances = 0;




这个解决方案的一个明显错误是它没有考虑

正在制作的物品副本。

Scott Meyers已经给出了一个很好的解决方案 -
http:// www 。 cuj.com/documents/s=8066/cuj9804meyers/


这篇关于一般C ++问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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