Help with solving seemingly mutually exclusive problems please

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
 
R

red floyd

Zilla said:
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");
I suspect your error lies here.
 
Z

Zilla

Zilla said:
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");

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.
 
V

Victor Bazarov

Zilla said:
Zilla said:
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");

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.
 
V

Victor Bazarov

Zilla said:
Zilla said:
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");

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
 
Z

Zilla

Zilla said:
Zilla wrote:
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");
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
 
Z

Zilla

Zilla said:
Zilla wrote:
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");
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).
- 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.
 
R

red floyd

Zilla said:
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.
 
R

red floyd

Zilla said:
[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).
 
Z

Zilla

Zilla said:
Zilla wrote:
Zilla wrote:
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
[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.
 
V

Victor Bazarov

Zilla said:
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;

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
 
V

Victor Bazarov

Zilla said:
Zilla wrote:
Zilla wrote:
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");
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).
- 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
 
Z

Zilla

Zilla said:
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;

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

Thanks, I'll try your ideas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top