Changes made via object reference are lost

Changes made via object reference are lost

Post by Svetlana Guljajev » Thu, 19 Oct 2000 04:00:00



Hello !

I am writing a piece of software, extensively using miscellanious data
structures : STL lists, vectors etc. The problem is the following:

I have objects of class MyObject, derived from BaseObjectClass.
I have a data container - DataContainer - holding custom created smart
pointers (template class RefPtr<>) to base class (BaseObjectClass).
What I need is to get a reference to object inside the list and set a couple
of properties on it.
I do the following:

MyObject& objRef = *dynamic_cast<MyObject*>((*iterator).GetAddress());
...
objRef.SetPropertyOne(true);

here :
- iterator - is the DataContainer iterator , operator * of it returns
RefPtr<BaseObjectClass>.
- GetAddress() method returns "bare" pointer to BaseObjectClass from
RefPtr<> object
- I dynamically cast it to MyObject* type and dereference it to initialize
objRef with its value
- SetPropertyOne(true) is trying to set object's property, hoping that this
property will actually be set in the object, stored in the container

But, when I later iterate thru this list and retrieve PropertyOne, it is not
set.

If I use pointers instead of references, everything works OK :

MyObject* objPtr = dynamic_cast<MyObject*>((*iterator).GetAddress());
...
objPtr->SetPropertyOne(true);

Please, explain me this behaviour. I am using Visual C++ 6.0, SP3 on Win2K.

Thanx in advance,
    Svetlana

 
 
 

Changes made via object reference are lost

Post by Victor Bazaro » Thu, 19 Oct 2000 04:00:00



> Hello !

> I am writing a piece of software, extensively using miscellanious data
> structures : STL lists, vectors etc. The problem is the following:

> I have objects of class MyObject, derived from BaseObjectClass.
> I have a data container - DataContainer - holding custom created smart
> pointers (template class RefPtr<>) to base class (BaseObjectClass).
> What I need is to get a reference to object inside the list and set a
couple
> of properties on it.
> I do the following:

> MyObject& objRef = *dynamic_cast<MyObject*>((*iterator).GetAddress());
> ...
> objRef.SetPropertyOne(true);

> here :
> - iterator - is the DataContainer iterator , operator * of it returns
> RefPtr<BaseObjectClass>.
> - GetAddress() method returns "bare" pointer to BaseObjectClass from
> RefPtr<> object
> - I dynamically cast it to MyObject* type and dereference it to
initialize
> objRef with its value
> - SetPropertyOne(true) is trying to set object's property, hoping that
this
> property will actually be set in the object, stored in the container

> But, when I later iterate thru this list and retrieve PropertyOne, it
is not
> set.

> If I use pointers instead of references, everything works OK :

> MyObject* objPtr = dynamic_cast<MyObject*>((*iterator).GetAddress());
> ...
> objPtr->SetPropertyOne(true);

> Please, explain me this behaviour. I am using Visual C++ 6.0, SP3 on

Win2K.

Svetlana,

If the dynamic_cast doesn't fail (returning NULL) the operations
are perfectly equivalent.  Take a look:
================================================================
#include <iostream>
  using std::cout;

class Base
{
public:
    virtual ~Base() {}
    Base* GetAddress() { return this; }

Quote:};

class Derived : public Base
{
public:
    ~Derived() {}
    void foo() { cout << "Derived::foo(" << this << ")\n"; }

Quote:};

typedef Base *Iterator;

int main()
{
    Base *ptr = new Derived[10];
    Iterator iterator = ptr + 5;

    Derived &r = *dynamic_cast<Derived*>((*iterator).GetAddress());
    r.foo();

    Derived *p = dynamic_cast<Derived*>((*iterator).GetAddress());
    p->foo();

    delete[] ptr;

    return 0;

Quote:}

================================================================
Prints out:
Derived::foo(00301D38)
Derived::foo(00301D38)

That is, the object for which it performs the operation (foo()) is
EXACTLY the same.

Of course, there is a possibility that you're not telling us the
whole truth :-)  I mean, that there is something going on in the
rest of your code and you think that the code you posted is wrong...

Good luck!

Victor
--
Please remove capital A's from my address when replying by mail

 
 
 

1. when do changes made via lineConfigDialog take affect?

In my app, I'm programmatically bringing up the modem configuration dialog
for my "comm/datamodem" device.  I change the databits/parity from 8/none to
7/odd, but they don't seem to be taking affect when I continue with my
connection.

When I step through the debugger, the return code from lineConfigDialog is
0, which means SUCCESS.  I then run my CONTROL PANEL->MODEMS and edit the
same modem, and it ALSO sees it still at 8/none.  So my app and control
panel are both seeing the old configuration.  I am NOT using
lineConfigDialogEdit.  My app is configured to use TAPI 1.4, and I'm running
it on Win98.

Regards,

Mark Franjione

2. Accessing Parent Page from web user control

3. Why am I losing the file lock when a child process terminates?

4. 2004 Deluxe Crashes After Internet Updates

5. boy am I lost

6. Roadrunner in San Antonio, Texas

7. Why am I losing performance counters?

8. When did they Break the Bills & Deposits screen?

9. I am totally lost today?

10. Why am I losing performance counters?

11. I am lost and dumb!

12. I am lost on something

13. Playing movies with DirectShow makes main window lose focus