a pointer to a derived class problem



 DEVELOP > c-Plus-Plus > a pointer to a derived class problem

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "stefven blonqhern"
Date: 02 Aug 2007 06:35:06 AM
Object: a pointer to a derived class problem
hello all, having a problem with derived classes. i'll show by
pseudo code example. first the base class and derived classes:
class Shape { // base class for all shapes
public:
virtual void Draw() {// draw code here}
};
class Square : public Shape {// etc};
class Circle : public Shape {// etc};
class Triangle : public Shape {// etc};
then I create another class called MyClass which has a member function
ChangeShape which creates a new Shape object and assigns a pointer to
it. the member function Render is being called constantly in the
background.
class MyClass {
public:
void ChangeShape(int shape) {
// if shape = 0 then sh = Square, 1 = Circle, 2 = Triangle
// but for the sake of brevity i'll just use a square
sh = Square();
shPtr = □
}
void Render() {shPtr->Draw};
Shape sh;
Shape* shPtr;
};
obviously whenever i access the member function Draw through the
Shape* (shPtr) it always calls the member for Shape not Square because
sh is of type Shape. all this might sound ridiculous? can i assign
derived classes to sh and keep the derived class type instead of them
being converted to the base class type? my implementation is somewhat
limited by the plug-in architecture i must work within.
TIA for any help, hope i explained that clearly.
Stephen.
.

User: "Victor Bazarov"

Title: Re: a pointer to a derived class problem 02 Aug 2007 06:53:43 AM
stefven blonqhern wrote:

hello all, having a problem with derived classes. i'll show by
pseudo code example. first the base class and derived classes:

class Shape { // base class for all shapes
public:
virtual void Draw() {// draw code here}
};

class Square : public Shape {// etc};
class Circle : public Shape {// etc};
class Triangle : public Shape {// etc};

then I create another class called MyClass which has a member function
ChangeShape which creates a new Shape object and assigns a pointer to
it. the member function Render is being called constantly in the
background.

class MyClass {
public:

void ChangeShape(int shape) {
// if shape = 0 then sh = Square, 1 = Circle, 2 = Triangle
// but for the sake of brevity i'll just use a square
sh = Square();

That's a VERY BAD IDEA(tm). What happens is called "slicing". The
object 'sh' is of the type 'Shape' and it is assigned only the base
class subobject from the temporary 'Square'. All the characteristics
(polymorphic included) are lost.

shPtr = □

That's not even C++. Did you mean
shPtr = &sh;
?

}

void Render() {shPtr->Draw};

Shape sh;
Shape* shPtr;
};

obviously whenever i access the member function Draw through the
Shape* (shPtr) it always calls the member for Shape not Square because
sh is of type Shape. all this might sound ridiculous?

Is that a question.

can i assign
derived classes to sh and keep the derived class type instead of them
being converted to the base class type?

No. Read up on "slicing".

my implementation is somewhat
limited by the plug-in architecture i must work within.

What does that mean?
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
.
User: "stefven blonqhern"

Title: Re: a pointer to a derived class problem 02 Aug 2007 07:29:18 AM
On 2 Aug, 12:53, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:

That's a VERY BAD IDEA(tm).

i suspected as much and Bjarne Stroustrup (Special Edition) has just
confirmed it.

What happens is called "slicing". The
object 'sh' is of the type 'Shape' and it is assigned only the base
class subobject from the temporary 'Square'. All the characteristics
(polymorphic included) are lost.

shPtr = &Square;


That's not even C++. Did you mean

shPtr = &sh;

yes i meant shPtr = &sh;
so what i'd need is something like:
Square shSqr;
Circle shCir;
Triangle shTri;
in the MyClass definition. then i can just assign shPtr to the
appropriate object after i create it in ChangeShape. this feels a
little clumsy though and wasteful since i will be creating many
objects that potentially won't get used.


my implementation is somewhat
limited by the plug-in architecture i must work within.


What does that mean?

just means that i don't have free reign to completely rework the
design. what i am doing has to fit into something else so to speak.
thanks for the help, Stephen.
.
User: "Victor Bazarov"

Title: Re: a pointer to a derived class problem 02 Aug 2007 07:32:56 AM
stefven blonqhern wrote:

[..]
so what i'd need is something like:

Square shSqr;
Circle shCir;
Triangle shTri;

in the MyClass definition. then i can just assign shPtr to the
appropriate object after i create it in ChangeShape. this feels a
little clumsy though and wasteful since i will be creating many
objects that potentially won't get used.

No, a better idea is to create your 'Square' or 'Circle' or whatever
*dynamically*, and only store the pointer to it:
shPtr = new Square; // for example
Don't store the object, it's unnecessary to duplicate it. The real
object will be created in the free store. You will need to dispose
of it when you don't have the need for it any longer. Make sure
your 'Shape' class declares the destructor as 'virtual'.

[..]

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
.
User: "stefven blonqhern"

Title: Re: a pointer to a derived class problem 02 Aug 2007 12:53:09 PM
On 2 Aug, 13:32, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:

Don't store the object, it's unnecessary to duplicate it. The real
object will be created in the free store. You will need to dispose
of it when you don't have the need for it any longer. Make sure
your 'Shape' class declares the destructor as 'virtual'.

Once i reassign the pointer will unused objects not get garbage
collected? I'd prefer to err on the side of caution anyway but just
curious.
Stephen
.
User: "=?ISO-8859-1?Q?Erik_Wikstr=F6m?="

Title: Re: a pointer to a derived class problem 02 Aug 2007 04:11:18 PM
On 2007-08-02 19:53, stefven blonqhern wrote:

On 2 Aug, 13:32, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:

Don't store the object, it's unnecessary to duplicate it. The real
object will be created in the free store. You will need to dispose
of it when you don't have the need for it any longer. Make sure
your 'Shape' class declares the destructor as 'virtual'.


Once i reassign the pointer will unused objects not get garbage
collected? I'd prefer to err on the side of caution anyway but just
curious.

Standard C++ does not come with a garbage collector, you must free all
memory allocated with new manually (using delete). There are garbage
collectors available for C++ if you want one, but I'd recommend to not
use one while learning (if nothing else to learn ways to avoid needing one).
--
Erik Wikström
.
User: "stefven blonqhern"

Title: Re: a pointer to a derived class problem 02 Aug 2007 07:40:08 PM
On 2 Aug, 22:11, Erik Wikstr=F6m <Erik-wikst...@telia.com> wrote:

On 2007-08-02 19:53, stefven blonqhern wrote:

On 2 Aug, 13:32, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:


Don't store the object, it's unnecessary to duplicate it. The real
object will be created in the free store. You will need to dispose
of it when you don't have the need for it any longer. Make sure
your 'Shape' class declares the destructor as 'virtual'.


Once i reassign the pointer will unused objects not get garbage
collected? I'd prefer to err on the side of caution anyway but just
curious.


Standard C++ does not come with a garbage collector, you must free all
memory allocated with new manually (using delete). There are garbage
collectors available for C++ if you want one, but I'd recommend to not
use one while learning (if nothing else to learn ways to avoid needing on=

e).


--
Erik Wikstr=F6m

"if a garbage collector is present, the deletes can be omitted in most
cases" Stroustrup
okay, i'm quite surprised that standard c++ doesn't have a garbage
collector. i'm with you though on the idea of not using one even if
just to learn why? this C++ gets better every day.
Stephen.
.
User: "James Kanze"

Title: Re: a pointer to a derived class problem 03 Aug 2007 06:35:28 AM
On Aug 3, 2:40 am, stefven blonqhern <robo_cree...@yahoo.co.uk> wrote:

On 2 Aug, 22:11, Erik Wikstr=F6m <Erik-wikst...@telia.com> wrote:

On 2007-08-02 19:53, stefven blonqhern wrote:

On 2 Aug, 13:32, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:

Don't store the object, it's unnecessary to duplicate it.
The real object will be created in the free store. You
will need to dispose of it when you don't have the need
for it any longer. Make sure your 'Shape' class declares
the destructor as 'virtual'.

Once i reassign the pointer will unused objects not get garbage
collected? I'd prefer to err on the side of caution anyway but just
curious.

Standard C++ does not come with a garbage collector, you
must free all memory allocated with new manually (using
delete). There are garbage collectors available for C++ if
you want one, but I'd recommend to not use one while
learning (if nothing else to learn ways to avoid needing
one).

"if a garbage collector is present, the deletes can be omitted
in most cases" Stroustrup
okay, i'm quite surprised that standard c++ doesn't have a garbage
collector.

History. When Stroustrup was developing C++, garbage collection
technology wasn't fully mature, so he designed the language not
to depend on it. Today, of course, the situation is different,
but it takes considerable time and effort to get those sort of
things through the standard's committee.

i'm with you though on the idea of not using one even if
just to learn why?

As a general rule: if you can't write correct code without
garbage collection, you won't be able to write it with. There
is no silver bullet, and garbage collection doesn't free you
from the design issues concerning lifetime of object. On the
other hand, if you know how to write correct code without
garbage collection, you'll also know how to write it with
garbage collection, and using garbage collection will mean less
work for you.
Perhaps the best analogy I can come up with: if you don't know
the difference between dirty dishes and clean ones, a dishwasher
won't teach it to you. But if you do, using a dishwasher is a
hell of a lot less work than washing dishes by hand.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34
.



User: "James Kanze"

Title: Re: a pointer to a derived class problem 03 Aug 2007 06:28:46 AM
On Aug 2, 7:53 pm, stefven blonqhern <robo_cree...@yahoo.co.uk> wrote:

On 2 Aug, 13:32, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:

Don't store the object, it's unnecessary to duplicate it. The real
object will be created in the free store. You will need to dispose
of it when you don't have the need for it any longer. Make sure
your 'Shape' class declares the destructor as 'virtual'.

Once i reassign the pointer will unused objects not get garbage
collected? I'd prefer to err on the side of caution anyway but just
curious.

Only if you've installed and activated garbage collection. It's
not active by default, and it's only available as a third party
add-on with many compilers. (Try searching for the Boehm
collector on the web. That's the one I use: it works well, and
isn't too difficult to install.)
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34
.

User: "BobR"

Title: Re: a pointer to a derived class problem 02 Aug 2007 02:36:19 PM
stefven blonqhern <robo_creeler@yahoo.co.uk> wrote in message...

On 2 Aug, 13:32, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:

Don't store the object, it's unnecessary to duplicate it. The real
object will be created in the free store. You will need to dispose
of it when you don't have the need for it any longer. Make sure
your 'Shape' class declares the destructor as 'virtual'.


Once i reassign the pointer will unused objects not get garbage
collected?

Don't know. Show your definition of 'garbage collected'.

I'd prefer to err on the side of caution anyway but just
curious.
Stephen

// includes here
class Shape{ public:
virtual ~Shape(){} // required for base
};
class Circle : public Shape{ public:
Circle(){}
};
{ // main() or?
using std::cout; // for NG post
Shape *dummy( 0 );
std::vector<Shape*> vecShapes( 10, dummy );
vecShapes.at( 2 ) = new Circle();
// .... use vecShapes ....
for( size_t i(0); i < vecShapes.size(); ++i){
if( vecShapes.at( i ) ){
cout<<"deleting vecShapes.at("<<i<<")"<<std::endl;
delete vecShapes.at( i );
}
} // for(i)
}
// out: deleting vecShapes.at(2)
--
Bob R
POVrookie
.


User: "stefven blonqhern"

Title: Re: a pointer to a derived class problem 02 Aug 2007 07:54:10 AM
that works well. (respect to Victor Bazarov). this is what i was
attempting to do yesterday but i made a big hash of it.
thanks, Stephen.
On 2 Aug, 13:32, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:

stefven blonqhern wrote:

[..]
so what i'd need is something like:


Square shSqr;
Circle shCir;
Triangle shTri;


in the MyClass definition. then i can just assign shPtr to the
appropriate object after i create it in ChangeShape. this feels a
little clumsy though and wasteful since i will be creating many
objects that potentially won't get used.


No, a better idea is to create your 'Square' or 'Circle' or whatever
*dynamically*, and only store the pointer to it:

shPtr = new Square; // for example

Don't store the object, it's unnecessary to duplicate it. The real
object will be created in the free store. You will need to dispose
of it when you don't have the need for it any longer. Make sure
your 'Shape' class declares the destructor as 'virtual'.

[..]


V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

.





  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER