473,387 Members | 1,282 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Help with solving seemingly mutually exclusive problems please

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
12 1468
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
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
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Vinay Aggarwal | last post by:
I have been thinking about the lazy initialization and double checked locking problem. This problem is explain in detail here http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html...
5
by: wooks | last post by:
I have defined a schema with an xsd:choice element for 2 mutually exclusive fields. When both are present I get an error which is good, but what is not so good is the error message which says...
3
by: softengine | last post by:
Can and how do you alter a data view to include a look up field from another data table? The data table of the dataview only has the key, the value I need is in another data table. Can and how...
5
by: WertmanTheMad | last post by:
Ive been playing with this for a few days and thought I might thow it out for seggestions. I have Several Queries that need counts returned The Queries are Mutually Exclusive meaning whatever...
4
by: ScottAR | last post by:
I'm writing simulator to help with testing out problems with deadlocking on a database. I have a function which I call from a delegate BeginInvoke call, and depending on the simulation I might...
2
by: js | last post by:
I include a asp:RadioButton in an ItemTemplate of a DataGrid like the following. However, the radio buttons are not mutual exclusive where select one will deselect the rest of the radio buttons. I...
2
by: Stimp | last post by:
I'm getting the error: 'SelectedIndex' and 'SelectedValue' attributes are mutually exclusive when I try to populate a series of dropdowns... any idea what I could be doing wrong here? ...
5
by: Stimp | last post by:
Hi all, I've come back to this problem again and I've identified which part of my code is producing the error: "'SelectedIndex' and 'SelectedValue' attributes are mutually exclusive" I...
4
by: Dooman | last post by:
Hello, I am getting this error when binding a drop down list box to a dataset: The 'SelectedIndex' and 'SelectedValue' attributes are mutually exclusive I have looked at other posts and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.