在工程可能会遇到这样一个情况,底层的类需要main函数中传参数。但是考虑到系统功能的设计,底层的类又不能直接定义在main函数中,因为底层的类是嵌套在顶层的类里面的,实现相应的功能需要顶层的类的中间结果。
这个时候我们怎样实现把main函数中的参数传给底层的类呢?
下面来看一个例子:为了简单起见,我一共写了两个简单的类,一个是类A,为顶层类、另一个是类B,为底层类。
classA.h
#include<iostream>
#include"classB.h"
using namespace std;
namespace TOPLEVEL
{
class A
{ public:
A(int pa1_);
~A();
void process_system();
void printa();
int pa1;
B *b;
};
}
classA.cpp
#include"classA.h"
namespace TOPLEVEL
{
A::A(int pa1_):pa1(pa1_)
{
b=new B(0,5);
}
A::~A()
{
delete b;
}
void A::process_system()
{
pa1+=2;
}
void A::printa()
{
process_system();
cout<<"sum :"<<b->sum(pa1)<<endl;
}
}
classB.h
#include<iostream>
using namespace std;
namespace TOPLEVEL
{
class B
{ public:
B(int pb1_,int pb2_);
~B(){};
int sum(int in);
int pb1,pb2;
};
}
classB.cpp
#include"classB.h"
namespace TOPLEVEL
{
B::B(int pb1_,int pb2_):pb1(pb1_),pb2(pb2_)
{}
int B::sum(int in)
{
return in+pb2;
}
}
main.cpp
#include<iostream>
#include<string>
#include"classA.h"
using namespace std;
using namespace TOPLEVEL;
int main(int argc,char **argv)
{
/*-----------------------读取参数,并转化成bool类型选择开关choose_botton----------*/
if(argc!=2)
{
cout<<"parameter num must be one,please check the parameter"<<endl;
return -1;
}
string para=argv[1];
string tmp="true";
bool choose_botton=false;
if(para==tmp)
choose_botton=true;
int num1=10;
TOPLEVEL::A *a=new TOPLEVEL::A(num1);
a->printa();
delete a;
system("pause");
}
问题是这样的:假如我们想通过输入参数来 控制classB里的加法输出结果是可调的,要么是pb1+pa1,要么是pb2+pa1,怎么传参数呢?即怎样把choose_botton传给类B呢?
方法一 通过构造函数逐级往里传,这个方法是比较笨的方法,因为需要修改全部的构造函数,如果工程比较庞大,这么做不太可取,因为还肯有其他地方用到classB,如果这样改,其他实例化classB的地方都要修改。
方法二 通过在顶层类(A)里定义静态数据成员,通过静态数据成员传参数。注意,这里涉及到嵌套类的前置声明问题。
方法三 通过全局变量传参数。
方法一:
classA.h
#include<iostream>
#include"classB.h"
using namespace std;
namespace TOPLEVEL
{
class A
{ public:
A(int pa1_,bool button);修改构造函数声明
~A();
void process_system();
void printa();
int pa1;
B *b;
};
}
classA.cpp
#include"classA.h"
namespace TOPLEVEL
{
A::A(int pa1_,bool botton):pa1(pa1_)
{
b=new B(0,5,botton);//传入内层构造函数
}
A::~A()
{
delete b;
}
void A::process_system()
{
pa1+=2;
}
void A::printa()
{
process_system();
cout<<"sum :"<<b->sum(pa1)<<endl;
}
}
classB.h
#include<iostream>
using namespace std;
namespace TOPLEVEL
{
class B
{ public:
B(int pb1_,int pb2_,bool botton_);//修改构造函数声明
~B(){};
bool botton;//传给内层参数
int sum(int in);
int pb1,pb2;
};
}
classB.cpp
#include"classB.h"
namespace TOPLEVEL
{
B::B(int pb1_,int pb2_,bool botton_):pb1(pb1_),pb2(pb2_),botton(botton_)//修改构造函数定义
{}
int B::sum(int in)
{
if(botton)
return in+pb2;
else
return in+pb1;
}
}
main.cpp
#include<iostream>
#include<string>
#include"classA.h"
using namespace std;
using namespace TOPLEVEL;
int main(int argc,char **argv)
{
/*-----------------------读取参数,并转化成bool类型选择开关choose_botton----------*/
if(argc!=2)
{
cout<<"parameter num must be one,please check the parameter"<<endl;
return -1;
}
string para=argv[1];
string tmp="true";
bool choose_botton=false;
if(para==tmp)
choose_botton=true;
int num1=10;
TOPLEVEL::A *a=new TOPLEVEL::A(num1,choose_botton);//传入最外层参数
a->printa();
delete a;
system("pause");
}
方法二:
classA.h
#ifndef CLASSA_
#define CLASSA_//防止重复包含
#include<iostream>
#include"classB.h"
using namespace std;
namespace TOPLEVEL
{ class B;//类前置声明
class A
{ public:
A(int pa1_);
~A();
void process_system();
void printa();
int pa1;
static bool botton;//声明静态变量
B *b;
};
}
<strong>#endif</strong>
classA.cpp
#include"classA.h"
namespace TOPLEVEL
{
A::A(int pa1_):pa1(pa1_)
{
b=new B(0,5);
}
A::~A()
{
delete b;
}
void A::process_system()
{
pa1+=2;
}
void A::printa()
{
process_system();
cout<<"sum :"<<b->sum(pa1)<<endl;
}
bool A::botton=false;//定义静态变量
}
classB.h
#ifndef CLASSB_
#define CLASSB_//防止重复包含
#include<iostream>
#include"classA.h"//
using namespace std;
namespace TOPLEVEL
{
class A;//前置声明
class B
{ public:
B(int pb1_,int pb2_);
~B(){};
int sum(int in);
int pb1,pb2;
};
}
#endif
classB.cpp
#include"classB.h"
namespace TOPLEVEL
{
B::B(int pb1_,int pb2_):pb1(pb1_),pb2(pb2_)
{}
int B::sum(int in)
{
if(A::botton)//实现
return in+pb2;
else
return in+pb1;
}
}
main.cpp
#include<iostream>
#include<string>
#include"classA.h"
using namespace std;
using namespace TOPLEVEL;
int main(int argc,char **argv)
{
/*-----------------------读取参数,并转化成bool类型选择开关choose_botton----------*/
if(argc!=2)
{
cout<<"parameter num must be one,please check the parameter"<<endl;
return -1;
}
string para=argv[1];
string tmp="true";
bool choose_botton=false;
if(para==tmp)
A::botton=true;//修改静态变量
int num1=10;
TOPLEVEL::A *a=new TOPLEVEL::A(num1);
a->printa();
delete a;
system("pause");
}
方法三:
classA.h
#include<iostream>
#include"classB.h"
using namespace std;
namespace TOPLEVEL
{
class A
{ public:
A(int pa1_);
~A();
void process_system();
void printa();
int pa1;
B *b;
};
}
classA.cpp
#include"classA.h"
namespace TOPLEVEL
{
A::A(int pa1_):pa1(pa1_)
{
b=new B(0,5);
}
A::~A()
{
delete b;
}
void A::process_system()
{
pa1+=2;
}
void A::printa()
{
process_system();
cout<<"sum :"<<b->sum(pa1)<<endl;
}
}
classB.h
#include<iostream>
using namespace std;
namespace TOPLEVEL
{
class B
{ public:
B(int pb1_,int pb2_);
~B(){};
int sum(int in);
int pb1,pb2;
};
}
classB.cpp
#include"classB.h"
extern bool button;//全局变量
namespace TOPLEVEL
{
B::B(int pb1_,int pb2_):pb1(pb1_),pb2(pb2_)
{}
int B::sum(int in)
{
if(button)//实现
return in+pb2;
else
return in+pb1;
}
}
main.cpp
#include<iostream>
#include<string>
#include"classA.h"
using namespace std;
using namespace TOPLEVEL;
bool button;//定义全局变量
int main(int argc,char **argv)
{
/*-----------------------读取参数,并转化成bool类型选择开关choose_botton----------*/
if(argc!=2)
{
cout<<"parameter num must be one,please check the parameter"<<endl;
return -1;
}
string para=argv[1];
string tmp="true";
if(para==tmp)
button=true;//赋值
int num1=10;
TOPLEVEL::A *a=new TOPLEVEL::A(num1);
a->printa();
delete a;
system("pause");
}