Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Re: Plugin using classes

Reply
Thread Tools

Re: Plugin using classes

 
 
Victor Bazarov
Guest
Posts: n/a
 
      06-30-2003
"Espen Ruud Schultz" <(E-Mail Removed)> 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


 
Reply With Quote
 
 
 
 
Espen Ruud Schultz
Guest
Posts: n/a
 
      06-30-2003
"Victor Bazarov" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)
> "Espen Ruud Schultz" <(E-Mail Removed)> 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


 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      06-30-2003
"Espen Ruud Schultz" <(E-Mail Removed)> wrote...
> "Victor Bazarov" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)
> > "Espen Ruud Schultz" <(E-Mail Removed)> 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


 
Reply With Quote
 
Espen Ruud Schultz
Guest
Posts: n/a
 
      06-30-2003
"Victor Bazarov" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)
> "Espen Ruud Schultz" <(E-Mail Removed)> wrote...
>> "Victor Bazarov" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)
>>> "Espen Ruud Schultz" <(E-Mail Removed)> 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 **** 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


 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      06-30-2003
"Espen Ruud Schultz" <(E-Mail Removed)> 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


 
Reply With Quote
 
Espen Ruud Schultz
Guest
Posts: n/a
 
      06-30-2003
"Victor Bazarov" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)
> "Espen Ruud Schultz" <(E-Mail Removed)> 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;

}


 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      06-30-2003
"Espen Ruud Schultz" <(E-Mail Removed)> wrote...
> "Espen Ruud Schultz" <(E-Mail Removed)> wrote in message
> news:m92Ma.10105$(E-Mail Removed)
> >
> > 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Eclipse Plugin: how to modify perl build path from an eclipse Plugin java class eser@libero.it Java 1 09-07-2007 09:45 PM
Getting reference of eclipse plugin view or its component from another eclipse plugin view? Jimmy Java 0 03-15-2007 11:04 PM
tomcat-maven-plugin problem - mvn is looking for org.apache.maven.plugins:maven-tomcat-plugin Marcin Cenkier Java 1 04-12-2006 08:00 PM
Plugin type discovery/registration - How do I discover what classes were loaded when I dynamically require in ruby files from a directory? Jeff Barczewski Ruby 4 02-27-2006 09:19 PM
Detect if Sun plugin is installed (in IE) EVEN if not set as default in plugin? PilotYid Java 1 10-31-2005 11:13 AM



Advertisments