silly question about inheritance | |
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 | | | | 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 | | | | 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 | | | | 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... | | | | 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 | | | | 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? | | | | 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 | | | | 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 | | | | 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 | | | | 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 | | | | 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. | | | | 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 | | | | 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 | | | | 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... ;) |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,418 network members.
|