468,735 Members | 2,251 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,735 developers. It's quick & easy.

virtual functions & container interaction

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

Apr 5 '07 #1
2 1767
babaco wrote:
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);
}

Apr 5 '07 #2
On Apr 4, 6:24 pm, Mark P <use...@fall2005REMOVE.fastmailCAPS.fm>
wrote:
>
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.

Apr 5 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

16 posts views Thread by Geiregat Jonas | last post: by
9 posts views Thread by richard.forrest1 | last post: by
15 posts views Thread by christopher diggins | last post: by
5 posts views Thread by richard pickworth | last post: by
7 posts views Thread by Markus Svilans | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.