473,322 Members | 1,287 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,322 software developers and data experts.

Array of classes as a function parameter...polymorphism question.

I have a class called "Base". This class has a protected member
variable "m_base" which can be retrieved using the public member
function "GetBaseMember". "m_base" is initialized to "1" and is never
changed.

I have another class which is a subclass of the "Base" class called
"Derived". This derived class has a member variable called
"m_derived". "m_derived" is initialized to "2" and is never changed.

I pass an array of "Base" classes as a parameter to a function.The
individual items in this array may or may not be subclasses of the
"Base" class. Within this function, the member function
"GetBaseMember" is called, and the value of "m_base" is displayed to
the screen.

Intuitively, the output should always be "1", but unfortunately it's
not. The output alternates between "1" and "2". I don't understand why
this is. How else can I pass this "subclassable" class array to a
function and retrieve the expected base class member variable? What
concept am I missing here?

#include <iostream>
using namespace std;

class Base
{
protected:
int m_base;

public:
Base() : m_base( 1 ){}
int GetBaseMember() { return m_base; }
};

class Derived : public Base
{
protected:
int m_derived;

public:
Derived() : Base(), m_derived( 2 ){}
};

void Foo( int cItems, Base b[] )
{
int i = 0;
for ( i = 0; i < cItems; i++ )
{
// I want the output to always be 1,
// but it alternates between 1 and 2
cout << "m_base = " << b[i].GetBaseMember() << endl;
}
}

int main()
{
const int NUM_ITEMS = 4;
Derived d[NUM_ITEMS];

Foo( NUM_ITEMS, d );

return 0;
}
Oct 6 '06 #1
13 1973
Jack wrote:
I have a class called "Base". This class has a protected member
variable "m_base" which can be retrieved using the public member
function "GetBaseMember". "m_base" is initialized to "1" and is never
changed.

I have another class which is a subclass of the "Base" class called
"Derived". This derived class has a member variable called
"m_derived". "m_derived" is initialized to "2" and is never changed.

I pass an array of "Base" classes as a parameter to a function.
Actually, you don't. Your function *expects* an array of Base objects.
You *pass* it an array of *Derived*s.
The
individual items in this array may or may not be subclasses of the
"Base" class.
If it's an array of 'Base', items in it are *always* objects of 'Base'.
None of them can be subobjects of anything. They are all stand-alone
objects, essentially.
Within this function, the member function
"GetBaseMember" is called, and the value of "m_base" is displayed to
the screen.

Intuitively, the output should always be "1", but unfortunately it's
not.
The behaviour of your program is undefined. You pass an array of
Derived objects where an array of Base is expected. There is no
conversion between the two. And since you declare your function as
receiving a *pointer* to 'Base', the compiler does not complain.
The undefined behaviour occurs when you index that pointer with any
expression other than 0.
The output alternates between "1" and "2". I don't understand why
this is.
The reason is immaterial. The behaviour is undefined; anything may
happen.
How else can I pass this "subclassable" class array to a
function and retrieve the expected base class member variable? What
concept am I missing here?
You want to use polymorphism. Probably compile-time one, through
templates. Your 'Foo' function should be defined as

template<class BDvoid Foo(int cItems, BD b[])
{
// keep inside just like you have it.
}
>
#include <iostream>
using namespace std;

class Base
{
protected:
int m_base;

public:
Base() : m_base( 1 ){}
int GetBaseMember() { return m_base; }
};

class Derived : public Base
{
protected:
int m_derived;

public:
Derived() : Base(), m_derived( 2 ){}
};

void Foo( int cItems, Base b[] )
{
int i = 0;
for ( i = 0; i < cItems; i++ )
{
// I want the output to always be 1,
// but it alternates between 1 and 2
cout << "m_base = " << b[i].GetBaseMember() << endl;
}
}

int main()
{
const int NUM_ITEMS = 4;
Derived d[NUM_ITEMS];

Foo( NUM_ITEMS, d );

return 0;
}
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 6 '06 #2
Victor Bazarov wrote:
Jack wrote:
>How else can I pass this "subclassable" class array to a
function and retrieve the expected base class member variable? What
concept am I missing here?

You want to use polymorphism. Probably compile-time one, through
templates. Your 'Foo' function should be defined as

template<class BDvoid Foo(int cItems, BD b[])
{
// keep inside just like you have it.
}
Even better would be:

template <typename It>
void Foo(It begin,It end)
{
for (It i = begin; i != end; ++i)
cout << "m_base = " << i->GetBaseMember() << endl;
}

int main()
{
const int NUM_ITEMS = 4;
Derived d[NUM_ITEMS];

Foo(d,d + NUM_ITEMS);

return 0;
}

This allows the use of STL containers in addition to arrays, or
subranges of either.
Oct 6 '06 #3
Jack wrote:
I have a class called "Base". This class has a protected member
variable "m_base" which can be retrieved using the public member
function "GetBaseMember". "m_base" is initialized to "1" and is never
changed.
[snip]
class Base
{
protected:
int m_base;

public:
Base() : m_base( 1 ){}
int GetBaseMember() { return m_base; }
};
I know this is just an example, but if m_base never changes, why not
declare it const? Same goes for GetBaseMember.

Nate
Oct 6 '06 #4
Nate Barney wrote:
Victor Bazarov wrote:
>Jack wrote:
>>How else can I pass this "subclassable" class array to a
function and retrieve the expected base class member variable? What
concept am I missing here?

You want to use polymorphism. Probably compile-time one, through
templates. Your 'Foo' function should be defined as

template<class BDvoid Foo(int cItems, BD b[])
{
// keep inside just like you have it.
}

Even better would be:

template <typename It>
void Foo(It begin,It end)
{
for (It i = begin; i != end; ++i)
Generally speaking all you need is

while (begin != end)
cout << "m_base = " << i->GetBaseMember() << endl;
cout << "m_base = " << (*begin++).GetBaseMember() << endl;
}

int main()
{
const int NUM_ITEMS = 4;
Derived d[NUM_ITEMS];

Foo(d,d + NUM_ITEMS);

return 0;
}

This allows the use of STL containers in addition to arrays, or
subranges of either.
I agree, it's better. But a concept of an iterator is not something
a beginner would grasp easily, I'm afraid, especially if they are
taught about arrays.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 6 '06 #5
Victor Bazarov wrote:
Nate Barney wrote:
>template <typename It>
void Foo(It begin,It end)
{
for (It i = begin; i != end; ++i)

Generally speaking all you need is

while (begin != end)
> cout << "m_base = " << i->GetBaseMember() << endl;

cout << "m_base = " << (*begin++).GetBaseMember() << endl;
>}
That's cool. I never thought of that. I guess I'm used to writing such
functions this way:

template <typename It>
void Foo(const It &begin,const It &end)
{
}

in case the iterator is larger than a pointer, in which case the extra
counter variable would be required. Standard library iterators are
probably not, but custom iterators may well be.
I agree, it's better. But a concept of an iterator is not something
a beginner would grasp easily, I'm afraid, especially if they are
taught about arrays.
Makes sense. I just thought I'd provide this example in case the OP
could grasp the concept and had just not been exposed to it.

Nate
Oct 6 '06 #6
Nate Barney wrote:
Victor Bazarov wrote:
>Nate Barney wrote:
>>template <typename It>
void Foo(It begin,It end)
{
for (It i = begin; i != end; ++i)

Generally speaking all you need is

while (begin != end)
>> cout << "m_base = " << i->GetBaseMember() << endl;

cout << "m_base = " << (*begin++).GetBaseMember() << endl;
>>}

That's cool. I never thought of that. I guess I'm used to writing such
functions this way:

template <typename It>
void Foo(const It &begin,const It &end)
{
}

in case the iterator is larger than a pointer, in which case the extra
counter variable would be required. Standard library iterators are
probably not, but custom iterators may well be.
[snip]

Note that the standard algorithms take iterators by value. The STL is very
much designed under the assumption that copying iterators is cheap. It
would be unwise to design iterators the break this assumption. I usually
follow the precedent set by the standard library. Thus, when taking
iterators as arguments, I take them by value. In my opinion, making
iterators reasonably small is part of good iterator design.

Also, there is not really a need for big iterators: in tree based data
structures one can use back pointers so that iterators can just be node
pointers. When all else fail, one can implement the iterator as a little
wrapper around a shared_ptr (been there, done that: the tree structure
underlying a rope does not allow for back pointers). I have yet to learn of
a situation, where an iterator has to be big.
Best

Kai-Uwe Bux
Oct 6 '06 #7
On Thu, 5 Oct 2006 23:49:41 -0400, "Victor Bazarov"
<v.********@comAcast.netwrote:
>The behaviour of your program is undefined. You pass an array of
Derived objects where an array of Base is expected. There is no
conversion between the two.
I was under the assumption that C++ would support polymorphism for
arrays of subclassed objects, but I guess I was wrong. IMHO, my
algorithm being described as "undefined" is a flaw in the language. No
conversion? Isn't it obvious? I guess not for the compiler.
>You want to use polymorphism. Probably compile-time one, through
templates. Your 'Foo' function should be defined as

template<class BDvoid Foo(int cItems, BD b[])
{
// keep inside just like you have it.
}
Of course I want to use polymorphism! Is there another option besides
templates though? It just seems like overkill for an extremely simple
task. The following example works fine. Why does C++ use polymorphism
for this example and not for my original one?

void Foo( Base& b )
{
// This always gives the desired output.
cout << "m_base = " << b.GetBaseMember() << endl;
}

int main()
{
const int NUM_ITEMS = 4;
Derived d[NUM_ITEMS];
int i;

for ( i = 0; i < NUM_ITEMS; i++ )
{
Foo( d[i] );
}

return 0;
}
Oct 6 '06 #8
Jack wrote:
On Thu, 5 Oct 2006 23:49:41 -0400, "Victor Bazarov"
<v.********@comAcast.netwrote:

>>The behaviour of your program is undefined. You pass an array of
Derived objects where an array of Base is expected. There is no
conversion between the two.


I was under the assumption that C++ would support polymorphism for
arrays of subclassed objects, but I guess I was wrong. IMHO, my
algorithm being described as "undefined" is a flaw in the language. No
conversion? Isn't it obvious? I guess not for the compiler.
No it isn't.

If you define and array of some base class of size N and attempt to
store derived objects of size N+M in said array, what would you expect
happen?

Use an array of pointers to base.

--
Ian Collins.
Oct 6 '06 #9
Kai-Uwe Bux wrote:
Nate Barney wrote:
>template <typename It>
void Foo(const It &begin,const It &end)
{
}

in case the iterator is larger than a pointer, in which case the extra
counter variable would be required. Standard library iterators are
probably not, but custom iterators may well be.
[snip]

Note that the standard algorithms take iterators by value. The STL is very
much designed under the assumption that copying iterators is cheap. It
would be unwise to design iterators the break this assumption. I usually
follow the precedent set by the standard library. Thus, when taking
iterators as arguments, I take them by value. In my opinion, making
iterators reasonably small is part of good iterator design.
Point well taken. Thanks for that.

Nate
Oct 6 '06 #10
"Jack" <ja**@ss.comwrote in message news:j6********************************@4ax.com...
On Thu, 5 Oct 2006 23:49:41 -0400, "Victor Bazarov"
<v.********@comAcast.netwrote:
The behaviour of your program is undefined. You pass an array of
Derived objects where an array of Base is expected. There is no
conversion between the two.

I was under the assumption that C++ would support polymorphism for
arrays of subclassed objects, but I guess I was wrong. IMHO, my
algorithm being described as "undefined" is a flaw in the language.
Rather, it would be a flaw in the language if it supported that, given the cost in space and/or
execution time needed and the very limited use it would get. For one thing, at run-time the program
would have to check the type stored to know how many bytes to advance to reach the array element
addressed - a complete waste when the objects are of the declared type.

You can always use an array of Base*, where each Base * may point to a Base or a Derived object, and
each would behave polymorphically.

DW
Oct 6 '06 #11
On Fri, 06 Oct 2006 18:45:34 +1300, Ian Collins <ia******@hotmail.com>
wrote:
>Jack wrote:
>On Thu, 5 Oct 2006 23:49:41 -0400, "Victor Bazarov"
<v.********@comAcast.netwrote:

>>>The behaviour of your program is undefined. You pass an array of
Derived objects where an array of Base is expected. There is no
conversion between the two.


I was under the assumption that C++ would support polymorphism for
arrays of subclassed objects, but I guess I was wrong. IMHO, my
algorithm being described as "undefined" is a flaw in the language. No
conversion? Isn't it obvious? I guess not for the compiler.
No it isn't.

If you define and array of some base class of size N and attempt to
store derived objects of size N+M in said array, what would you expect
happen?

Use an array of pointers to base.
Very good explanation! It makes perfect sense now. An array of
pointers, of course! Thank you!
Oct 6 '06 #12
On Fri, 06 Oct 2006 00:04:30 -0400, Nate Barney <na********@gmail.com>
wrote:
>Jack wrote:
>I have a class called "Base". This class has a protected member
variable "m_base" which can be retrieved using the public member
function "GetBaseMember". "m_base" is initialized to "1" and is never
changed.

[snip]
>class Base
{
protected:
int m_base;

public:
Base() : m_base( 1 ){}
int GetBaseMember() { return m_base; }
};

I know this is just an example, but if m_base never changes, why not
declare it const? Same goes for GetBaseMember.

Nate
Sure, why not. LOL!

class Base
{
protected:
const int m_base;

public:
Base() : m_base( 1 ){}
const int GetBaseMember() { return m_base; }
};
Oct 6 '06 #13
Jack wrote:
On Fri, 06 Oct 2006 00:04:30 -0400, Nate Barney <na********@gmail.com>
wrote:
>Jack wrote:
>>class Base
{
protected:
int m_base;

public:
Base() : m_base( 1 ){}
int GetBaseMember() { return m_base; }
};
I know this is just an example, but if m_base never changes, why not
declare it const? Same goes for GetBaseMember.

Sure, why not. LOL!

class Base
{
protected:
const int m_base;

public:
Base() : m_base( 1 ){}
const int GetBaseMember() { return m_base; }
};
Actually, I meant:

int GetBaseMember() const { return m_base; }
Oct 9 '06 #14

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

Similar topics

9
by: justanotherguy63 | last post by:
Hi, I am designing an application where to preserve the hierachy and for code substitability, I need to pass an array of derived class object in place of an array of base class object. Since I...
24
by: Alf P. Steinbach | last post by:
The eighth chapter (chapter 2.1) of my attempted Correct C++ tutorial is now available, although for now only in Word format -- comments welcome! Use the free & system-independent Open Office...
4
by: mflll | last post by:
I am looking into the different techniques of handling arrays of edit boxes in Java Script. The first program below works fine. However, are there better ways of doing this, where the person...
7
by: Jim Carlock | last post by:
Looking for suggestions on how to handle bad words that might get passed in through $_GET variables. My first thoughts included using str_replace() to strip out such content, but then one ends...
7
by: heddy | last post by:
I have an array of objects. When I use Array.Resize<T>(ref Object,int Newsize); and the newsize is smaller then what the array was previously, are the resources allocated to the objects that are...
173
by: Zytan | last post by:
I've read the docs on this, but one thing was left unclear. It seems as though a Module does not have to be fully qualified. Is this the case? I have source that apparently shows this. Are...
2
by: cmonthenet | last post by:
Hello, I searched for an answer to my question and found similar posts, but none that quite addressed the issue I am trying to resolve. Essentially, it seems like I need something like a virtual...
26
by: aruna.mysore | last post by:
Hi all, I have a specific problem passing a function pointer array as a parameter to a function. I am trying to use a function which takes a function pointer array as an argument. I am too sure...
5
by: Fokko Beekhof | last post by:
Hello all, please consider the following code: -------------------------------------------------- #include <tr1/memory> struct BaseA { int x;
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.