[comp.lang.c++] inheriting an object with a strange constructor...

andersnb@lab.nyu.edu (Brian Anderson) (12/11/89)

Hi,
	I have an object that uses the va_arg facility to create an
object that has an unknown size until runtime:

class A {
private:
	char* thing;	
public:
	A(int i...) {
	  // create space for thing and
	  // use va_arg to read in i arguments
	  // placing them in thing
	}
};

	I must do it this way since the size of each argument is
variable and I don't want to waste space using unions.  This work
fine.  However, I now want to inherit this class into another class B.
I there anyway to pass the variable number of arguments from class B's
constructor to the constructor of A?  For example (probably not valid
C++ code):

class B: A {
public:
	B(int i...): A(i...) {
	   // other stuff
	}
};

	Is there a better way to do this?  Remember I am very
concerned about space utilization, so an array of union types is not a
good answer.
--
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Brian G. Anderson                                       |
NYU Ultracomputer Research Project                     |||
715 Broadway  Rm. 1006                                |||||
New York, NY  10003                                   |||||
(212) 998-3346                                     --- //\ ---
arpa:	andersnb@cmcl2                             ----/ \----
uucp:	{ihnp4,seismo}!cmcl2!andersnb              ----   ----

chip@ateng.com (Chip Salzenberg) (12/13/89)

According to andersnb@lab.nyu.edu (Brian Anderson):
>class A {
>private:
>	char* thing;	
>public:
>       A(int i...) {
>         // parse arguments
>	}
>};
>
>class B: A {
>public:
>       B(int i...): A(i...) {
>	   // other stuff
>	}
>};

I don't think you can do what I think you want.  But if you provide a version
of A's constructur that's fast and does nothing, you can then call A::init()
with a va_list argument:

	class A {
	public:
		A(int i ...);
	protected:
		init(int, va_list);     // does all the construction work
	private:
		char *thing;
	};

	class B: public A {
	public:
		B(int i ...);
	};

	A::init(int i, va_list ap)
	{
	    if (i)
	    {
		// allocate thing, etc.
	    }
	    else
		thing = NULL;
	}

	A::A(int i ...)
	{
	    va_list ap;
	    va_start(ap, i);
	    init(i, ap);
	    va_end(ap);
	}

	B::B(int i ...)
	  : A(0)  // note that init(0) does nothing important
	{
	    va_list ap;
	    va_start(ap, i);
	    init(i, ap);
	    va_end(ap);
	}

It could get more complicated, but this will work.
-- 
You may redistribute this article only to those who may freely do likewise.
Chip Salzenberg at A T Engineering;  <chip@ateng.com> or <uunet!ateng!chip>
	  "The Usenet, in a very real sense, does not exist."