请帮助解决看似相互排斥的问题 [英] Help with solving seemingly mutually exclusive problems please
问题描述
我将所有内容放在一个文件中以方便使用;我也有#include
< * .hfiles而不是< *因为我的编译器是ANSI C之前所以它需要
需要.h
#include< iostream.h>
#include< iomanip.h>
#include< string.h>
class Cmd {
public:
virtual~Cmd(){}
char * printCmd( )
{
返回_name;
}
受保护:
Cmd( ){}
char _name [16];
};
class ACmd:public Cmd {
public:
static Cmd * getInstance()
{
if(!_instance){
_instance = new ACmd;
}
返回_instance;
}
~ACmd(){}
私人:
ACmd()
{
strcpy(_name," ACmd");
}
静态Cmd * _instance;
};
类BCmd:public Cmd {
public:
static Cmd * getInstance()
{
if(!_instance){
_instance = new BCmd;
}
返回_instance;
}
~BCmd(){}
私人:
BCmd()
{
strcpy(_name," ACmd");
}
静态Cmd * _instance;
};
typedef struct {
Cmd * cmd;
} CmdS;
Cmd * ACmd :: _ instance = NULL;
Cmd * BCmd :: _ instance = NULL;
int main()
{
Cmd * cmd = ACmd :: getInstance();
Cmd * cmd1 = BCmd :: getInstance();
CmdS * cmds =(CmdS *)malloc(sizeof(CmdS));
CmdS * cmds1 =(CmdS *)malloc(sizeof(CmdS));
cmds-> cmd = cmd;
cmds1-> cmd = cmd1;
cout<< " cmds-> cmd:" << cmds-> cmd-> printCmd()
<< hex<< " 0X" << cmds-> cmd
<< " ,cmds1-> cmd:" << cmds1-> cmd-> printCmd()
<< " 0X" << cmds1-> cmd<<结束;
//取消注释下面的一个或另一个作业...
//这满足下面的if语句;
//但是,cmds-> cmd的值AND内容是
//覆盖
cmds-> cmd = cmds1-> cmd;
//这不满足下面的if语句;
//然而,值cmds-> cmd被保留,
//只有它内容更改
// * cmds-> cmd = * cmds1-> cmd;
cout<< " cmds-> cmd:" << cmds-> cmd-> printCmd()
<< hex<< " 0X" << cmds-> cmd
<< " ,cmds1-> cmd:" << cmds1-> cmd-> printCmd()
<< " 0X" << cmds1-> cmd<< endl;
if(cmds-> cmd == BCmd :: getInstance()){
cout<< "等于" <<结束;
}
返回0;
}
使用第一个赋值产生;注意cmds-> cmd的值和
内容如何变化
cmds-> cmd:ACmd 0x0x996e008,cmds1-> cmd:ACmd 0x0x996e020
cmds-> cmd:ACmd 0x0x996e020,cmds1-> cmd:ACmd 0x0x996e020
等于
使用秒分配收益率;这里注意
cmds-> cmd的值是如何变化的,只是内容;但if语句
失败。
cmds-> cmd:ACmd 0x0x8c7b008,cmds1-> cmd:ACmd 0x0x8c7b020
cmds-> cmd:ACmd 0x0x8c7b008,cmds1-> cmd:ACmd 0x0x8c7b020
我怎样才能得到这个?
cmds-> cmd: ACmd 0x0x8c7b008,cmds1-> cmd:ACmd 0x0x8c7b020
cmds-> cmd:ACmd 0x0x8c7b008,cmds1-> cmd:ACmd 0x0x8c7b020
等于
I put everything in one file for ease of use; also I have #include
<*.hfiles instead of the <*since my compiler is pre-ANSI C so it
needs the .h
#include <iostream.h>
#include <iomanip.h>
#include <string.h>
class Cmd {
public:
virtual ~Cmd() {}
char* printCmd()
{
return _name;
}
protected:
Cmd() {}
char _name[16];
};
class ACmd : public Cmd {
public:
static Cmd* getInstance()
{
if (!_instance) {
_instance=new ACmd;
}
return _instance;
}
~ACmd() {}
private:
ACmd()
{
strcpy(_name, "ACmd");
}
static Cmd* _instance;
};
class BCmd : public Cmd {
public:
static Cmd* getInstance()
{
if (!_instance) {
_instance=new BCmd;
}
return _instance;
}
~BCmd() {}
private:
BCmd()
{
strcpy(_name, "ACmd");
}
static Cmd* _instance;
};
typedef struct {
Cmd* cmd;
} CmdS;
Cmd* ACmd::_instance=NULL;
Cmd* BCmd::_instance=NULL;
int main()
{
Cmd* cmd=ACmd::getInstance();
Cmd* cmd1=BCmd::getInstance();
CmdS* cmds=(CmdS*)malloc(sizeof(CmdS));
CmdS* cmds1=(CmdS*)malloc(sizeof(CmdS));
cmds->cmd=cmd;
cmds1->cmd=cmd1;
cout << "cmds->cmd: " << cmds->cmd->printCmd()
<< hex << " 0x" << cmds->cmd
<< " ,cmds1->cmd: " << cmds1->cmd->printCmd()
<< " 0x" << cmds1->cmd << endl;
// Uncomment one or the other assignment below...
// This satisfies the if statement below;
// however, the value AND contents of cmds->cmd are
// overwritten
cmds->cmd=cmds1->cmd;
// This does NOT satisfy the if statement below;
// however, the value cmds->cmd are preserved,
// only its contents change
// *cmds->cmd=*cmds1->cmd;
cout << "cmds->cmd: " << cmds->cmd->printCmd()
<< hex << " 0x" << cmds->cmd
<< " ,cmds1->cmd: " << cmds1->cmd->printCmd()
<< " 0x" << cmds1->cmd << endl;
if (cmds->cmd==BCmd::getInstance()) {
cout << "equal" << endl;
}
return 0;
}
Using the first assignment yields; notice how the both value and
content of cmds->cmd changed
cmds->cmd: ACmd 0x0x996e008 ,cmds1->cmd: ACmd 0x0x996e020
cmds->cmd: ACmd 0x0x996e020 ,cmds1->cmd: ACmd 0x0x996e020
equal
Using the second assignment yields; here notice how the value of
cmds->cmd did NOT change, only the content; however the if statement
failed.
cmds->cmd: ACmd 0x0x8c7b008 ,cmds1->cmd: ACmd 0x0x8c7b020
cmds->cmd: ACmd 0x0x8c7b008 ,cmds1->cmd: ACmd 0x0x8c7b020
How can I get this?
cmds->cmd: ACmd 0x0x8c7b008 ,cmds1->cmd: ACmd 0x0x8c7b020
cmds->cmd: ACmd 0x0x8c7b008 ,cmds1->cmd: ACmd 0x0x8c7b020
equal
推荐答案
Zilla写道:
Zilla wrote:
我将所有内容放在一个文件中以方便使用;我也有#include
< * .hfiles而不是< *因为我的编译器是ANSI C之前所以它需要
需要.h
#include< iostream.h>
#include< iomanip.h>
#include< string.h>
class Cmd {
public:
virtual~Cmd(){}
char * printCmd( )
{
返回_name;
}
受保护:
Cmd( ){}
char _name [16];
};
class ACmd:public Cmd {
public:
static Cmd * getInstance()
{
if(!_instance){
_instance = new ACmd;
}
返回_instance;
}
~ACmd(){}
私人:
ACmd()
{
strcpy(_name," ACmd");
}
静态Cmd * _instance;
};
类BCmd :public Cmd {
public:
static Cmd * getInstance()
{
if(!_instance) {
_instance = new BCmd;
}
返回_instance;
}
~BCmd(){}
私人:
BCmd()
{
strcpy(_name," ; ACMD");
I put everything in one file for ease of use; also I have #include
<*.hfiles instead of the <*since my compiler is pre-ANSI C so it
needs the .h
#include <iostream.h>
#include <iomanip.h>
#include <string.h>
class Cmd {
public:
virtual ~Cmd() {}
char* printCmd()
{
return _name;
}
protected:
Cmd() {}
char _name[16];
};
class ACmd : public Cmd {
public:
static Cmd* getInstance()
{
if (!_instance) {
_instance=new ACmd;
}
return _instance;
}
~ACmd() {}
private:
ACmd()
{
strcpy(_name, "ACmd");
}
static Cmd* _instance;
};
class BCmd : public Cmd {
public:
static Cmd* getInstance()
{
if (!_instance) {
_instance=new BCmd;
}
return _instance;
}
~BCmd() {}
private:
BCmd()
{
strcpy(_name, "ACmd");
我怀疑你的错误就在这里。
I suspect your error lies here.
}
static Cmd * _instance;
};
typedef struct {
Cmd * cmd;
} CmdS;
}
static Cmd* _instance;
};
typedef struct {
Cmd* cmd;
} CmdS;
10月19日,下午5:02,red floyd< no.s ... @ here.dudewrote:
On Oct 19, 5:02 pm, red floyd <no.s...@here.dudewrote:
Zilla写道:
Zilla wrote:
我把所有东西放在一个文件中以方便使用;我也有#include
< * .hfiles而不是< *因为我的编译器是ANSI-C之前所以它
需要.h
I put everything in one file for ease of use; also I have #include
<*.hfiles instead of the <*since my compiler is pre-ANSI C so it
needs the .h
#include< iostream.h>
#include< iomanip.h>
#include < string.h中>
#include <iostream.h>
#include <iomanip.h>
#include <string.h>
class Cmd {
public:
virtual~Cmd(){}
char * printCmd()
{
return _name;
}
protected:
Cmd(){}
char _name [16];
};
class Cmd {
public:
virtual ~Cmd() {}
char* printCmd()
{
return _name;
}
protected:
Cmd() {}
char _name[16];
};
class ACmd:public Cmd {
public:
static Cmd * getInstance()
{
if(!_instance){
_instance = new ACmd;
}
返回_instance;
}
~ACmd(){}
私人:
ACmd()
{
strcpy(_name," ACmd");
}
static Cmd * _instance;
};
class ACmd : public Cmd {
public:
static Cmd* getInstance()
{
if (!_instance) {
_instance=new ACmd;
}
return _instance;
}
~ACmd() {}
private:
ACmd()
{
strcpy(_name, "ACmd");
}
static Cmd* _instance;
};
class BCmd:public Cmd {
public:
static Cmd * getInstance()
{
if(!_instance){
_instance = new BCmd;
}
返回_instance;
}
~BCmd(){}
私人:
BCmd()
{
strcpy(_name," ACmd");
class BCmd : public Cmd {
public:
static Cmd* getInstance()
{
if (!_instance) {
_instance=new BCmd;
}
return _instance;
}
~BCmd() {}
private:
BCmd()
{
strcpy(_name, "ACmd");
我怀疑你的错误就在这里。
I suspect your error lies here.
}
static Cmd * _instance;
};
}
static Cmd* _instance;
};
typedef struct {
Cmd * cmd;
} CmdS; - 隐藏引用的文字 -
typedef struct {
Cmd* cmd;
} CmdS;- Hide quoted text -
- 显示引用文字 - 隐藏引用文字 -
- 显示引用文字 -
- Show quoted text -- Hide quoted text -
- Show quoted text -
如何? ACmd和BCmd是单例,这就是一个编码的方式。参见
设计模式书。
How? ACmd and BCmd are singletons, and that''s how one codes one. See
Design Patterns book.
Zilla写道:
Zilla wrote:
10月19日下午5:02,red floyd< no.s ... @ here.dudewrote:
On Oct 19, 5:02 pm, red floyd <no.s...@here.dudewrote:
> Zilla写道:
>Zilla wrote:
>>我将所有内容放在一个文件中以方便使用;我也有#include
< * .hfiles而不是< *因为我的编译器是ANSI-C之前所以它需要.h
>>I put everything in one file for ease of use; also I have #include
<*.hfiles instead of the <*since my compiler is pre-ANSI C so it
needs the .h
>> #include< iostream.h>
#include< iomanip.h>
#include< string.h>
>>#include <iostream.h>
#include <iomanip.h>
#include <string.h>
>> class Cmd {
public:
virtual~Cmd(){}
char * printCmd()
{
返回_name;
}
受保护:
Cmd(){}
char _name [16];
} ;
>>class Cmd {
public:
virtual ~Cmd() {}
char* printCmd()
{
return _name;
}
protected:
Cmd() {}
char _name[16];
};
>> class ACmd:public Cmd {
public:
static Cmd * getInstance()
{
if(!_instance){
_instance = new ACmd;
}
return _instance;
}
~ACmd(){}
私人:
ACmd()
{
strcpy(_name," ACmd");
}
静态Cmd * _instance;
};
>>class ACmd : public Cmd {
public:
static Cmd* getInstance()
{
if (!_instance) {
_instance=new ACmd;
}
return _instance;
}
~ACmd() {}
private:
ACmd()
{
strcpy(_name, "ACmd");
}
static Cmd* _instance;
};
>> class BCmd:public Cmd {
public:
static Cmd * getInstance()
{
if(!_instance){
_instance = new BCmd;
}
return _instance;
}
~BCmd(){}
私人:
BCmd()
{
strcpy(_name," ACmd");
>>class BCmd : public Cmd {
public:
static Cmd* getInstance()
{
if (!_instance) {
_instance=new BCmd;
}
return _instance;
}
~BCmd() {}
private:
BCmd()
{
strcpy(_name, "ACmd");
我怀疑你的错误就在这里。
I suspect your error lies here.
>> }
static Cmd * _instance;
};
>> }
static Cmd* _instance;
};
>> typedef struct {
Cmd * cmd;
} CmdS; - 隐藏引用的文字 -
>>typedef struct {
Cmd* cmd;
} CmdS;- Hide quoted text -
- 显示引用的文字 - 隐藏引用的文字 -
- 显示引用的文字 -
- Show quoted text -- Hide quoted text -
- Show quoted text -
如何? ACmd和BCmd是单例,这就是一个编码的方式。参见
设计模式书。
How? ACmd and BCmd are singletons, and that''s how one codes one. See
Design Patterns book.
-
请在通过电子邮件回复时删除资金''A'
我请不要回复最热门的回复,请不要问
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask
这篇关于请帮助解决看似相互排斥的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!