[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] Konstante template argumenter i C++



Fredag den 21. april 2006 13:58 skrev Michael Rasmussen:
> On Fri, 21 Apr 2006 13:35:55 +0200, Mads Bondo Dydensborg wrote:
> > Ikke så vidt jeg kan vurdere. Jeg tror det er et løfte til brugeren om at
> > templaten ikke ændrer ved det referencen peger på... ?
> int main() {
>     SayHi< const std::string > sayHi;
>     sayHi.setName("Adam");

"Adam" er const ( const char hejs C kompatibilitet, alt det der).

>     std::string name = sayHi.greeting(); 
Det er en kopiering, så det er ligemeget at greeting returnerer en const.

>     std::cout << name << std::endl;
>     std::cout << "Adding family name to name and try inserting" <<
> std::endl; name += " Ant";
>     sayHi.setName(name);

const her i member betyder bare at member ikke ændrer name. 

>     std::cout << sayHi.greeting() << std::endl;
>     name += "s";
>     char *s = const_cast< char *> (name.c_str());

Det her er meget farligt. c_str ændrer sig så snart name ændrer sig. Det gør 
den så ikke i resten af programmet, tilfældigvis.

>     sayHi.setName(s);

char * går stadig som const pga. C kompat.
Der bliver konstrueret et nyt midlertigt objekt som parameter, som 
efterfølgende bliver assignet til din name vha. assignment operatoren (=).
>     std::cout << "Greeting from a char*" << std::endl;
>     std::cout << sayHi.greeting() << std::endl;

Og, det er det der kommer ud her.

> }
>
> Som ovenstående viser, er den fuldstændig ligeglad med const eller ej
> erklæringen. Mystiks:-\

Det er ikke så underligt i dette tilfælde. Det er ikke en konstant parameter i 
templateerklæringen, men "blot" en normal klasse, som bliver brugt som type i 
member erklæringer.

Det jeg har gang i, er en konstant parameter i template erklæringen. (Eller, 
det er ihvertfald det jeg prøver). Det er noget anderledes, i det man kun må 
bruge "constant expressions", "the address of an object of function with 
external linkage" eller "a non-overloaded function pointer to member".

Mads

-- 
Mads Bondo Dydensborg.                               sslug@sslug
Smoking kills. If you're killed, you've lost a very important part of your
life.  
            - Brooke Shields, during an interview to become spokesperson for a
                                               federal anti-smoking campaign.



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