C++ initializing values στον constructor

5 views
Skip to first unread message

Demosthenes Koptsis

unread,
Dec 28, 2011, 4:34:45 AM12/28/11
to TheHackerspace
Υπάρχει διαφορά στο

// constructor
Employee::Employee( const string &first, const string &last,
const string &ssn )
{
firstName=first;
lastName=last;
socialSecurityNumber=ssn;
} // end Employee constructor


από το

// constructor
Employee::Employee( const string &first, const string &last,
const string &ssn )
: firstName( first ), lastName( last ), socialSecurityNumber( ssn )
{
//empty body
} // end Employee constructor


Panos Georgiadis

unread,
Dec 28, 2011, 5:26:37 AM12/28/11
to thessaloniki...@googlegroups.com
Το δεύτερο είναι για συντινομία. Αν γράφεις μία class με 5 μεταβλητές που θέλει να αρχικοποιήσεις είναι καλό. Συνήθως από 5 και πάνω τα γράφεις με τον πρώτο τρόπο καθαρά για λόγους συντήρισης και εύκολης ανάγνωσης του κώδικα. Εννοείται δε, πως αν γράφεις της κλάσεις σου σε ξεχωριστό header file τότε γράφει με τον πρώτο τρόπο.

Δεν ξερω αν υπάρχει κάποια άλλη διαφορά, θα σου πουν τα παιδιά :)

2011/12/28 Demosthenes Koptsis <demost...@gmail.com>


--
TheHackerspace - Thessaloniki's Hackerspace

http://the-hackerspace.org/



--
Best Regards,
Panos Georgiadis

Demosthenes Koptsis

unread,
Dec 28, 2011, 5:41:34 AM12/28/11
to thessaloniki...@googlegroups.com
Ευχαριστώ.
:)

TheHackerspace

unread,
Dec 28, 2011, 9:13:33 AM12/28/11
to thessaloniki...@googlegroups.com
Διάλεξε όποιον τρόπο θές!

Προσωπικά θα σου πρότεινα τον πρώτο γιατί είναι περισσότερο K.I.S.S.

Στις 28-12-2011, ημέρα Τετ, και ώρα 12:26 +0200, ο/η Panos Georgiadis
έγραψε:

Demosthenes Koptsis

unread,
Dec 28, 2011, 5:52:52 PM12/28/11
to thessaloniki...@googlegroups.com
K.I.S.S. = ?

TheHackerspace

unread,
Dec 28, 2011, 5:56:18 PM12/28/11
to thessaloniki...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages