Go back to Richel Bilderbeek's homepage.
Go back to Richel Bilderbeek's C++ page.
Keyword to start a class declaration. A class is a user-defined type with multiple
elements and access levels.
The
class keyword
also be used to create a template function.
Don't
forget the semicolon at the end of the class definition!
A
class can have member functions, member data,
member constants, and member types.
struct MyClass
{
//public by default
void SetX(const
int x) { m_x = x; } //A member function
int m_x; //A
member variable
};
All
classes have a (default-constructed) constructor,
destructor, copy
constructor and copy-assignment operator:
struct NoClass {}; //Do all
classes really have a constructor, destructor,
//copy
constructor and copy-assignment operator?
This
class called NoClass is silently converted by your compiler to the following
(from [0]).
struct NoClass
{
NoClass() //Default constructor
{
//something
}
NoClass(const NoClass& rhs) //copy constructor
{
//something
}
~NoClass() //Default destructor
{
//something
}
NoClass& operator=(const NoClass& rhs) //copy-assignment
operator
{
//something
}
};
Know
what functions C++ silently writes and calls [0].
A
class has three different access levels: public,
private and protected.
A class' default for functions and variables is private.
This is the only (!) difference with a struct,
which has public as its default access level.
struct MyClass
{
//public by default, so the keyword public below is redundant
};
{
MyClass m;
m.m_public = 0; //OK, this
member variable is public
m.m_protected = 0; //Not allowed, this member variable is protected
m.m_private = 0; //Not allowed, this member variable is private
}
Specific
class types are an abstract base class,
a base class, a derived
class, a functor and a template class.
See
class design for some guidelines on class design.
Design patterns are standarized class designs to achieve a certain goal, often
involving multiple classes.
*
Canvas
*
DllHandle
[0]
Scott Meyers. Effective C++ (3rd edition).ISBN: 0-321-33487-6. Item 5: 'Know
what functions C++ silently writes and calls'
Go back to Richel Bilderbeek's C++ page.
Go back to Richel Bilderbeek's homepage.