virtual functions & container interaction

B

babaco

Hi,

I've come upon a situation where I don't understand what's happening.
Basically, I'm implementing a kind of 'chain of responsibility' using
some covariant types:

#include <iostream>
#include <string>
#include <vector>
using namespace std;

class Shape {
public:
int sides;
};

class Square : public Shape {
public:
int sides;
Square() : sides(4) {};
};

class Triangle : public Shape {
public:
int sides;
Triangle() : sides(3) {};
};


class ShapeMaker {
public:
virtual Shape* CreateObject(void) const = 0;
};


class SquareMaker : public ShapeMaker {
public:
Square* CreateObject(void) const {
cout << "CreateObject: for SQUARE" << endl;
return (new Square);
}
};

class TriangleMaker : public ShapeMaker {
public:
Triangle* CreateObject(void) const {
cout << "CreateObject: for TRIANGLE" << endl;
return (new Triangle);
}
};

class ShapeContainer {
private:
vector<Square*> squares;
vector<Triangle*> triangles;
vector<Shape*> shapes;
public:
void enter(Square* s) { squares.push_back(s); }
void enter(Triangle* t) { triangles.push_back(t); }
void enter(Shape* s) { shapes.push_back(s); }

friend ostream& operator<<(ostream& os, ShapeContainer &c) {
return os << "Container has \t" << c.squares.size() << " squares
\n"
<< " \t" << c.triangles.size() << "
triangles\n"
<< " \t" << c.shapes.size() << " generic shapes
\n";
}

};

int main ()
{

SquareMaker* sfactory = new SquareMaker;
TriangleMaker* tfactory = new TriangleMaker;

cout << "\nFirst Set of Tests:" << endl;
ShapeContainer container1;
container1.enter(sfactory->CreateObject());
container1.enter(tfactory->CreateObject());
cout << container1;

cout << "\n\nSecond Set of Tests:" << endl;
vector<ShapeMaker*> master_factory;
master_factory.push_back(sfactory);
master_factory.push_back(tfactory);
ShapeContainer container2;

vector<ShapeMaker*>::iterator it = master_factory.begin();
while(it != master_factory.end()) {
// this calls the correct derived type function
// but returns an object of the base class. huh?
container2.enter( (*it)->CreateObject() );
++it;
}

cout << container2;
}

This compiles under gcc 4.0.1 (apple, x86). When I run it I get this:

First Set of Tests:
CreateObject: for SQUARE
CreateObject: for TRIANGLE
Container has 1 squares
1 triangles
0 generic shapes


Second Set of Tests:
CreateObject: for SQUARE
CreateObject: for TRIANGLE
Container has 0 squares
0 triangles
2 generic shapes

My question is why does (*it)->CreateObject() call the correct derived
class type, but result in a base class return type? Please be gentle;
I've been writing Perl code for years now and I'm lazy and used to
dynamic bindings.

TIA
 
M

Mark P

babaco said:
Hi,

I've come upon a situation where I don't understand what's happening.
Basically, I'm implementing a kind of 'chain of responsibility' using
some covariant types:

#include <iostream>
#include <string>
#include <vector>
using namespace std;

class Shape {
public:
int sides;
};

class Square : public Shape {
public:
int sides;
Square() : sides(4) {};
};

class Triangle : public Shape {
public:
int sides;
Triangle() : sides(3) {};
};

Why do you give Square and Triangle a member "sides" when this is
already included in Shape? Probably you mean to declare this in Shape only.
class ShapeMaker {
public:
virtual Shape* CreateObject(void) const = 0;
};


class SquareMaker : public ShapeMaker {
public:
Square* CreateObject(void) const {
cout << "CreateObject: for SQUARE" << endl;
return (new Square);
}
};

class TriangleMaker : public ShapeMaker {
public:
Triangle* CreateObject(void) const {
cout << "CreateObject: for TRIANGLE" << endl;
return (new Triangle);
}
};

class ShapeContainer {
private:
vector<Square*> squares;
vector<Triangle*> triangles;
vector<Shape*> shapes;
public:
void enter(Square* s) { squares.push_back(s); }
void enter(Triangle* t) { triangles.push_back(t); }
void enter(Shape* s) { shapes.push_back(s); }

friend ostream& operator<<(ostream& os, ShapeContainer &c) {
return os << "Container has \t" << c.squares.size() << " squares
\n"
<< " \t" << c.triangles.size() << "
triangles\n"
<< " \t" << c.shapes.size() << " generic shapes
\n";
}

};

int main ()
{

SquareMaker* sfactory = new SquareMaker;
TriangleMaker* tfactory = new TriangleMaker;

cout << "\nFirst Set of Tests:" << endl;
ShapeContainer container1;
container1.enter(sfactory->CreateObject());
container1.enter(tfactory->CreateObject());
cout << container1;

cout << "\n\nSecond Set of Tests:" << endl;
vector<ShapeMaker*> master_factory;
master_factory.push_back(sfactory);
master_factory.push_back(tfactory);
ShapeContainer container2;

vector<ShapeMaker*>::iterator it = master_factory.begin();
while(it != master_factory.end()) {
// this calls the correct derived type function
// but returns an object of the base class. huh?
container2.enter( (*it)->CreateObject() );
++it;
}

cout << container2;
}

This compiles under gcc 4.0.1 (apple, x86). When I run it I get this:

First Set of Tests:
CreateObject: for SQUARE
CreateObject: for TRIANGLE
Container has 1 squares
1 triangles
0 generic shapes


Second Set of Tests:
CreateObject: for SQUARE
CreateObject: for TRIANGLE
Container has 0 squares
0 triangles
2 generic shapes

My question is why does (*it)->CreateObject() call the correct derived
class type, but result in a base class return type? Please be gentle;
I've been writing Perl code for years now and I'm lazy and used to
dynamic bindings.

The problem you're running into is that functions can be virtual with
respect to their calling object but not with respect to their arguments.
Hence (*it)->CreateObject() looks statically like
ShapeMaker::CreateObject() which resolves dynamically as you expect.
However, in container2.enter( (*it)->CreateObject() ), the enter
function is resolved at compile time based on the static type of
(*it)->CreateObject() which is a Shape* (since the static type of **it
is ShapeMaker).

There are of course tricks to make functions appear virtual with respect
to their arguments. One way to do this is define foo( Obj& o) as
o.doSomethingTo( foo) where doSomethingTo is a virtual function of class
Obj.

-Mark

// simple example...
// C::foo is "virtual" with respect to its argument

#include <iostream>

using namespace std;

struct C;

struct B
{
virtual void apply( C& c) = 0;
};

struct D1 : public B
{
virtual void apply( C& c)
{
cout << "case D1" << endl;
}
};

struct D2 : public B
{
virtual void apply( C& c)
{
cout << "case D2" << endl;
}
};

struct C
{
void foo( B& b)
{
b.apply( *this);
}
};

int main()
{
C c;
B* b1 = new D1;
B* b2 = new D2;
c.foo( *b1);
c.foo( *b2);
}
 
B

babaco

Why do you give Square and Triangle a member "sides" when this is
already included in Shape? Probably you mean to declare this in Shape only.

Yes, that's what I meant. I should have known I couldn't post code to
usenet without making a mistake ...
The problem you're running into is that functions can be virtual with
respect to their calling object but not with respect to their arguments.
Hence (*it)->CreateObject() looks statically like
ShapeMaker::CreateObject() which resolves dynamically as you expect.
However, in container2.enter( (*it)->CreateObject() ), the enter
function is resolved at compile time based on the static type of
(*it)->CreateObject() which is a Shape* (since the static type of **it
is ShapeMaker).

Thank you, that helps.
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top