定义一个类中的类 [英] Definig a Class within a Class

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

问题描述

这可能很容易回答,但对我而言是新的。

昨天我意识到需要能够在另一个类别中定义一个类别。所以我编写了一些测试代码,几乎没有语法错误。


我的第一个目标是使用全局变量计算外部类,

起作用罚款


但是我想计算外类中的内部类。

Varibale innernum是全局的内部类,但是

我不知道如何访问它。

我得到的错误是

class.cpp:在构造函数`Outer :: Inner :: Inner ()'':

class.cpp:48:错误:类型`Outer''不是类型`Outer :: Inner'的基本类型


它在下面的测试代码中评论。如果somebode可以显示正确的

语法,我会很高兴。

rds

#include< stdio.h>


int outernum = 0;


class Outer

{

public:


外(无效);

~外();

无效测试(无效);

int innernum;


class Inner

{

public:


Inner(void );

~Inner();

};

};


Outer ::外(无效)

{

printf(" Outer :: Outer\ nn);

innernum = 0;

outernum ++;

printf(" outernum =%d \ n",outernum);

}


Outer ::〜外()

{

printf(" Outer :: ~Outer\ n);

outernum - ;

printf(" outernum =%d \ n",outernum);

}


无效外:: test(void)

{

printf(" Outer :: test\ n);

内部a;

内部b;

}


外部::内部::内部(无效)

{

printf(" Outer :: Inner :: Inner\ nn);

// Outer :: innernum ++;

// printf(" innernum =%d \ n",Outer :: innernum);

}


Outer :: Inner :: ~Inner()

{

printf(" Outer :: Inner ::〜Inner\\\
);

//外面:: innernum--;

// printf(" innernum =%d \ n",Outer :: innernum);

}


int main(无效)

{

外a;

外b;

a.test();

返回0;

}

解决方案



Guenther Sohler <顾************* @ newlogic.at>在消息中写道

news:pa **************************** @ newlogic.at ... < blockquote class =post_quotes>这可能很容易回答,但对我来说它是新的。
昨天我意识到需要能够在另一个类中定义一个类。所以我写了一些测试代码,几乎没有语法错误。

我的第一个目标是使用全局变量计算外部类,
工作正常
但后来我想计算外类中的内部类。
Varibale innernum是内部类的全局代码,但
我不知道如何访问它。
错误我得到的是
class.cpp:在构造函数`Outer :: Inner :: Inner()'':
class.cpp:48:error:type`Outer''不是一个基类型输入
`Outer :: Inner''
它在下面的测试代码中注释。如果somebode可以显示正确的语法,我会很高兴。
rds

#include< stdio.h>

int outernum = 0;

上课类
{
公开:

外(无效);
〜外();
无效test(void);
int innernum;

类内部
{
公开:

内在(无效);
~Internal();
};
};

Outer :: Outer(void)
{
printf(" Outer :: Outer \\ \\ n");
innernum = 0;
outernum ++;
printf(" outernum =%d \ n",outernum);
}

Outer ::〜外()
{/> printf(" Outer :: ~Outer\ n);
outernum--;
printf(" outernum =%d \ n",outernum);
}
void Outer :: test(void)
{
printf(" Outer :: test \\内部a;
内部b;
}

外部::内部::内部(无效)
{
printf(Outer :: Inner :: Inner\ n);
// Outer :: innernum ++;
// printf(& ; innernum =%d \ n,Outer :: innernum;
}

Outer :: Inner ::〜Inner()
{
printf (Outer :: Inner ::〜Inner\\\
);
// Outer :: innernum--;
// printf(" innernum =%d \ n",Outer :: innernum);
}
int main(无效)
{
外部a;
外部b;
a.test( );
返回0;
}




您的代码是正确的,您的编译器已损坏。获得更好的编译器。


john


John Harrison写道:

[SNIP]

您的代码是正确的,您的编译器已损坏。获得更好的编译器。




错误。内部类在C ++中没有对外部类的特殊访问权。


Attila aka WW


Guenther Sohler< gu ** ***********@newlogic.at>写在

新闻:pa **************************** @ newlogic.at:

这可能很容易回答,但对我而言是新的。
昨天我意识到需要能够在另一个类中定义一个类。所以我编写了一些测试代码及其几乎没有语法的语法
没有错误。

我的第一个目标是使用全局变量计算外部类,
工作正常
但是我想要计算外类中的内部类。
Varibale innernum是内部类的全局代码,但是我不知道如何访问它。
class.cpp:在构造函数`Outer :: Inner :: Inner()'':
class.cpp:48:错误:类型`外部''不是类型的基本类型
`Outer :: Inner''



您无法访问Inner内部的任何非静态成员。因为:

你的内部类位于Outer的命名空间内,但这就是全部。任何

Inner都没有绑定到任何外部,所以带有

count变量的外部对象不需要存在且无法通过
$访问b $ b内部。


如果你想访问任何外部非静态数据成员,你需要一个

外部*这个指针来修改它们。


请记住以下内容也是合法的:


//你的课程被剪掉了。


/ /在全球范围内:


Outer :: Inner inner_class; //合法,内在公在外面。


我认为你实际上寻找的是工厂模式,所以

任何内部有一个出境外上课。


This is probably very easy to answer but for me its new.
Yesterday I realized the need to be able to define a class within another
one. So I have written some test code and its almost syntax error free.

My first goal is to count the Outer classes with a global variable, which
works fine

But then I want to count the Inner classes within the Outer class.
The Varibale innernum is global in repsect to the Class Inner, but
I dont know how to access it.
The error I am getting is
class.cpp: In constructor `Outer::Inner::Inner()'':
class.cpp:48: error: type `Outer'' is not a base type for type `Outer::Inner''

Its commented in the testcode below. If somebode could show me the right
syntax, I would be happy.
rds
#include <stdio.h>

int outernum=0;

class Outer
{
public:

Outer(void);
~Outer();
void test(void);
int innernum;

class Inner
{
public:

Inner(void);
~Inner();
};
};

Outer::Outer(void)
{
printf("Outer::Outer\n");
innernum=0;
outernum++;
printf("outernum=%d\n",outernum);
}

Outer::~Outer()
{
printf("Outer::~Outer\n");
outernum--;
printf("outernum=%d\n",outernum);
}

void Outer::test(void)
{
printf("Outer::test\n");
Inner a;
Inner b;
}

Outer::Inner::Inner(void)
{
printf("Outer::Inner::Inner\n");
// Outer::innernum++;
// printf("innernum=%d\n",Outer::innernum);
}

Outer::Inner::~Inner()
{
printf("Outer::Inner::~Inner\n");
// Outer::innernum--;
// printf("innernum=%d\n",Outer::innernum);
}

int main(void)
{
Outer a;
Outer b;
a.test();
return 0;
}

解决方案


"Guenther Sohler" <gu*************@newlogic.at> wrote in message
news:pa****************************@newlogic.at...

This is probably very easy to answer but for me its new.
Yesterday I realized the need to be able to define a class within another
one. So I have written some test code and its almost syntax error free.

My first goal is to count the Outer classes with a global variable, which
works fine

But then I want to count the Inner classes within the Outer class.
The Varibale innernum is global in repsect to the Class Inner, but
I dont know how to access it.
The error I am getting is
class.cpp: In constructor `Outer::Inner::Inner()'':
class.cpp:48: error: type `Outer'' is not a base type for type `Outer::Inner''
Its commented in the testcode below. If somebode could show me the right
syntax, I would be happy.
rds
#include <stdio.h>

int outernum=0;

class Outer
{
public:

Outer(void);
~Outer();
void test(void);
int innernum;

class Inner
{
public:

Inner(void);
~Inner();
};
};

Outer::Outer(void)
{
printf("Outer::Outer\n");
innernum=0;
outernum++;
printf("outernum=%d\n",outernum);
}

Outer::~Outer()
{
printf("Outer::~Outer\n");
outernum--;
printf("outernum=%d\n",outernum);
}

void Outer::test(void)
{
printf("Outer::test\n");
Inner a;
Inner b;
}

Outer::Inner::Inner(void)
{
printf("Outer::Inner::Inner\n");
// Outer::innernum++;
// printf("innernum=%d\n",Outer::innernum);
}

Outer::Inner::~Inner()
{
printf("Outer::Inner::~Inner\n");
// Outer::innernum--;
// printf("innernum=%d\n",Outer::innernum);
}

int main(void)
{
Outer a;
Outer b;
a.test();
return 0;
}



Your code is correct, your compiler is broken. Get a better compiler.

john


John Harrison wrote:
[SNIP]

Your code is correct, your compiler is broken. Get a better compiler.



False. Inner classes have no special access to the outer class in C++.

Attila aka WW


Guenther Sohler <gu*************@newlogic.at> wrote in
news:pa****************************@newlogic.at:

This is probably very easy to answer but for me its new.
Yesterday I realized the need to be able to define a class within
another one. So I have written some test code and its almost syntax
error free.

My first goal is to count the Outer classes with a global variable,
which works fine

But then I want to count the Inner classes within the Outer class.
The Varibale innernum is global in repsect to the Class Inner, but
I dont know how to access it.
The error I am getting is
class.cpp: In constructor `Outer::Inner::Inner()'':
class.cpp:48: error: type `Outer'' is not a base type for type
`Outer::Inner''


You cannot access any nonstatic member of Outer inside Inner. Because:
your Inner class is inside the namespace of Outer, but that''s all. Any
Inner is not bound to any Outer, so there the Outer object carrying the
count variable does not need to be present and is not accessible via
Inner.

If you want to access any Outer nonstatic data members you need an
Outer*this pointer to modify them.

Just keep in mind that the following also is legal:

//your classes snipped.

//at global scope:

Outer::Inner inner_class; //legal, Inner is public in Outer.

I think what you actually were looking for is factory pattern, so that
any Inner has an outbound Outer class.


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

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