[an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive]
 
[an error occurred while processing this directive] [an error occurred while processing this directive]
Skåne Sjælland Linux User Group - http://www.sslug.dk Home   Subscribe   Mail Archive   Forum   Calendar   Search
MhonArc Date: [Date Prev] [Date Index] [Date Next]   Thread: [Date Prev] [Thread Index] [Date Next]   MhonArc
 

Re: [CPROG] arv og arrays



Anders Nielsen <sslug@sslug> skrev:

> > Følgende program ser ud til at compile, men når jeg ser i Bjarne
> > Stroustrups bog, kan jeg ikke gennemskue hvorfor:
> >
> > class a {
> >   double x;
> > };
> >
> > class b:public a{
> > };
> >
> > int main(int argc, char *argv[])
> > { a *myarray;
> >   myarray = new b[2];
> > }
> >
> > Er det altid tilladt at gøre sådan. Det jeg leder efter er egentlig en
> > litteraturhenvisning, så jeg kan benytte denne metode med ro i sjælen.

"In general, if a class Derived has a public base class Base, then a
Derived* can be assigned to a variable of type Base* without the use
of explicit type conversion." (3rd ed., sec 12.2, p304).

Hvis det var det, du mente?

> Jeg mener dog helt klart, at du vil få problemer med denne konstruktion.
> Nu er layoutet for a og b jo helt ens i  dit eksempel, men hvis det ikke
> længere er tilfældet, så kan oversætteren vel ikke afgøre hvor myarray[1]
> starter.....

myarray er af typen "pointer til a", og det er altså ikke selve a og
b, der skal være plads til i arrayet. Derfor går det godt.

Når man vil til at hente b'erne ud igen kan der dog opstå problemer,
fordi man kun ved, at der er a'er i sit array. Det kan derfor blive
nødvendigt at undersøge med dynamic_cast, hvordan det egentlig
forholder sig.

	if (b* bptr = dynamic_cast<b*>&myarray[1]) {
		// Jeps, myarray[1] var et b, og kan nu tilgås via bptr
	}	

-- 
Regards, Anders                         If a Microsoft product
(address is valid)                      fails, who do you sue?


 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2005-08-10, 20:07 CEST [an error occurred while processing this directive]
This page is maintained by [an error occurred while processing this directive]MHonArc [an error occurred while processing this directive] # [an error occurred while processing this directive] *