[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: [PROGRAMMERING] member initialisation. ISO C++ forbids assignment of arrays



> Hvordan initialiserer jeg så array's?
Det afhænger lidt af, hvad du vil med dit array. Men normalt kopierer man
element for element med funktioner der gør dette for dig (strncpy f.eks.).

Et char array kan dog ogsaa initialiseres direkte, hvis ikke det er en
normal klasse member.

HC
------------------------------------------
#include <iostream>
#include <string>
#include <cstring>

using namespace std;

namespace {
        class Test {
                char name[5];
        public:
                Test () { strncpy(name, "test", sizeof(name)); }
                virtual ~Test () {};
                const char *my_name () const { return name; }
        };

        class T1 {
                char *n;
        public:
                T1 () : n("T1") {}
                const char* N () { return n; }
        };

        class T2 {
                string n;
        public:
                T2 () : n("T2") {}
                const string& N () { return n; }
        };

        class T3 {
                static char n[5];
        public:
                const char* N () { return n; }
        };

        char T3::n[5] = "T3";
}


int main (int argc, char *argv[])
{
        Test tst;
        T1 t1;
        T2 t2;
        T3 t3;
        cout << tst.my_name() << ' ' << t1.N() << ' ' << t2.N() << ' ' <<
                t3.N() << endl;

        char s[5] = "hej";
        cout << s << endl;
        // s = "nej " <--- fejler
}



 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2006-05-01, 02:01 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] *