嵌套类的新功能 [英] New to nested classes

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

问题描述

大家好,

我很难使用嵌套类。大多数示例看起来像

  class  A 
{
public
private

class B {
public
private
}
}



但是我一直在努力的程序要求我实现一个如下所示的嵌套类



  class  A 
{
公开 // 某些功能
私人 B
{
public :string S;
int x;
} // 结束嵌套类
string s2;
int x2;
} // 结束外层





好​​的!所以现在我已经给出了一个我正在使用的模型

我的问题是:

1.我还是为B级做一个构造函数吗? />
看起来像......

  class  A ::  class  A(){ //   A的构造函数 
class A :: class B :: class B(){ // B的构造函数

}





的方式让人感到困惑

OR .....



  class  A ::  class  A(){}  //   class A构造函数 
class A :: Class B :: class B(){} // A构造函数之外的B类构造函数



2.如何使用变量对于B级?

我是否总是要指定它们的位置?

例如

 type < span class =code-keyword> class  A ::  class 一个函数(){
x2 = x + 5 ;
return x2;
}





x2属于A,但编译器是否理解x属于B?



我尝试了什么:



说实话,我的第一次尝试输入如下

  class  A ::  class  A( ){
class A :: class B :: 班级 B(){}
}



我有一种直觉,认为这是完全错误的。我还没有调试,因为我还在编写A类的其余函数

解决方案

你的意思是类似于

  #include   <  字符串 >  
#include < iostream >
使用 命名空间标准

class A
{
public :< span class =code-comment> // 一些函数
A( int x,string s, int x2,string s2);

private
class B
{
public
B( int x,string s);
string s;
int x;
}; // 结束嵌套类

B b ;
string s2;
int x2;

public
string getbs();


}; // 结束外部类


A :: A( int x,string s, int x2 ,string s2):b(x,s),s2(s2),x2(x2)
{
}

A :: B :: B( int x,string s):s(s),x(x)
{
}

string A :: getbs (){ return bs;}

int main()
{
A a( 1 hi 2 hello );

cout<< a.getbs()<< ENDL;
}





请注意,由于访问规则( B A 中的私人信息>您无法在 main <主<中访问 A :: B / code> function。


嵌套类声明 [ ^ ]。

hello all,
I'm having a hard time using nested classes. most examples look like

class A
{
    public: 
    private:

   class B{
      public:
      private:
}
}


However the program I have been working on requires me to implement a nested class that looks like the following

class A
{
public: //some functions
private:  class B
         {
           public:  string s;
                    int x;
         }//ends the nested class
string s2;
int x2;
}//ends outer class



OK! so now that I've given a model of what I'm working with
my questions are:
1. do I still make a constructor for class B?
would that look like ....

class A:: class A( ) {                //constructor for A
   class A:: class B:: class B( ) {     //constructor for B

    }



by the way that looks confusing
OR.....

class A ::class A(){   }          //class A constructor
   class A:: Class B :: class B (){} // class B constructor outside of A's constructor


2. how do I use the variables for class B?
do I always have to specify where they are?
for example

type  class A ::class A function(){
                x2= x+ 5;
               return x2;
     }



x2 belongs to A, but will the compiler understand that x belongs to B?

What I have tried:

to be honest my first attempt was typed like the following

class A :: class A( ){
   class A::class B :: class B(){  }
}


I had a gut feeling that it's completely wrong. I haven't debugged yet because I'm still writing the rest of the functions for class A

解决方案

Do you mean something similar to

#include <string>
#include <iostream>
using namespace std;

class A
{
public: //some functions
  A(int x, string s, int x2, string s2);

private:
  class B
  {
  public:
    B(int x, string s);
    string s;
    int x;
  };//ends the nested class

  B b;
  string s2;
  int x2;

public:
  string getbs();


};//ends outer class


A::A(int x, string s, int x2, string s2):b(x,s),s2(s2),x2(x2)
{
}

A::B::B(int x, string s):s(s), x(x)
{
}

string A::getbs(){return b.s;}

int main()
{
  A a(1,"hi",2,"hello");

  cout << a.getbs() << endl;
}

?

Please note, due to access rules (B is a private in A) you cannot access A::B in the main function.


Nested Class Declarations[^].


这篇关于嵌套类的新功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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