[Home]
[Edit this page]
[Recent Changes]
[Special Pages]
[Help]
OtherOS » cppint » CSharpFAQ » CppConstructor
Displaying differences between revision 4 and revision 5
= (C++) Constructor =
The method called when a class is instanciated. A class can have multiple constructors. The opposite of a constructor is a destructor.
[code]
class Example
{
public:
Example()
: mValue(0)
{
//Empty, as mValue is already set to zero
}
Example(const int& value) //Constructor #2
: mValue(value);
{
//Empty, as mValue is already set to value
}
const int mValue;
};
int main(int argc, char* argv[])
{
Example e1; //Calls constructor #1
Example e2(10); //Calls constructor #2
Example * pe1 = new Example; //Calls constructor #1
Example * pe2 = new Example(10); //Calls constructor #2
delete pe1; //Calls destructor
delete pe2; //Calls destructor
return 0;¶
}
[/code]
Define and initialize member variables in the same order [1]. Prefer initialization to assignment in constructors [2]. Never call virtual functions during construction or destruction [3,4].
== Code links ==
* argc
* argv
* char
* class
* const
* delete
* int
* main
* new
* return
== References ==
* 1) Herb Sutter and Andrei Alexandrescu. C++ coding standards: 101 rules, guidelines, and best practices. ISBN: 0-32-111358-6. Item 47: 'Define and initialize member variables in the same order'.
* 2) Herb Sutter and Andrei Alexandrescu. C++ coding standards: 101 rules, guidelines, and best practices. ISBN: 0-32-111358-6. Item 48: 'Prefer initialization to assignment in constructors'
* 3) Herb Sutter and Andrei Alexandrescu. C++ coding standards: 101 rules, guidelines, and best practices. ISBN: 0-32-111358-6. Item 49: 'Never call virtual functions during construction or destruction'
* 4) Scott Meyers. Effective C++ (3rd edition).ISBN: 0-321-33487-6. Item 9: Never call virtual functions during construction or destruction.
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
OtherOS » cppint » CSharpFAQ » CppConstructor
Displaying differences between revision 4 and revision 5
= (C++) Constructor =
The method called when a class is instanciated. A class can have multiple constructors. The opposite of a constructor is a destructor.
[code]
class Example
{
public:
Example()
: mValue(0)
{
//Empty, as mValue is already set to zero
}
Example(const int& value) //Constructor #2
: mValue(value);
{
//Empty, as mValue is already set to value
}
const int mValue;
};
int main(
{
Example e1; //Calls constructor #1
Example e2(10); //Calls constructor #2
Example * pe1 = new Example; //Calls constructor #1
Example * pe2 = new Example(10); //Calls constructor #2
delete pe1; //Calls destructor
delete pe2; //Calls destructor
[/code]
Define and initialize member variables in the same order [1]. Prefer initialization to assignment in constructors [2]. Never call virtual functions during construction or destruction [3,4].
== Code links ==
* argc
* argv
* char
* class
* const
* delete
* int
* main
* new
* return
== References ==
* 1) Herb Sutter and Andrei Alexandrescu. C++ coding standards: 101 rules, guidelines, and best practices. ISBN: 0-32-111358-6. Item 47: 'Define and initialize member variables in the same order'.
* 2) Herb Sutter and Andrei Alexandrescu. C++ coding standards: 101 rules, guidelines, and best practices. ISBN: 0-32-111358-6. Item 48: 'Prefer initialization to assignment in constructors'
* 3) Herb Sutter and Andrei Alexandrescu. C++ coding standards: 101 rules, guidelines, and best practices. ISBN: 0-32-111358-6. Item 49: 'Never call virtual functions during construction or destruction'
* 4) Scott Meyers. Effective C++ (3rd edition).ISBN: 0-321-33487-6. Item 9: Never call virtual functions during construction or destruction.
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
