Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ Moderated > Is it bad to ha...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 3 Topic 9351 of 10104
Post > Topic >>

Is it bad to have objects pass themselves to new objects?

by "k04jg02@[EMAIL PROTECTED] " <k04jg02@[EMAIL PROTECTED] > Mar 5, 2008 at 09:53 PM

My app has a bunch of different data types, and a bunch of different
graph types that can draw the data. This is a pretty much a 1:1
mapping -- one graph type for each data type. One way I've heard of
handling this situation is to have a factory. The factory would take
in the data and put out the appropriate graphs. I thought, "That seems
unnecessary, why not just have the data types know what graph type
they correspond to..." So something like this:

class Graph {};

class GraphA {
public:
     GraphA(dataA* data) : _data(data) {}
     void Render() { /* Do some stuff with data to make a pretty graph
*/ }
private:
     dataA* _data;
};

class data {
public:
     virtual Graph* BuildGraph() = 0;
};

class dataA : public data {
public:
     virtual Graph* BuildGraph() { return new GraphA(this); }
};

Now instead of plugging the data into a factory to make a graph you
just do data->BuildGraph() and it makes the graph. My question is: is
this a bad idea? Is there some reason why a factory is preferable?

One problem I noticed is that if later GraphA and dataA are made
templates, then their declaration and source both goes in their
headers, and you end up with circular #include's because dataA needs
to know about GraphA in order to instantiate it, and GraphA needs to
know about dataA in order to plot it. But maybe there are other perils?

-- 
      [ See http://www.gotw.ca/resources/clcm.htm
for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
 




 3 Posts in Topic:
Is it bad to have objects pass themselves to new objects?
"k04jg02@[EMAIL PROT  2008-03-05 21:53:39 
Re: Is it bad to have objects pass themselves to new objects?
Ulrich Eckhardt <dooms  2008-03-06 10:38:15 
Re: Is it bad to have objects pass themselves to new objects?
Carl Barron <cbarron41  2008-03-07 03:42:25 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Wed Oct 15 22:37:28 CDT 2008.