Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!bbn!oberon!nunki.usc.edu!jeenglis From: jeenglis@nunki.usc.edu (Joe English) Newsgroups: comp.lang.c++ Subject: Re: Initializer in C++ Keywords: constructor, operator= Message-ID: <2615@nunki.usc.edu> Date: 7 Feb 89 00:30:39 GMT References: <668@usl.usl.edu> Reply-To: jeenglis@nunki.usc.edu (Joe English) Organization: University of Southern California, Los Angeles, CA Lines: 50 pcb@usl.usl.edu (Peter C. Bahrs) writes: >In standard C we have the ability to initialize arrays such as: > > int x[] = {1,2,3}; >and > static int x[2][3] = {{1,2,3},{4,5,6}}; >etc... > >I would like to have a class such as ARRAY (actually matrix) that >takes initializers... > ARRAY x(2,3)={{1,2,3},{4,5,6}}; > >So the construct would?? be someting like: > ARRAY (int r, int c) > { > allocate the buf to be r by c... > } >And now I guess the operator= would be: > void operator=(int *s) > { > now loop through and let each buf[i][j] = *s > and then s++ > } >This is as close as I have come, however it doesn't work, the operator= >never gets called. Maybe the constructor should take another parameter??? operator= is only called when an assignment is made to an *already initialized* object. In the case of an initializer like "SomeClass Foo = Bar;" what gets called is SomeClass::Someclass(typeof(Bar)), if it exists. I'm not sure if your example is legal -- can you use both initializer forms in the same statement, like "SomeClass Foo(diddle,wubba) = Bar;" ? You're right, you need to add another parameter to the constructor, to take the values of the array. However, you can't write ARRAY x(2,3,{{...},{...}}); since it's not possible to declare aggregate or array constants other than strings (I think**). You'll still have to declare a static int[][] to hold the values (since this can take an aggregate initializer) and pass it to the constructor. (**Or is it possible in C++ yet? If so, I haven't heard about it. The lack of support for aggregate constants is being discussed in comp.lang.c right now.) --Joe English jeenglis@nunki.usc.edu