cplusplusquestion
10/8/2008 5:12:00 AM
On Oct 8, 2:45 pm, Salt_Peter <pj_h...@yahoo.com> wrote:
> On Oct 7, 11:44 pm, cplusplusquest...@gmail.com wrote:
>
>
>
> > On Oct 8, 1:36 pm, joseph cook <joec...@gmail.com> wrote:
>
> > > On Oct 7, 11:12 pm, cplusplusquest...@gmail.com wrote:
>
> > > > Sorry I didn't finish the edited yet:
>
> > > > I have code in main function:
>
> > > > int main(){
> > > > int a[20];
> > > > int b[20][20];
>
> > > > for(int i=0; i<20; i++)
> > > > a[i]=0;
> > > > .......
> > > > AClass a(a, b, c);
>
> > > This compiled?? What compiler? You are declaring two variable 'a' in
> > > the same scope. One is an array, and the other is of type "AClass"
> > > Joe C
>
> > Sorry again. You are right. The declaration should be AClass
> > aclass(a, b, c).
>
> ok, to get you started...
> this is a class:
>
> class A { };
>
> this is not:
>
> class A { }
>
> This doesn't compile:
>
> class A
> {
> public:
> A(int z) : n(z) { }
>
> };
>
> int main()
> {
> A a;
>
> }
>
> but this should:
>
> class A
> {
> int n;
> public:
> A(int z) : n(z) { }
>
> };
>
> int main()
> {
> A a(99);
>
> }
>
> Magic numbers is usually bad news, templates are SO simple:
>
> template < typename T, const std::size_t Size >
> class A
> {
> T array[Size];
> public:
> A()
> {
> for( std::size_t i = 0; i < Size; ++i )
> array[i] = i;
> }
>
> };
>
> int main()
> {
> A< int, 20 > a;
>
> }
>
> See any pointers? No, not directly, why?
> Pointers nearly always mean MORE work, less maintainable code, and few
> guarentees.
> You can pass an array by reference, but even that is a lost exercise.
> A solution using std::vector< int > would most likely be vastly
> superior.
Thank you for your explanation, I still can't understand why my code
does not work well.