Z
Zilla
I put everything in one file for ease of use; also I have #include
<*.h> files 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
<*.h> files 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