By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,144 Members | 1,250 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,144 IT Pros & Developers. It's quick & easy.

Help with solving seemingly mutually exclusive problems please

P: n/a
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

Oct 19 '07 #1
Share this Question
Share on Google+
12 Replies


P: n/a
Zilla wrote:
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;
Oct 19 '07 #2

P: n/a
On Oct 19, 5:02 pm, red floyd <no.s...@here.dudewrote:
Zilla wrote:
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;- Hide quoted text -

- Show quoted text -- Hide quoted text -

- Show quoted text -
How? ACmd and BCmd are singletons, and that's how one codes one. See
Design Patterns book.

Oct 19 '07 #3

P: n/a
Zilla wrote:
On Oct 19, 5:02 pm, red floyd <no.s...@here.dudewrote:
>Zilla wrote:
>>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;- Hide quoted text -

- Show quoted text -- Hide quoted text -

- Show quoted text -

How? ACmd and BCmd are singletons, and that's how one codes one. See
Design Patterns book.
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 19 '07 #4

P: n/a
Zilla wrote:
On Oct 19, 5:02 pm, red floyd <no.s...@here.dudewrote:
>Zilla wrote:
>>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;- Hide quoted text -

- Show quoted text -- Hide quoted text -

- Show quoted text -

How? ACmd and BCmd are singletons, and that's how one codes one. See
Design Patterns book.
You're thinking so much outside the box that you aren't seeing the
obvious mistake. Hint: it's a copy-and-paste error. Look at it again
and this time really try to see what the function is and what it needs
to do (what every statement needs to do), and what it actually does
(and what the effect of it is).

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 19 '07 #5

P: n/a
On Oct 19, 6:01 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
Zilla wrote:
On Oct 19, 5:02 pm, red floyd <no.s...@here.dudewrote:
Zilla wrote:
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;- Hide quoted text -
- Show quoted text -- Hide quoted text -
- Show quoted text -
How? ACmd and BCmd are singletons, and that's how one codes one. See
Design Patterns book.

You're thinking so much outside the box that you aren't seeing the
obvious mistake. Hint: it's a copy-and-paste error. Look at it again
and this time really try to see what the function is and what it needs
to do (what every statement needs to do), and what it actually does
(and what the effect of it is).

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask- Hide quoted text -

- Show quoted text -
Ah, the _name value, but that's beside the point - I don't care what
the names are - make them same. Look at the pointer values that I
print out. TY

Oct 19 '07 #6

P: n/a
On Oct 19, 6:10 pm, Zilla <zill...@bellsouth.netwrote:
On Oct 19, 6:01 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:


Zilla wrote:
On Oct 19, 5:02 pm, red floyd <no.s...@here.dudewrote:
>Zilla wrote:
>>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;- Hide quoted text -
>- Show quoted text -- Hide quoted text -
>- Show quoted text -
How? ACmd and BCmd are singletons, and that's how one codes one. See
Design Patterns book.
You're thinking so much outside the box that you aren't seeing the
obvious mistake. Hint: it's a copy-and-paste error. Look at it again
and this time really try to see what the function is and what it needs
to do (what every statement needs to do), and what it actually does
(and what the effect of it is).
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask- Hide quoted text -
- Show quoted text -

Ah, the _name value, but that's beside the point - I don't care what
the names are - make them same. Look at the pointer values that I
print out. TY- Hide quoted text -

- Show quoted text -
Ok here's output for correcte _name cut & paste error

With cmds->cmd=cmds1->cmd; uncommented...
cmds->cmd: ACmd 0x0x40004820 ,cmds1->cmd: BCmd 0x0x40004840
cmds->cmd: BCmd 0x0x40004840 ,cmds1->cmd: BCmd 0x0x40004840
equal

With *cmds->cmd=*cmds1->cmd; uncommented
cmds->cmd: ACmd 0x0x40004820 ,cmds1->cmd: BCmd 0x0x40004840
cmds->cmd: BCmd 0x0x40004820 ,cmds1->cmd: BCmd 0x0x40004840

Again, in the first case, value AND content of cmds->cmd changed, but
if passed. In the seconde case, ONLY content changed, bugt if fails.

I want ONLY content change, and passing if. TY again.

Oct 19 '07 #7

P: n/a
Zilla wrote:
On Oct 19, 6:10 pm, Zilla <zill...@bellsouth.netwrote:
>On Oct 19, 6:01 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:

Again, in the first case, value AND content of cmds->cmd changed, but
if passed. In the seconde case, ONLY content changed, bugt if fails.

I want ONLY content change, and passing if. TY again.
OK, I misunderstood. I figured you were looking at the output and
seeing ACmd multiple times and getting confuse.
Oct 19 '07 #8

P: n/a
Zilla wrote:
On Oct 19, 6:10 pm, Zilla <zill...@bellsouth.netwrote:
>On Oct 19, 6:01 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:


>>Zilla wrote:
On Oct 19, 5:02 pm, red floyd <no.s...@here.dudewrote:
Zilla wrote:
>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
[redacted]
>
Again, in the first case, value AND content of cmds->cmd changed, but
if passed. In the seconde case, ONLY content changed, bugt if fails.

I want ONLY content change, and passing if. TY again.
It could be a compiler bug, especially since your compiler is apparently
ancient (by your own admission).
Oct 19 '07 #9

P: n/a
On Oct 19, 6:59 pm, red floyd <no.s...@here.dudewrote:
Zilla wrote:
On Oct 19, 6:10 pm, Zilla <zill...@bellsouth.netwrote:
On Oct 19, 6:01 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
>Zilla wrote:
On Oct 19, 5:02 pm, red floyd <no.s...@here.dudewrote:
Zilla wrote:
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

[redacted]
Again, in the first case, value AND content of cmds->cmd changed, but
if passed. In the seconde case, ONLY content changed, bugt if fails.
I want ONLY content change, and passing if. TY again.

It could be a compiler bug, especially since your compiler is apparently
ancient (by your own admission).
Thanks, but it's less fundamental than that since it happens in Visual
C++ 2005. I use HPUX 10.20 (for technical reasons) at work.

Oct 19 '07 #10

P: n/a
Zilla wrote:
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;
So, after this, the 'cmd' pointer in 'cmds' points to the _same_
object of class 'Cmd' as the 'cmd' pointer in 'cmds1'. IOW, they
both point to the same object.

The comment before this assignment is incorrect. The *contents*
are not overwritten. The pointers both point the same memory.
// 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
To get the different objects to compare equal, you need to compare
objects, not pointers. You can never change the behaviour of the
standard operator == for pointers. You can implement your own
equality operator in Cmd, and then do

if (*cmds->cmd == *BCmd::getInstance()) {
cout << "equal" << endl;
}

thus comparing the objects to which those pointers point.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 22 '07 #11

P: n/a
Zilla wrote:
On Oct 19, 6:10 pm, Zilla <zill...@bellsouth.netwrote:
>On Oct 19, 6:01 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:


>>Zilla wrote:
On Oct 19, 5:02 pm, red floyd <no.s...@here.dudewrote:
Zilla wrote:
>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;- Hide quoted text -
>>>>- Show quoted text -- Hide quoted text -
>>>>- Show quoted text -
>>>How? ACmd and BCmd are singletons, and that's how one codes one.
See Design Patterns book.
>>You're thinking so much outside the box that you aren't seeing the
obvious mistake. Hint: it's a copy-and-paste error. Look at it
again
and this time really try to see what the function is and what it
needs
to do (what every statement needs to do), and what it actually does
(and what the effect of it is).
>>V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask- Hide
quoted text -
>>- Show quoted text -

Ah, the _name value, but that's beside the point - I don't care what
the names are - make them same. Look at the pointer values that I
print out. TY- Hide quoted text -

- Show quoted text -

Ok here's output for correcte _name cut & paste error

With cmds->cmd=cmds1->cmd; uncommented...
cmds->cmd: ACmd 0x0x40004820 ,cmds1->cmd: BCmd 0x0x40004840
cmds->cmd: BCmd 0x0x40004840 ,cmds1->cmd: BCmd 0x0x40004840
equal
"Equal" because they are the same pointers (which you compare).
>
With *cmds->cmd=*cmds1->cmd; uncommented
cmds->cmd: ACmd 0x0x40004820 ,cmds1->cmd: BCmd 0x0x40004840
cmds->cmd: BCmd 0x0x40004820 ,cmds1->cmd: BCmd 0x0x40004840
No "equal" because the pointers are different (and you compare pointers
instead of objects).
Again, in the first case, value AND content of cmds->cmd changed, but
if passed. In the seconde case, ONLY content changed, bugt if fails.

I want ONLY content change, and passing if. TY again.
If you only want the contents to change the assignment should be to
the object, not to the pointer, and comparison has to be between the
two objecs, not between pointers.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 22 '07 #12

P: n/a
On Oct 22, 9:49 am, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
Zilla wrote:
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;

So, after this, the 'cmd' pointer in 'cmds' points to the _same_
object of class 'Cmd' as the 'cmd' pointer in 'cmds1'. IOW, they
both point to the same object.

The comment before this assignment is incorrect. The *contents*
are not overwritten. The pointers both point the same memory.
// 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

To get the different objects to compare equal, you need to compare
objects, not pointers. You can never change the behaviour of the
standard operator == for pointers. You can implement your own
equality operator in Cmd, and then do

if (*cmds->cmd == *BCmd::getInstance()) {
cout << "equal" << endl;
}

thus comparing the objects to which those pointers point.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Thanks, I'll try your ideas

Oct 23 '07 #13

This discussion thread is closed

Replies have been disabled for this discussion.