一个简单的c ++问题...... [英] A simple c++ question...

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

问题描述

我有几节课...


a类:std :: list< baseclass>

{

int keya;

};


class b:std :: list< a>

{

int keyb;

};


class c:std :: list< b>

{

int keyc;

};

我想在每个级别弄清楚列表的大小......


如果我有:


int main(int argc,char * argv [])

{

cx;


cout<< c类列表的大小是 << x.size()<< endl;

cout<< b类列表的大小是 << ???? << endl;

cout<< 班级列表的大小是 << ???? <<< endl;

}

------------------------------- ------------------------------------------


想一想......

使用std :: list的继承可能不是最好的想法..

或许:


类a:std :: list< baseclass>

{

int keya;

std :: list< sometype> ; baseclasslist;

};


class b

{

int keyb;

std :: list< a> alist;

};


class c

{

int keyc;

std :: list< b> blist;

};


可能是一个更好的实现,但问题仍然存在。

I have a few classes...

class a : std::list<baseclass>
{
int keya;
};

class b : std::list<a>
{
int keyb;
};

class c : std::list<b>
{
int keyc;
};
I''m trying to figure out at each level the size of the lists...

so if I have:

int main(int argc, char *argv[])
{
c x;

cout << "The size of the c class list is " << x.size() << endl;
cout << "The size of the b class list is " << ???? << endl;
cout << "The size of the a class list is " << ???? <<< endl;
}
-------------------------------------------------------------------------

Thinking about it...
Using inheritance of std::list may not have been the best idea..
perhaps:

class a : std::list<baseclass>
{
int keya;
std::list<sometype> baseclasslist;
};

class b
{
int keyb;
std::list<a> alist;
};

class c
{
int keyc;
std::list<b> blist;
};

May have been a better implementation, but the question remains.

推荐答案

" JustSomeGuy" <无** @ nottelling.com>写了...
"JustSomeGuy" <no**@nottelling.com> wrote...
我有几节课......
[...]
I have a few classes...
[..]




耐心是一种美德/>



Patience is a virtue


你的问题是你私下里,所以你不能
访问a,b,c的超类中的size()函数等等。这是'b $ ba工作的一段代码''splain it。


davet。


#include< iostream>

#include< list>

using namespace std;

class baseclass

{

public:


};

class A:public std :: list< int>

{


int keya;

};


B类:public std :: list< A> ;

{


int keyb;

};


class C: public std :: list< B>

{


int keyc;

};


int main()

{

A a;

B b;

C c;


for(int k = 0; K< 10; k ++)

{

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

{

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

{


a.push_back(i);


}


b.push_back(a);

}


c.push_back(b);

}


cout<< 班级列表的大小是 << a.size()<< endl;


cout<< b类列表的大小是 << b.size()<< endl;

cout<< c类列表的大小是 << c.size()<<结束;


返回0;

}


" JustSomeGuy" <无** @ nottelling.com>在消息中写道

news:pxFKc.54262
Your problem was you were inherting privately, so you couldn''t
access the size() function in the superclass of a, b,c etc. Here''s
a working piece of code to ''splain it.

davet.

#include <iostream>
#include <list>
using namespace std;
class baseclass
{
public:

};
class A : public std::list<int>
{

int keya;
};

class B : public std::list<A>
{

int keyb;
};

class C : public std::list<B>
{

int keyc;
};

int main()
{
A a;
B b;
C c;

for (int k=0; k<10; k++)
{
for (int j=0; j<10 ; j++)
{
for (int i=0; i< 10; i++)
{

a.push_back(i);

}

b.push_back(a);
}

c.push_back(b);
}

cout << "The size of the a class list is " << a.size() << endl;

cout << "The size of the b class list is " << b.size() << endl;
cout << "The size of the c class list is " << c.size() << endl;

return 0;
}

"JustSomeGuy" <no**@nottelling.com> wrote in message
news:pxFKc.54262


Mr4.2978@pd7tw1no ...
Mr4.2978@pd7tw1no...
我有几节课.. 。

类a:std :: list< baseclass>
{keya;
};

b类:std: :list< a>
{key int;
};

类c:std :: list< b>
{
int keyc;
};

我想在每个级别弄清楚列表的大小......

所以如果我有:

int main(int argc,char * argv [])
{
cx;

cout<< c类列表的大小是 << x.size()<< endl;
cout<< b类列表的大小是 << ???? << endl;
cout<< 班级列表的大小是 << ???? <<< endl;
}
--------------------------------------- ----------------------------------

想一想...... 也许:

类a:std :: list< baseclass>
{
int keya;
std :: list< sometype> baseclasslist;
};

class b
{
int keyb;
std :: list< a> alist;
};

class c
{
int keyc;
std :: list< b> blist;
};

可能是一个更好的实现,但问题仍然存在。
I have a few classes...

class a : std::list<baseclass>
{
int keya;
};

class b : std::list<a>
{
int keyb;
};

class c : std::list<b>
{
int keyc;
};
I''m trying to figure out at each level the size of the lists...

so if I have:

int main(int argc, char *argv[])
{
c x;

cout << "The size of the c class list is " << x.size() << endl;
cout << "The size of the b class list is " << ???? << endl;
cout << "The size of the a class list is " << ???? <<< endl;
}
-------------------------------------------------------------------------

Thinking about it...
Using inheritance of std::list may not have been the best idea..
perhaps:

class a : std::list<baseclass>
{
int keya;
std::list<sometype> baseclasslist;
};

class b
{
int keyb;
std::list<a> alist;
};

class c
{
int keyc;
std::list<b> blist;
};

May have been a better implementation, but the question remains.



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

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