473,387 Members | 1,303 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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
Jul 22 '05 #1
13 1198

"Gernot Frisch" <Me@Privacy.net> wrote in message
news:2u*************@uni-berlin.de...
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[i].IsPointInside(); ??
v_pPrimitives[i]->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
Jul 22 '05 #2

"Gernot Frisch" <Me@Privacy.net> wrote in message
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[i].IsPointInside(); ??


v_pPrimitives[i]->IsPointInside()

I don't see any problem now.

Sharad
Jul 22 '05 #3
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...
Jul 22 '05 #4

"Gernot Frisch" <Me@Privacy.net> wrote in message
news:2u*************@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
Jul 22 '05 #5

"John Harrison" <jo*************@hotmail.com> schrieb im Newsbeitrag
news:2u*************@uni-berlin.de...

"Gernot Frisch" <Me@Privacy.net> wrote in message
news:2u*************@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


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?
Jul 22 '05 #6

"Gernot Frisch" <Me@Privacy.net> wrote in message
news:2u*************@uni-berlin.de...

"John Harrison" <jo*************@hotmail.com> schrieb im Newsbeitrag
news:2u*************@uni-berlin.de...

"Gernot Frisch" <Me@Privacy.net> wrote in message
news:2u*************@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


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
Jul 22 '05 #7

"John Harrison" <jo*************@hotmail.com> wrote in message
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
Jul 22 '05 #8

"John Harrison" <jo*************@hotmail.com> schrieb im Newsbeitrag
news:2u*************@uni-berlin.de...

"Gernot Frisch" <Me@Privacy.net> wrote in message
news:2u*************@uni-berlin.de...

"John Harrison" <jo*************@hotmail.com> schrieb im
Newsbeitrag
news:2u*************@uni-berlin.de...
>
> "Gernot Frisch" <Me@Privacy.net> wrote in message
> news:2u*************@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


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.


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
Jul 22 '05 #9

"Sharad Kala" <no*****************@yahoo.com> wrote in message
news:2u*************@uni-berlin.de...

"John Harrison" <jo*************@hotmail.com> wrote in message
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."
^^^^^^^^^^^^


#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
Jul 22 '05 #10
"Gernot Frisch" <Me@Privacy.net> wrote in message
news:2u*************@uni-berlin.de...
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(); ??


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.
Jul 22 '05 #11

"John Harrison" <jo*************@hotmail.com> wrote in message
news:2u*************@uni-berlin.de...

#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
Jul 22 '05 #12
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
Jul 22 '05 #13

"JKop" <NU**@NULL.NULL> schrieb im Newsbeitrag
news:qc*******************@news.indigo.ie...
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.


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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Stephan Diehl | last post by:
I have a question about metaclasses: How would be the best way to "merge" different metaclasses? Or, more precisely, what is the best way to merge metaclass functionality? The idea is basicly...
17
by: Andrew Koenig | last post by:
Suppose I want to define a class hierarchy that represents expressions, for use in a compiler or something similar. We might imagine various kinds of expressions, classified by their top-level...
2
by: Tony Johansson | last post by:
Hello Experts!! Here we use multiple inheritance from two classes.We have a class named Person at the very top and below this class we have a Student class and an Employee class at the same...
1
by: Tony Johansson | last post by:
Hello Experts! I have some questions about inheritance that I want to have an answer to. It says "Abstract superclasses define a behavioral pattern without specifying the implementation" I...
3
by: Quentin Huo | last post by:
Hi: C# doesn't support multiple inheritance but it supports interface.But I think these ways are different. For example, C++ supports multiple inheritance. I have two classess classA and...
18
by: Tom Cole | last post by:
I'm working on a small Ajax request library to simplify some tasks that I will be taking on shortly. For the most part everything works fine, however I seem to have some issues when running two...
14
by: JoeC | last post by:
I have been writing games and I also read about good programming techniques. I tend to create large objects that do lots of things. A good example I have is a unit object. The object controls...
3
by: Jess | last post by:
Hello, I've been reading Effective C++ about multiple inheritance, but I still have a few questions. Can someone give me some help please? First, it is said that if virtual inheritance is...
10
by: r035198x | last post by:
There are a lot of common C mistakes. I have put together the ones I consider to be the silliest made by C programmers. These are usually the source of the popular C programming tears and remembering...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.