Re: Plugin using classes

Discussion in 'C++' started by Victor Bazarov, Jun 30, 2003.

  1. "Espen Ruud Schultz" <> wrote...
    > [...] But I'm having problems:
    >
    > void PlugIn( First *TempObject ) {
    >
    > Second *SecondObject = new Second;
    >
    > std::cout << "&SecondObject: " << &SecondObject << std::endl;
    > std::cout << "&TempObject: " << &TempObject << std::endl;
    >
    > TempObject = SecondObject;
    >
    > std::cout << "&SecondObject: " << &SecondObject << std::endl;
    > std::cout << "&TempObject: " << &TempObject << std::endl;
    >
    > }
    >
    > TempObject never gets SecondObject's pointer. No errors, it's just like

    the
    > line "TempObject = SecondObject;" never happened. First and Second are

    the
    > two classes, Second is derivated from First...


    What are you trying to accomplish with this? What do you expect
    to see when "TempObject = SecondObject;" actually happens? How
    do you know it doesn't happen?

    If your purpose is to _return_ 'SecondObject' to the caller of
    your 'PlugIn' function, then you need to pass a _reference_ to
    a pointer into the function:

    void PlugIn(First*& TempObject)

    otherwise you're changing a local variable and outside world
    will never see that change (not to mention you have a memory leak).

    Victor
    Victor Bazarov, Jun 30, 2003
    #1
    1. Advertising

  2. "Victor Bazarov" <> wrote in message
    news:
    > "Espen Ruud Schultz" <> wrote...
    >> [...] But I'm having problems:
    >>
    >> void PlugIn( First *TempObject ) {
    >>
    >> Second *SecondObject = new Second;
    >>
    >> std::cout << "&SecondObject: " << &SecondObject << std::endl;
    >> std::cout << "&TempObject: " << &TempObject << std::endl;
    >>
    >> TempObject = SecondObject;
    >>
    >> std::cout << "&SecondObject: " << &SecondObject << std::endl;
    >> std::cout << "&TempObject: " << &TempObject << std::endl;
    >>
    >> }
    >>
    >> TempObject never gets SecondObject's pointer. No errors, it's just
    >> like the line "TempObject = SecondObject;" never happened. First and
    >> Second are the two classes, Second is derivated from First...

    >
    > What are you trying to accomplish with this? What do you expect
    > to see when "TempObject = SecondObject;" actually happens? How
    > do you know it doesn't happen?
    >


    I expect the pointer TempObject to point to the object SecondObject, but
    this doesn't happen. TempObject still points to the old object after the
    line "TempObject = SecondObject;"...

    >
    > If your purpose is to _return_ 'SecondObject' to the caller of
    > your 'PlugIn' function, then you need to pass a _reference_ to
    > a pointer into the function:
    >
    > void PlugIn(First*& TempObject)
    >
    > otherwise you're changing a local variable and outside world
    > will never see that change (not to mention you have a memory leak).
    >


    Yeah, that could work, but first I gotta make the line where I assign the
    SecondObject to the TempObject to work. If you see those cout lines before
    and after the assign code? The after output is exactly the same as the
    before output. I would at least expect &TempObject to have a new address...

    , Espen
    Espen Ruud Schultz, Jun 30, 2003
    #2
    1. Advertising

  3. "Espen Ruud Schultz" <> wrote...
    > "Victor Bazarov" <> wrote in message
    > news:
    > > "Espen Ruud Schultz" <> wrote...
    > >> [...] But I'm having problems:
    > >>
    > >> void PlugIn( First *TempObject ) {
    > >>
    > >> Second *SecondObject = new Second;
    > >>
    > >> std::cout << "&SecondObject: " << &SecondObject << std::endl;
    > >> std::cout << "&TempObject: " << &TempObject << std::endl;
    > >>
    > >> TempObject = SecondObject;
    > >>
    > >> std::cout << "&SecondObject: " << &SecondObject << std::endl;
    > >> std::cout << "&TempObject: " << &TempObject << std::endl;
    > >>
    > >> }
    > >>
    > >> TempObject never gets SecondObject's pointer. No errors, it's just
    > >> like the line "TempObject = SecondObject;" never happened. First and
    > >> Second are the two classes, Second is derivated from First...

    > >
    > > What are you trying to accomplish with this? What do you expect
    > > to see when "TempObject = SecondObject;" actually happens? How
    > > do you know it doesn't happen?
    > >

    >
    > I expect the pointer TempObject to point to the object SecondObject, but
    > this doesn't happen. TempObject still points to the old object after the
    > line "TempObject = SecondObject;"...
    >
    > >
    > > If your purpose is to _return_ 'SecondObject' to the caller of
    > > your 'PlugIn' function, then you need to pass a _reference_ to
    > > a pointer into the function:
    > >
    > > void PlugIn(First*& TempObject)
    > >
    > > otherwise you're changing a local variable and outside world
    > > will never see that change (not to mention you have a memory leak).
    > >

    >
    > Yeah, that could work, but first I gotta make the line where I assign the
    > SecondObject to the TempObject to work. If you see those cout lines

    before
    > and after the assign code? The after output is exactly the same as the
    > before output. I would at least expect &TempObject to have a new

    address...

    Why? '&TempObject' is the address of the variable called 'TempObject',
    not the contents of it. When you assign to 'TempObject', you change
    the _contents_ of the variable, not its address. Perhaps you needed
    to do

    cout << (void*)TempObject << endl;

    to see the change? Or you could do

    cout << *TempObject << endl;

    before and after to see what _object_ it points to. But outputting
    the address (&TempObject) will not help you see the result of the
    assignment operator.

    Victor
    Victor Bazarov, Jun 30, 2003
    #3
  4. "Victor Bazarov" <> wrote in message
    news:
    > "Espen Ruud Schultz" <> wrote...
    >> "Victor Bazarov" <> wrote in message
    >> news:
    >>> "Espen Ruud Schultz" <> wrote...
    >>>> [...] But I'm having problems:
    >>>>
    >>>> void PlugIn( First *TempObject ) {
    >>>>
    >>>> Second *SecondObject = new Second;
    >>>>
    >>>> std::cout << "&SecondObject: " << &SecondObject << std::endl;
    >>>> std::cout << "&TempObject: " << &TempObject << std::endl;
    >>>>
    >>>> TempObject = SecondObject;
    >>>>
    >>>> std::cout << "&SecondObject: " << &SecondObject << std::endl;
    >>>> std::cout << "&TempObject: " << &TempObject << std::endl;
    >>>>
    >>>> }
    >>>>
    >>>> TempObject never gets SecondObject's pointer. No errors, it's just
    >>>> like the line "TempObject = SecondObject;" never happened. First and
    >>>> Second are the two classes, Second is derivated from First...
    >>>
    >>> What are you trying to accomplish with this? What do you expect
    >>> to see when "TempObject = SecondObject;" actually happens? How
    >>> do you know it doesn't happen?
    >>>

    >>
    >> I expect the pointer TempObject to point to the object SecondObject, but
    >> this doesn't happen. TempObject still points to the old object after
    >> the line "TempObject = SecondObject;"...
    >>
    >>>
    >>> If your purpose is to _return_ 'SecondObject' to the caller of
    >>> your 'PlugIn' function, then you need to pass a _reference_ to
    >>> a pointer into the function:
    >>>
    >>> void PlugIn(First*& TempObject)
    >>>
    >>> otherwise you're changing a local variable and outside world
    >>> will never see that change (not to mention you have a memory leak).
    >>>

    >>
    >> Yeah, that could work, but first I gotta make the line where I assign
    >> the SecondObject to the TempObject to work. If you see those cout
    >> lines before and after the assign code? The after output is exactly
    >> the same as the before output. I would at least expect &TempObject to
    >> have a new address...

    >
    > Why? '&TempObject' is the address of the variable called 'TempObject',
    > not the contents of it. When you assign to 'TempObject', you change
    > the _contents_ of the variable, not its address. Perhaps you needed
    > to do
    >
    > cout << (void*)TempObject << endl;
    >
    > to see the change? Or you could do
    >
    > cout << *TempObject << endl;
    >
    > before and after to see what _object_ it points to. But outputting
    > the address (&TempObject) will not help you see the result of the
    > assignment operator.
    >


    As usual I mix this shit up. Ok, to start over with something simpler and
    more accurate to what I'm trying to do:

    First *PlugIn( void ) {

    return new Second;

    }

    void main( void ) {

    First *FirstObject;
    FirstObject = PlugIn();

    }

    How do I get the pointer FirstObject to point to the Second object created
    in PlugIn()? Because the way its done here doesn't work. FirstObject still
    points to the First object even after the call to PlugIn()...

    , Espen
    Espen Ruud Schultz, Jun 30, 2003
    #4
  5. "Espen Ruud Schultz" <> wrote...
    > [...] Ok, to start over with something simpler and
    > more accurate to what I'm trying to do:
    >
    > First *PlugIn( void ) {
    >
    > return new Second;
    >
    > }
    >
    > void main( void ) {


    int main( void ) {

    >
    > First *FirstObject;
    > FirstObject = PlugIn();
    >
    > }
    >
    > How do I get the pointer FirstObject to point to the Second object created
    > in PlugIn()?


    But you did it, already. Of course, for simplicity's sake, you
    could have written it in one line:

    First *FirstObject = PlugIn();

    > Because the way its done here doesn't work. FirstObject still
    > points to the First object even after the call to PlugIn()...


    I don't think you understand yourself what you're saying...

    Yes, it points to First subobject of Second. What did you expect?
    But that's how all derived-to-base pointer conversions work. You
    need to utilise polymorphism there:

    #include <iostream>
    class First {
    public:
    virtual ~First() {}
    virtual void Foo() { std::cout << "First::Foo()\n"; }
    };

    class Second : public First {
    public:
    void Foo() { std::cout << "Second::Foo()\n"; }
    };

    First* PlugIn() {
    return new Second;
    }

    int main() {
    First *FirstObject = PlugIn();
    FirstObject->Foo(); // will print "Second::Foo()\n"
    delete FirstObject;
    return 0;
    }

    Victor
    Victor Bazarov, Jun 30, 2003
    #5
  6. "Victor Bazarov" <> wrote in message
    news:
    > "Espen Ruud Schultz" <> wrote...
    >> [...] Ok, to start over with something simpler and
    >> more accurate to what I'm trying to do:
    >>
    >> First *PlugIn( void ) {
    >>
    >> return new Second;
    >>
    >> }
    >>
    >> void main( void ) {

    >
    > int main( void ) {
    >
    >>
    >> First *FirstObject;
    >> FirstObject = PlugIn();
    >>
    >> }
    >>
    >> How do I get the pointer FirstObject to point to the Second object
    >> created in PlugIn()?

    >
    > But you did it, already. Of course, for simplicity's sake, you
    > could have written it in one line:
    >
    > First *FirstObject = PlugIn();
    >
    >> Because the way its done here doesn't work. FirstObject still
    >> points to the First object even after the call to PlugIn()...

    >
    > I don't think you understand yourself what you're saying...
    >
    > Yes, it points to First subobject of Second. What did you expect?
    > But that's how all derived-to-base pointer conversions work. You
    > need to utilise polymorphism there:
    >
    > #include <iostream>
    > class First {
    > public:
    > virtual ~First() {}
    > virtual void Foo() { std::cout << "First::Foo()\n"; }
    > };
    >
    > class Second : public First {
    > public:
    > void Foo() { std::cout << "Second::Foo()\n"; }
    > };
    >
    > First* PlugIn() {
    > return new Second;
    > }
    >
    > int main() {
    > First *FirstObject = PlugIn();
    > FirstObject->Foo(); // will print "Second::Foo()\n"
    > delete FirstObject;
    > return 0;
    > }
    >


    But it doesn't print "Second::Foo()\n", it prints "First::Foo()\n". I've
    been trying all you exampleson a similar code, and I do not get the result
    you say I will. Here's my entire code, check it out please:

    Sorry for all the whitespace

    #include <iostream>

    /** FIRST
    ********************************************************************/
    class First {

    public:

    First( void );
    ~First( void );

    public:

    int Number;

    void Test( void );

    };

    First::First( void ) {

    std::cout << "First::First()" << std::endl;

    }

    First::~First( void ) {

    std::cout << "First::~First()" << std::endl;

    }

    void First::Test( void ) {

    std::cout << "First::Test()" << std::endl;

    Number = 1;
    std::cout << "First::Number = " << Number << std::endl;

    }

    /** SECOND
    *******************************************************************/
    class Second : public First {

    public:

    Second( void );
    ~Second( void );

    public:

    void Test( void );

    };

    Second::Second( void ) {

    std::cout << "Second::Second()" << std::endl;

    }

    Second::~Second( void ) {

    std::cout << "Second::~Second()" << std::endl;

    }

    void Second::Test( void ) {

    std::cout << "Second::Test()" << std::endl;

    Number = 2;
    std::cout << "Second::Number = " << Number << std::endl;

    }

    First *PlugIn( ) {

    return new Second;

    }

    /** MAIN
    *********************************************************************/
    int main( void ) {

    std::cout << "Start!" << std::endl;

    First *FirstObject = new First;

    FirstObject->Test();

    std::cout << "&FirstObject: " << &FirstObject << std::endl;

    FirstObject = PlugIn();

    std::cout << "&FirstObject: " << &FirstObject << std::endl;

    FirstObject->Test();

    delete FirstObject;

    std::cout << "Stop!" << std::endl; return 0;

    }
    Espen Ruud Schultz, Jun 30, 2003
    #6
  7. "Espen Ruud Schultz" <> wrote...
    > "Espen Ruud Schultz" <> wrote in message
    > news:m92Ma.10105$
    > >
    > > But it doesn't print "Second::Foo()\n", it prints "First::Foo()\n".

    I've
    > > been trying all you exampleson a similar code, and I do not get the
    > > result you say I will. Here's my entire code, check it out please:
    > >

    >
    > Darnit! I just forgot virtual! Why do I never see my own errors until I
    > post them for others to see?


    Try to follow a simple rule: if somebody posts the code and claims
    that it works a certain way, don't re-type it into your editor,
    copy-and-paste it (or save the posting as a text file, then edit
    away the excess), then just compile and run. I rarely post the code
    without testing it first...

    I'm glad that you've now solved it.

    Victor
    Victor Bazarov, Jun 30, 2003
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page