包含一个人自己的类型 [英] containing one's own type

查看:62
本文介绍了包含一个人自己的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与我上一篇文章相关,但是单独的问题


一个班级可以自制成容器吗?

我可以

创建一个表示名称,值对的属性类

创建一个包含属性映射的attributeGroup类

然后添加到attributeGroup类以包装一个地图

属性组?


这样一个attributeGroup可以包含n级属性吗?

解决方案

Christopher写道:


与我上一篇文章相关,但是单独的问题


一个班级可以制成自己的容器?



你问,任何一个盒子都可以包含几个相同大小的盒子和

容量,对吧?未在这个宇宙中。

可以我

创建一个属性类表示一个名称,值对

创建一个attributeGroup类包装地图属性

,然后添加到attributeGroup类也包裹地图

attributeGroups的?


那样一个attributeGroup可以包含n级属性?


我译文:不能让我记住了这些英文句子身边,也许你可以

把它在C ++(不要担心关于你的代码compilability现在)


)(V

- ?

请解除资本 '' A'是当通过e-mail

回答我不给顶贴的回信,就别问

在7月24日,1:下午41点,Victor Bazarov< v.Abaza ... @ comAcast.netwrote:


Christopher写道:

我不能让我的思绪围绕那些英语句子,也许你可以用b ++把它放在C ++中(现在不用担心代码的可编程性)?


V



无错误处理的概念:


//基本名称价值对

类属性

{

public:

Attribute();

~Attribute();


const std :: stri ng& GetName()const;

void SetName(const std :: string& name);


template< typename T>

void SetValue(const T& value)

{

//使用boost'的词汇演员

}
<登记/>
模板<类型名T>

空隙的GetValue(常量的std :: string&安培;名,T&安培; value_out)

{

//使用升压的词汇投

}


私人:


标准: :字符串名称;

std ::字符串值;

}


//名称值对组

class AttributeGroup

{

public:

AttributeGroup();

~AttributeGroup();


const std :: string& GetName()const;

void SetName(const std :: string& name);


void InsertAttribute(const Attribute& attribute);

void RemoveAttribute(const std :: string& name);

const Attribute&的getAttribute(常量的std :: string&安培;名称)常量;


//可疑代码

空隙InsertAttributeGroup(常量AttributeGroup&安培;基);

void RemoveAttributeGroup(const std :: string& name);

const AttributeGroup& GetAttributeGroup(常量的std :: string&安培;名)

常量;


私人:


的std ::字符串名称;


typedef std :: map< std :: string,AttributeAttributeMap;


//可疑代码

typedef std :: map< std :: string,AttributeGroupAttributeGroupMap;

}


Victor Bazarov写道:


克里斯托弗写道:

>与我的最后一个职位,但单独的问题

可以在类被制成自己的容器?


你在回绝,可以在任何盒包含相同的尺寸和

容量的几箱,对不对?不在这个宇宙中。



这取决于你对包含的定义。这是完全可能的:


A级

{

A * array_of_A [10];

public:

void init()

{

for(int i = 0; i< 10; ++ i)

array_of_A [i] = new A;

}

};


Related to my last post, but seperate question

Can a class be made into a container of its self?
Could I
Create an attribute class representing a name, value pair
Create an attributeGroup class wrapping a map of attributes
and then add to the attributeGroup class to also wrap a map of
attributeGroups?

That way an attributeGroup could contain n levels of attributes?

解决方案

Christopher wrote:

Related to my last post, but seperate question

Can a class be made into a container of its self?

You''re asking, can any box contain several boxes of the same size and
capacity, right? Not in this universe.

Could I
Create an attribute class representing a name, value pair
Create an attributeGroup class wrapping a map of attributes
and then add to the attributeGroup class to also wrap a map of
attributeGroups?

That way an attributeGroup could contain n levels of attributes?

I couldn''t get my mind around those English sentences, perhaps you could
put it in C++ (don''t worry about compilability of your code for now)?

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


On Jul 24, 1:41 pm, Victor Bazarov <v.Abaza...@comAcast.netwrote:

Christopher wrote:
I couldn''t get my mind around those English sentences, perhaps you could
put it in C++ (don''t worry about compilability of your code for now)?

V


Conceptual without error handling considered:

// Basic name value pair
class Attribute
{
public:
Attribute();
~Attribute();

const std::string & GetName() const;
void SetName(const std::string & name);

template<typename T>
void SetValue(const T & value)
{
// use boost''s lexical cast
}

template<typename T>
void GetValue(const std::string & name, T & value_out)
{
// use boost''s lexical cast
}

private:

std::string name;
std::string value;
}

// Group of name value pairs
class AttributeGroup
{
public:
AttributeGroup();
~AttributeGroup();

const std::string & GetName() const;
void SetName(const std::string & name);

void InsertAttribute(const Attribute & attribute);
void RemoveAttribute(const std::string & name);
const Attribute & GetAttribute(const std::string & name) const;

// Questionable Code
void InsertAttributeGroup(const AttributeGroup & group);
void RemoveAttributeGroup(const std::string & name);
const AttributeGroup & GetAttributeGroup(const std::string & name)
const;

private:

std::string name;

typedef std::map<std::string, AttributeAttributeMap;

// Questionable Code
typedef std::map<std::string, AttributeGroupAttributeGroupMap;
}


Victor Bazarov wrote:

Christopher wrote:

>Related to my last post, but seperate question

Can a class be made into a container of its self?


You''re asking, can any box contain several boxes of the same size and
capacity, right? Not in this universe.

It depends on your definition of "contain". This is perfectly possible:

class A
{
A* array_of_A[10];

public:
void init()
{
for(int i = 0; i < 10; ++i)
array_of_A[i] = new A;
}
};


这篇关于包含一个人自己的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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