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

J

Jack

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.GetBaseMember() << endl;
}
}

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

Foo( NUM_ITEMS, d );

return 0;
}
 
V

Victor Bazarov

Jack said:
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 BD> void 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.GetBaseMember() << endl;
}
}

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

Foo( NUM_ITEMS, d );

return 0;
}


V
 
N

Nate Barney

Victor said:
Jack said:
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 BD> void 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.
 
N

Nate Barney

Jack said:
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
 
V

Victor Bazarov

Nate said:
Victor said:
Jack said:
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 BD> void 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
 
N

Nate Barney

Victor said:
Generally speaking all you need is

while (begin != end)


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
 
J

Jack

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 BD> void 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 );
}

return 0;
}
 
K

Kai-Uwe Bux

Nate said:
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
 
I

Ian Collins

Jack said:
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.
 
N

Nate Barney

Kai-Uwe Bux said:
Nate said:
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
 
D

David W

Jack said:
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
 
J

Jack

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

Jack

Jack said:
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; }
};
 
N

Nate Barney

Jack said:
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; }
 

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

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top