bmc@mayo.edu (Bruce Cameron) (06/13/91)
Being a novice to C++, I do not understand why I am unable to access a
base class' constructor from a derived class. I understand the syntax
to be:
dc::dc(dc ctor params):[bc](bc ctor params)
{
body of dc ctor
}
Yet when I try to do this with a base class that makes use of a vararg
list I get a variety of syntax errors (using Sun C++2.1). Is it
possible to have a base class constructor with a vararg list and
derive classes from it that initialize the base class through its
constructor? If not, any work arrounds for this? How are these
supposed to work?
Thanks for any and all help.
(only error I get is line 57: syntax error)
Sample code I'm trying:
#include <stdlib.h>
#include <stream.h>
#include <stdarg.h>
class base {
private:
int *b1;
protected:
int b2, b3;
public:
base (int a ...);
void bar ();
};
class derived : public base {
private:
int *d1;
protected:
base::b2;
base::b3;
int d2;
public:
derived (int a, int *b, int c ...);
void foo ();
};
base::base (int a ...)
{
va_list va;
b2 = 3;
b3 = 2;
b1 = new int[b2];
for (int i = 0; i < b2; i++) b1[i] = b3;
va_start (va, a);
if (!(a)) return;
if (!(b3 = va_arg (va, int))) return;
b2 = a;
delete b1;
b1 = new int [b2];
for (i = 0; i < b2; i++) b1[i] = b3;
}
void base::bar ()
{
for (int i = 0; i < b2; i++)
cout << b1[i] << " ";
}
derived::derived (int a, int b):(int c ...)
{
d2 = a;
d1 = new int [d2];
for (int i = 0; i < d2; i++) d1[i] = b;
}
void derived::foo()
{
if (d2 < b2) {
for (int i = 0; i < d2; i++)
if (d1[i] < b3) d1[i] = b3;
}
else {
for (int i = 0; i < b2; i++)
if (d1[i] > b3) d1[i] = b3;
}
for (int i = 0; i < d2; i++)
cout << d1[i] << " ";
}
main ()
{
base b (6,12);
derived d (12,6);
cout << "base class\n";
b.bar();
cout << "\n derived class \n";
d.foo();
}
--
--Bruce
"An eye for an eye makes the whole world blind"
Ghandi
----------------------------------------------------
Bruce M. Cameron bmc@bru.mayo.edu
Medical Sciences 1-14 voice: (507) 284-3288
Mayo Foundation fax: (507) 284-9623
200 1st ST SW
Rochester, MN 55905 ARS -- WD9CKW
----------------------------------------------------
The views expressed are those of the author and do not reflect
those of the Mayo Foundation, the Biomedical Imaging Resource
or any person connected with those organizations.
They don't speak for me, I don't speak for them.steve@taumet.com (Stephen Clamage) (06/14/91)
bmc@mayo.edu (Bruce Cameron) writes: >Being a novice to C++, I do not understand why I am unable to access a >base class' constructor from a derived class... >Yet when I try to do this with a base class that makes use of a vararg >list I get a variety of syntax errors (using Sun C++2.1). Is it >possible to have a base class constructor with a vararg list and >derive classes from it that initialize the base class through its >constructor? If not, any work arrounds for this? How are these >supposed to work? You can't do it. There is no syntax to support just passing along a variable argument list to any function, constructor or not. You have to use the facilities in <stdarg.h> or <varargs.h>. Some workarounds: 1. Don't use varargs in the constructor. Pass an array of args, either with a separate "count" argument, or a designated termination value at the end of the array (like NULL for an array of pointers). 2. Write an auxiliary private 'init' function called from constructors. It takes a va_alist parameter. The constructor sets up the va_alist and passes it to the init function. -- Steve Clamage, TauMetric Corp, steve@taumet.com