[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



Hej gnalle

Ref:
The C++ Programing Language / Bjarne Stroustrup. -- 3rd ed. 1. printing
(the page numbering might differ by up two pages when using the 7.+
printing):

par. 10.4.2 Default Constructors (p.243):
    "If a user has declared a default constuctor, that one will be used;
otherwise, the compiler will try to generete one if needed. ..."

Dit eksempel er fulkommen legalt --- men jeg ville være *meget* forsigtig
med at anvende det "ro i sjælen"!
Du rører nemlig noget jeg har lært at kalde for: "minimal kanonisk form"

Den minimale kanoniske form inkluderer default-constructor,
copy-constructor samt assignment-operator:

class foo {
protected:
    double x;
public:
    foo() { /* some x initializer */ );
    virtual ~foo() { }
private:  // prevent usage -- ensure compile-time error if used:
    foo(const foo&);
    foo& operator= (const foo&);
};

Definerer man derimod en klasse med *udelukkende* non-default
constructors:

class bar : public foo {
public:
    bar(double i) { x= i; }
};

er den minimale kanoniske form ikke implicit erklæret.

Et andet problem med dit eksempel er at class a ikke har en virtuel
destructor - der er reserveret plads til b[2] men en
    delete[] myarray;
vil ikke nødvendigvis fjerne b[2] idet myarray jo kun fylder a[2]  // dog
== b[2] i eksempel


Med mindre du til fingerspidsidserne har tjek på hvad der sker når du
allokerer og deallokerer vil jeg anbefale dig at bruge
class vector<.> der er en optimeret C++ klasse istedet for "C" arrays:
// Det er en anvorlig mangel at undervisere generelt ikke har kendskab til
C++-stl

#include <vector>
int main()
{
    vector<foo> myvector(2, bar(1));
}

MORALE:
Blandingen af "C" og "C++" kode er utrolig farlig - gennerelt må til
enhver tid siges om "C" -- brug den ikke med "ro i sjælen"!

gnalle 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.
>
> --
>  Niels L Ellegaard
>
> 'http://dirac.ruc.dk/~gnalle/'




 
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] *