LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Indirection in class member declaration (https://www.linuxquestions.org/questions/programming-9/indirection-in-class-member-declaration-481340/)

websinger 09-07-2006 02:01 PM

Indirection in class member declaration
 
Hi;
In the C++ code fragment below I would like to
declare plotter as being either if type
XPlotter or GIFPlotter depending on the value of "plot"

Code:

...
char *myplotter;
char *my_out;
if(!strcmp(plot,"X")){
  myplotter="XPlotter";
  my_out="cout";
}
else{
  myplotter="GIFPlotter";
  myout="somefile.gif";
}

myplotter plotter(cin,my_out,cerr, params); //<line 68>  declare 'plotter' here
...

...
this is what I get in compiling:
Quote:

g++ fractal.c++ -o fractal
fractal.c++: In function 'int main(int, char**)':
fractal.c++:68: error: expected `;' before 'plotter'
fractal.c++:68: warning: statement has no effect
I assume ( silly me ) that what I want here is some sort of indirection. but either I'm not setting up the pointers right
( not unlikely ) or I've badly misunderstood the syntax here.

Thanks for any assistance

Jeff

mjones490 09-07-2006 03:39 PM

What is the function signature of the function plotter? Also, you declare and define char* myplotter. What is the purpose of this variable?

ntubski 09-07-2006 03:52 PM

Quote:

Originally Posted by websinger

I assume ( silly me ) that what I want here is some sort of indirection. but either I'm not setting up the pointers right
( not unlikely ) or I've badly misunderstood the syntax here.

Thanks for any assistance

Jeff

You've badly misunderstood the syntax and the difference between runtime strings vs compile time variables names.

When you declare a variable you have to give an actual type. Putting the name of a variable which holds a string won't work because the string isn't there during compile time. If you want to decide the type of plotter at compile time you can probably do so with macros, but that probably isn't what you want.

The proper way of doing this is to define a Plotter class that is a superclass of XPlotter and GIFPlotter and declare plotter as a pointer to type Plotter. A pointer of a superclass may point to an object of any of its subclasses.

websinger 09-07-2006 03:54 PM

Re: Plotter
 
Hi
these are from Gnu's plotutils package specifically
the plotter class ( c++ plotting library )
from <plotter.h>
Quote:

/* PLOTTER CTORS (new-style, thread-safe) */
Plotter (FILE *infile, FILE *outfile, FILE *errfile, PlotterParams &params);
Plotter (FILE *outfile, PlotterParams &params);
Plotter (istream& in, ostream& out, ostream& err, PlotterParams &params);
Plotter (ostream& out, PlotterParams &params);
Plotter (PlotterParams &params);
myplotter is a variable which will carry either the values of
"XPlotter" "GIFPlotter"
the end of which is to convey on (my line 68)

Code:

XPlotter plotter(cin,cout,cerr,params);
or
GIFPlotter plotter(cin,somefile.gif,cerr,params);

what I wish of course is to be able to choose whether
to output to the screen or to a file.

Jeff

websinger 09-07-2006 04:00 PM

re: Plotter class
 
ntubski:
yes- but it is a plotter class ( see above )

Quote:

The proper way of doing this is to define a Plotter class that is a superclass of XPlotter and GIFPlotter and declare plotter as a pointer to type Plotter. A pointer of a superclass may point to an object of any of its subclasses.
this is what I was attempting however ineffectively

ntubski 09-07-2006 07:28 PM

Quote:

Originally Posted by websinger
myplotter is a variable which will carry either the values of
"XPlotter" "GIFPlotter"
the end of which is to convey on (my line 68)

What I was trying to say in my first post is that you can't use a variable like that. The syntax for declaring a variable is
Code:

<TYPE_NAME> <VAR_NAME>;
neither of these can be substituted with an expression that returns a string. You have to put an actual type name and a variable name. If you could use a variable in this way you would have to write declarations with type names in quotes like this:
Code:

"GIFPlotter" plotter
But you don't write them like this because type names are not strings.


Quote:

Originally Posted by websinger
Hi
these are from Gnu's plotutils package specifically
the plotter class ( c++ plotting library )
from <plotter.h>

/* PLOTTER CTORS (new-style, thread-safe) */
Plotter (FILE *infile, FILE *outfile, FILE *errfile, PlotterParams &params);
Plotter (FILE *outfile, PlotterParams &params);
Plotter (istream& in, ostream& out, ostream& err, PlotterParams &params);
Plotter (ostream& out, PlotterParams &params);
Plotter (PlotterParams &params);

Quote:

Originally Posted by websinger
ntubski:
yes- but it is a plotter class ( see above )

Quote:

The proper way of doing this is to define a Plotter class that is a superclass of XPlotter and GIFPlotter and declare plotter as a pointer to type Plotter. A pointer of a superclass may point to an object of any of its subclasses.
this is what I was attempting however ineffectively

What I didn't realize until now, is that XPlotter and GIFPlotter are also plotutils classes. I thought they were your own which is why I was telling you that you should subclass Plotter.

All you have to do is declare plotter as a Pointer to Plotter:
Code:

...
Plotter *plotter; //declare 'plotter' here

if(!strcmp(plot,"X")){
  //Instances of the XPlotter and XDrawablePlotter classes always ignore the output stream     
  //argument, since they write graphics to an X Display rather than to a stream.
  plotter = new XPlotter(cin,NULL,cerr,params);
}
else{
  FILE *gif_file;
  gif_file = fopen("somefile.gif", "w");

  //add some error checking here...

  plotter = new GIFPlotter(cin,gif_file,cerr,params);
}
...

Note: I left params in both the constructors although according to the plotutils manual there is no such parameter.

websinger 09-07-2006 07:52 PM

Re: *plotter
 
Ntubski - Thanks I'll try that, this is actually closer to my
initial efforts, but I kept loosing scope from my function being declared inside the conditional. The pointer should address that.

I think parts of the Plotutils doc are somewhat out of date
the listing of the constructors in my reply to mark jones
is copied right out of the header file.

I've been using the plotutils code for XPlotter for a couple of
years. but have had to write separate programs to view or save to file . This iteration I'm trying to consolidate the code, and hopefully increase my skills on o-o syntax. Thanks for the assist.

Jeff

graemef 09-08-2006 12:10 AM

You want to first declare plotter as a pointer to the base class
Code:

Plotter *plotter;
Then within your if instantiate the object that you want
Code:

if(!strcmp(plot,"X"))
{
  plotter = new XPlotter();
  my_out="cout";
}
else
{
  plotter = new GIFPlotter();
  myout="somefile.gif";
}

Well that's the theory, I'll leave you the correct any mistakes I made!

ntubski 09-08-2006 10:24 AM

Quote:

Originally Posted by websinger
Ntubski - Thanks I'll try that, this is actually closer to my
initial efforts, but I kept loosing scope from my function being declared inside the conditional. The pointer should address that.

Just make sure you delete the object after you finish otherwise you get a memory leak :eek:

Quote:

I think parts of the Plotutils doc are somewhat out of date
the listing of the constructors in my reply to mark jones
is copied right out of the header file.
Yeah, only the old manual was online, and I didn't feel like downloading the whole package...

Quote:

I've been using the plotutils code for XPlotter for a couple of
years. but have had to write separate programs to view or save to file . This iteration I'm trying to consolidate the code, and hopefully increase my skills on o-o syntax. Thanks for the assist.

Jeff
Glad I could help :)


All times are GMT -5. The time now is 06:40 PM.