silly question about inheritance

G

Gernot Frisch

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.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
 
J

John Harrison

Gernot Frisch said:
Hi,

assume this situation:

class primitive
{
virtual void IsPointInside() =NULL;

NULL isn't guaranteed to be 0, so

virtual void IsPointInside() = 0 ;
};
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.IsPointInside(); ??
v_pPrimitives->IsPointInside();


<head scratch>

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


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
 
S

Sharad Kala

Gernot Frisch said:
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)
i=v_sphere.begin()
v_pPrimitives.push_back(&*i);
...

// and call the v_pPrimitives.IsPointInside(); ??


v_pPrimitives->IsPointInside()

I don't see any problem now.

Sharad
 
G

Gernot Frisch

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?

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

John Harrison

Gernot Frisch said:
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
 
G

Gernot Frisch

John Harrison said:
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

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?
 
J

John Harrison

Gernot Frisch said:
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?

Only if those methods are virtual but otherwise yes.

john
 
S

Sharad Kala

John Harrison said:
NULL isn't guaranteed to be 0, so

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
 
G

Gernot Frisch

John Harrison said:
Only if those methods are virtual but otherwise yes.

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
 
J

John Harrison

Sharad Kala said:
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."
^^^^^^^^^^^^

#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
 
A

Andrew Koenig

Gernot Frisch said:
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.IsPointInside(); ??


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

(*v_pPrimitives).IsPointInside();

or, equivalently,

v_pPrimitives->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.
 
S

Sharad Kala

John Harrison said:
#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.

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
 
J

JKop

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


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
 
G

Gernot Frisch

JKop said:
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.

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top