error passing array of abstract type

  • Thread starter Thread starter Andrew K
  • Start date Start date
A

Andrew K

Hello,

I have run into a problem with VC7 that worked in VC6.

These two sections should be exactly the same...

class test { public: virtual void blah(void)=0;};
void func(test[]) {}

and

class test { public: virtual void blah(void)=0;};
void func(test const *) {}

but only the 2nd compiles in VC7 where as both compiled in VC6.
The VC7 error for the first one is error C2259: 'test' : cannot instantiate
abstract class

Any reason why VC7 doesn't accept it?

Thanks
 
Andrew said:
Hello,

I have run into a problem with VC7 that worked in VC6.

These two sections should be exactly the same...

class test { public: virtual void blah(void)=0;};
void func(test[]) {}

and

class test { public: virtual void blah(void)=0;};
void func(test const *) {}

but only the 2nd compiles in VC7 where as both compiled in VC6.
The VC7 error for the first one is error C2259: 'test' : cannot
instantiate abstract class

Any reason why VC7 doesn't accept it?

Yes, because it's illegal. It's not possible to create an array of 'test'
since it contains a pure virtual function. VC6 was wrong to accept it.

-cd
 
But 'test[]' is the same as 'test const *' isn't it? Pointers to abstract
classes are legal so if the two are equivalent then the array version should
be accepted to no?

I'm missing something aren't I?

Carl Daniel said:
Andrew said:
Hello,

I have run into a problem with VC7 that worked in VC6.

These two sections should be exactly the same...

class test { public: virtual void blah(void)=0;};
void func(test[]) {}

and

class test { public: virtual void blah(void)=0;};
void func(test const *) {}

but only the 2nd compiles in VC7 where as both compiled in VC6.
The VC7 error for the first one is error C2259: 'test' : cannot
instantiate abstract class

Any reason why VC7 doesn't accept it?

Yes, because it's illegal. It's not possible to create an array of 'test'
since it contains a pure virtual function. VC6 was wrong to accept it.

-cd
 
I thought VC7 was correct initially too, but then read some google postings
that said the two versions I showed should be equivalent, but it was in the
context of a char(which should make no different I'd think). I also tried
it in gcc and it accepted both versions, which was why I asked.

Thanks

Carl Daniel said:
Andrew said:
Hello,

I have run into a problem with VC7 that worked in VC6.

These two sections should be exactly the same...

class test { public: virtual void blah(void)=0;};
void func(test[]) {}

and

class test { public: virtual void blah(void)=0;};
void func(test const *) {}

but only the 2nd compiles in VC7 where as both compiled in VC6.
The VC7 error for the first one is error C2259: 'test' : cannot
instantiate abstract class

Any reason why VC7 doesn't accept it?

Yes, because it's illegal. It's not possible to create an array of 'test'
since it contains a pure virtual function. VC6 was wrong to accept it.

-cd
 
Andrew said:
I thought VC7 was correct initially too, but then read some google
postings that said the two versions I showed should be equivalent,
but it was in the context of a char(which should make no different
I'd think). I also tried it in gcc and it accepted both versions,
which was why I asked.

The C++ standard is actually quite specific on it. Section 8.3.4, paragraph
1 says:

<quote>
In a declaration T D where D has the form
D1 [constant-expression-opt]

and the type of the identifier in the declaration T D1 is
"derived-declarator-type-list T," then the type of the
identifier of D is an array type. T is called the array element type; this
type shall not be a reference type, the
(possibly cv-qualified) type void, a function type or an abstract class
type. ...
</quote>

VC7 and 7.1 are correct in rejecting it. In the cases where it's legal, the
two parameter lists that you showed are effectively identical - the array
syntax is simply not legal for abstract classes.

-cd
 
Back
Top