Connecting Tech Pros Worldwide Forums | Help | Site Map

silly question about inheritance

Gernot Frisch
Guest
 
Posts: n/a
#1: Jul 22 '05
Hi,

assume this situation:

class primitive
{
virtual void IsPointInside() =NULL;
};
class sphere : public primitive
{ ... };
class tetrahedron : public primitive
{ ... };

std::vector<sphere > v_sphere;
std::vector<tetrahedron> v_tetrahedron;

// Can I do this:

std::vector<primitive*> v_pPrimitives;
for(i=v_sphere.begin; i!=v_sphere.end(); ++i)
v_pPrimitives.push_back(&*i);
....

// and call the v_pPrimitives[i].IsPointInside(); ??

<head scratch>

I can't store different types in a single array, but I can store their
pointers, no!?

--
-Gernot
int main(int argc, char** argv) {printf
("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);}

________________________________________
Looking for a good game? Do it yourself!
GLBasic - you can do
www.GLBasic.com





John Harrison
Guest
 
Posts: n/a
#2: Jul 22 '05

re: silly question about inheritance



"Gernot Frisch" <Me@Privacy.net> wrote in message
news:2uuq3aF2esu27U1@uni-berlin.de...[color=blue]
> Hi,
>
> assume this situation:
>
> class primitive
> {
> virtual void IsPointInside() =NULL;[/color]

NULL isn't guaranteed to be 0, so

virtual void IsPointInside() = 0 ;
[color=blue]
> };
> class sphere : public primitive
> { ... };
> class tetrahedron : public primitive
> { ... };
>
> std::vector<sphere > v_sphere;
> std::vector<tetrahedron> v_tetrahedron;
>
> // Can I do this:
>
> std::vector<primitive*> v_pPrimitives;
> for(i=v_sphere.begin; i!=v_sphere.end(); ++i)
> v_pPrimitives.push_back(&*i);
> ...
>
> // and call the v_pPrimitives[i].IsPointInside(); ??[/color]

v_pPrimitives[i]->IsPointInside();
[color=blue]
>
> <head scratch>
>
> I can't store different types in a single array, but I can store their
> pointers, no!?
>[/color]

Well I would say that all you are storing is primitive*, but since those
primitive* will be pointing to objects whose type derives from primitive you
can use the usual virtual function mechanism.

It's really no different to this

primitive* p = new sphere();
p->IsPointInside();

john


Sharad Kala
Guest
 
Posts: n/a
#3: Jul 22 '05

re: silly question about inheritance



"Gernot Frisch" <Me@Privacy.net> wrote in message[color=blue]
> Hi,
>
> assume this situation:
>
> class primitive
> {
> virtual void IsPointInside() =NULL;
> };
> class sphere : public primitive
> { ... };
> class tetrahedron : public primitive
> { ... };
>
> std::vector<sphere > v_sphere;
> std::vector<tetrahedron> v_tetrahedron;
>
> // Can I do this:
>
> std::vector<primitive*> v_pPrimitives;
> for(i=v_sphere.begin; i!=v_sphere.end(); ++i)[/color]

i=v_sphere.begin()
[color=blue]
> v_pPrimitives.push_back(&*i);[/color]
...[color=blue]
>
> // and call the v_pPrimitives[i].IsPointInside(); ??[/color]

v_pPrimitives[i]->IsPointInside()

I don't see any problem now.

Sharad


Gernot Frisch
Guest
 
Posts: n/a
#4: Jul 22 '05

re: silly question about inheritance


[color=blue]
> It's really no different to this
>
> primitive* p = new sphere();
> p->IsPointInside();[/color]

One second... Then, if I have a
std::vector<primitive> prims;

can I push_back a sphere, which will keep all the additional sphere
specific information?

I get to thinking I never really understood about inheritances...


John Harrison
Guest
 
Posts: n/a
#5: Jul 22 '05

re: silly question about inheritance



"Gernot Frisch" <Me@Privacy.net> wrote in message
news:2uuqovF2f7b16U1@uni-berlin.de...[color=blue]
>[color=green]
> > It's really no different to this
> >
> > primitive* p = new sphere();
> > p->IsPointInside();[/color]
>
> One second... Then, if I have a
> std::vector<primitive> prims;
>
> can I push_back a sphere, which will keep all the additional sphere
> specific information?[/color]

No that is called object slicing, any additional information is lost. In
this case it wouldn't even compile because you have a pure virtual function
in primitive.

john


Gernot Frisch
Guest
 
Posts: n/a
#6: Jul 22 '05

re: silly question about inheritance



"John Harrison" <john_andronicus@hotmail.com> schrieb im Newsbeitrag
news:2uur3aF2ecc5hU1@uni-berlin.de...[color=blue]
>
> "Gernot Frisch" <Me@Privacy.net> wrote in message
> news:2uuqovF2f7b16U1@uni-berlin.de...[color=green]
>>[color=darkred]
>> > It's really no different to this
>> >
>> > primitive* p = new sphere();
>> > p->IsPointInside();[/color]
>>
>> One second... Then, if I have a
>> std::vector<primitive> prims;
>>
>> can I push_back a sphere, which will keep all the additional sphere
>> specific information?[/color]
>
> No that is called object slicing, any additional information is
> lost. In
> this case it wouldn't even compile because you have a pure virtual
> function
> in primitive.
>
> john[/color]

So, let me put this together. If I write a wrapper that has a vector
of pointers, I can store all different types of objects and use their
common methods regardless of what type they are, right?


John Harrison
Guest
 
Posts: n/a
#7: Jul 22 '05

re: silly question about inheritance



"Gernot Frisch" <Me@Privacy.net> wrote in message
news:2uurl3F2fpdjoU1@uni-berlin.de...[color=blue]
>
> "John Harrison" <john_andronicus@hotmail.com> schrieb im Newsbeitrag
> news:2uur3aF2ecc5hU1@uni-berlin.de...[color=green]
> >
> > "Gernot Frisch" <Me@Privacy.net> wrote in message
> > news:2uuqovF2f7b16U1@uni-berlin.de...[color=darkred]
> >>
> >> > It's really no different to this
> >> >
> >> > primitive* p = new sphere();
> >> > p->IsPointInside();
> >>
> >> One second... Then, if I have a
> >> std::vector<primitive> prims;
> >>
> >> can I push_back a sphere, which will keep all the additional sphere
> >> specific information?[/color]
> >
> > No that is called object slicing, any additional information is
> > lost. In
> > this case it wouldn't even compile because you have a pure virtual
> > function
> > in primitive.
> >
> > john[/color]
>
> So, let me put this together. If I write a wrapper that has a vector
> of pointers, I can store all different types of objects and use their
> common methods regardless of what type they are, right?
>[/color]

Only if those methods are virtual but otherwise yes.

john


Sharad Kala
Guest
 
Posts: n/a
#8: Jul 22 '05

re: silly question about inheritance



"John Harrison" <john_andronicus@hotmail.com> wrote in message
[color=blue]
> NULL isn't guaranteed to be 0, so[/color]

I do agree that NULL is probably not the right way to declare the pure
virtual function. But I thought there was a guarantee in C++ that NULL
always evaluates to 0.

18.1/4 says -
"The macro NULL is an implementation-defined C++ null pointer constant in
this International Standard". This line refers to footnote 180 which reads -
"Possible definitions include 0 and 0L, but not (void*)0."

4.10/1 reads - "A null pointer constant is an integral constant expression
rvalue of integer type that evaluates to zero."
^^^^^^^^^^^^

btw, Comeau online rejects and VC8 accepts following code -
#define NULL 0L
class primitive
{
virtual void IsPointInside() = NULL;
};
int main(){}

I think probably Comeau is correct.

Sharad


Gernot Frisch
Guest
 
Posts: n/a
#9: Jul 22 '05

re: silly question about inheritance



"John Harrison" <john_andronicus@hotmail.com> schrieb im Newsbeitrag
news:2uurnaF2etjrmU1@uni-berlin.de...[color=blue]
>
> "Gernot Frisch" <Me@Privacy.net> wrote in message
> news:2uurl3F2fpdjoU1@uni-berlin.de...[color=green]
>>
>> "John Harrison" <john_andronicus@hotmail.com> schrieb im
>> Newsbeitrag
>> news:2uur3aF2ecc5hU1@uni-berlin.de...[color=darkred]
>> >
>> > "Gernot Frisch" <Me@Privacy.net> wrote in message
>> > news:2uuqovF2f7b16U1@uni-berlin.de...
>> >>
>> >> > It's really no different to this
>> >> >
>> >> > primitive* p = new sphere();
>> >> > p->IsPointInside();
>> >>
>> >> One second... Then, if I have a
>> >> std::vector<primitive> prims;
>> >>
>> >> can I push_back a sphere, which will keep all the additional
>> >> sphere
>> >> specific information?
>> >
>> > No that is called object slicing, any additional information is
>> > lost. In
>> > this case it wouldn't even compile because you have a pure
>> > virtual
>> > function
>> > in primitive.
>> >
>> > john[/color]
>>
>> So, let me put this together. If I write a wrapper that has a
>> vector
>> of pointers, I can store all different types of objects and use
>> their
>> common methods regardless of what type they are, right?
>>[/color]
>
> Only if those methods are virtual but otherwise yes.[/color]

Yes, I know. If they are not (pure?) virtual, the base classes method
would be called (since it's a pointer to a base-type). Wow, thank you
so much. Now I can store different object types in a vector. I have to
provide a TypeOf function as well and then can use all common
functions on them. Really great!

Bye,
Gernot


John Harrison
Guest
 
Posts: n/a
#10: Jul 22 '05

re: silly question about inheritance



"Sharad Kala" <no_spam.sharadk_ind@yahoo.com> wrote in message
news:2uurqkF2fmkd4U1@uni-berlin.de...[color=blue]
>
> "John Harrison" <john_andronicus@hotmail.com> wrote in message
>[color=green]
> > NULL isn't guaranteed to be 0, so[/color]
>
> I do agree that NULL is probably not the right way to declare the pure
> virtual function. But I thought there was a guarantee in C++ that NULL
> always evaluates to 0.
>
> 18.1/4 says -
> "The macro NULL is an implementation-defined C++ null pointer constant in
> this International Standard". This line refers to footnote 180 which[/color]
reads -[color=blue]
> "Possible definitions include 0 and 0L, but not (void*)0."
>
> 4.10/1 reads - "A null pointer constant is an integral constant expression
> rvalue of integer type that evaluates to zero."
> ^^^^^^^^^^^^[/color]

#define NULL (1 - 1)

I think that is a legal definition of NULL, but I doubt that

virtual void func() = NULL;

would compile. I'm assuming that the syntax of a pure virtual requires the
integer literal 0, not an expression that evaluates to zero.

john


Andrew Koenig
Guest
 
Posts: n/a
#11: Jul 22 '05

re: silly question about inheritance


"Gernot Frisch" <Me@Privacy.net> wrote in message
news:2uuq3aF2esu27U1@uni-berlin.de...[color=blue]
> Hi,
>
> assume this situation:
>
> class primitive
> {
> virtual void IsPointInside() =NULL;
> };
> class sphere : public primitive
> { ... };
> class tetrahedron : public primitive
> { ... };
>
> std::vector<sphere > v_sphere;
> std::vector<tetrahedron> v_tetrahedron;
>
> // Can I do this:
>
> std::vector<primitive*> v_pPrimitives;
> for(i=v_sphere.begin; i!=v_sphere.end(); ++i)
> v_pPrimitives.push_back(&*i);
> ...
>
> // and call the v_pPrimitives[i].IsPointInside(); ??[/color]

Almost. v_pPrimitives is a vector of pointers, so you have to say

(*v_pPrimitives[i]).IsPointInside();

or, equivalently,

v_pPrimitives[i]->IsPointInside();

Note that changing the number of elements in v_sphere or v_tetrahedron might
reallocate the objects in those vectors, which would invalidate the pointers
in v_pPrimitives. If you want to use a technique such as this one, you
might be better off using std::deque, which does not move elements after
allocating them as long as new elements are allocated or deleted only at
either end of the deque.


Sharad Kala
Guest
 
Posts: n/a
#12: Jul 22 '05

re: silly question about inheritance



"John Harrison" <john_andronicus@hotmail.com> wrote in message
news:2uusa9F2fgmjiU1@uni-berlin.de...[color=blue]
>
> #define NULL (1 - 1)
>
> I think that is a legal definition of NULL, but I doubt that
>
> virtual void func() = NULL;
>
> would compile. I'm assuming that the syntax of a pure virtual requires the
> integer literal 0, not an expression that evaluates to zero.[/color]

I share your thoughts but VC8 doesn't complain about this code too -

#define NULL (1 - 1)
class primitive
{
virtual void IsPointInside() = NULL;
};

int main(){}

Comeau does crib though.

Sharad


JKop
Guest
 
Posts: n/a
#13: Jul 22 '05

re: silly question about inheritance


[color=blue]
> I'm assuming that the syntax of a pure virtual requires
> the integer literal 0, not an expression that evaluates to zero.[/color]


I myself don't even think of the "= 0" as having any association
whatsoever with the assignment operator, nor with the integral literal
which is zero. "= 0" is just a funky little yoke you stick on the end of
the function declaration to specify that it's "pure".

I myself never write NULL anywhere in my code, I *always* use 0 in its
place; that's another reason why I wouldn't use it as the OP did.


-JKop
Gernot Frisch
Guest
 
Posts: n/a
#14: Jul 22 '05

re: silly question about inheritance



"JKop" <NULL@NULL.NULL> schrieb im Newsbeitrag
news:qcrid.40880$Z14.15752@news.indigo.ie...[color=blue]
>[color=green]
>> I'm assuming that the syntax of a pure virtual requires
>> the integer literal 0, not an expression that evaluates to zero.[/color]
>
>
> I myself don't even think of the "= 0" as having any association
> whatsoever with the assignment operator, nor with the integral
> literal
> which is zero. "= 0" is just a funky little yoke you stick on the
> end of
> the function declaration to specify that it's "pure".
>
> I myself never write NULL anywhere in my code, I *always* use 0 in
> its
> place; that's another reason why I wouldn't use it as the OP did.[/color]

I always use NULL where I set a pointer to 0L. It's bacause my IDE
highlights it in green and I like green... ;)


Closed Thread