Showing posts with label C. Show all posts
Showing posts with label C. Show all posts
Apr 14, 2013
How can I handle easily a pointer to a method?
How can I handle easily a pointer to a method?
If you use templates, you won't have to write the annoying type of a pointer to a method (at the 'cost' of using a template). You can also use typedef to declare the type.
struct A
{
void m() {}
};
template
void f(A &a
8
PMETHOD pm)
{
(a.*pm)()
}
void function()
{
A a;
f(a, &A::m);
}
How do I declare, assign and call a pointer to a method?
How do I declare, assign and call a pointer to a method?
Note that a pointer to a method does not hold a second pointer to an instance of a
class. To use a pointer to a method, you need an instance of the class onto which this
method can be called (possibly a second pointer).
struct A
{
void m(int) {}
};
void function()
{
void (A::*pm)(int) = &A::m; // pointer on method
A a; // instance of a class
(a.*m)(1); // calling the method with parameter value 1.
}
How do I put the code of methods outside classes?
How do I put the code of methods outside classes?
It is possible to put in a class only the prototype of the methods, and to put all the
algorithms outside. This is recommended, because it allows the programmer to read
a short prototype of the class, and makes re-usability easier:
// in a header file (.h file)
struct A
{
int a;
A();
virtual ~A();
void f();
};
// in a compilation unit (.cpp file)
A::A()
{
/* ... */
};
A::~A()
{
/* ... */
};
void A::f()
{
/* ... */
};
How do I free the memory allocated to a class?
How do I free the memory allocated to a class?
The method called when the memory occupied by a class is freed is called the destruc-
tor. With derived classes, destructor should always be virtual. If a class is destroyed
through a base class pointer whose destructor isn't virtual, the result is undened
(only part of the destructors will be called).
struct A
{
A() {}
virtual ~A() {}
};
struct B: public A
{
B() {}
~B() {}
};
void function()
{
B *b = new B();
A *a = b;
delete a; // calls ~A and ~B. If ~A wasn't virtual, only ~A would be called.
}
How do I create an instance of a class?
How do I create an instance of a class?
The methods called when a class is created are called contructors. There are four
possible ways of specifying constructors; the fth method is worth mentioning for
clarifying reasons:
- default constructor
- copy constructor
- value constructor
- conversion constructor
- copy assignment (not a constructor)
struct A
{
A() { /* ... */ } // default constructor
A(const A &a) { /* ... */ } // copy constructor
A(int i, int j) { /* ... */ } // value constructor
A &operator=(const A &a) { /* ... */ } // copy assignment
};
struct B
{
B() { /* ... */ } // default constructor
B(const A &a) { /* ... */ } // conversion constructor
};
void function()
{
A a0(0, 0); // shortcut, value constructor
A a1(a0); // shortcut, copy constructor
B b1(a1); // shortcut, conversion constructor
B b; // shortcut, default constructor
b1 = a0; // conversion contructor
a0 = a1; // copy assignment
}
Who has access to class members?
Who has access to class members?
There are three levels of access to members
private : access is granted only to the class' methods and to friend functions
and classes (Question 6.17 explains friend).
protected : access is granted only to the methods and to derived classes' meth-
ods.
public : access is granted to everyone.
Restricting access to members is usefull for detecting illicit use of the members of a
class when compiling, as shown in the following code.
class A
{
private:
int a0;
void f0() { /* ... */ }
protected:
int a1;
void f1() { f0(); } // ok
public:
int a2;
void f2() { /* ... */ }
};
void function()
{
A a;
a.a0 = 0; // error
a.f0(); // error
a.a1 = 0; // error
4
a.f1(); // error
a.a2 = 0; // ok
a.f2(); // ok
}
How do I use class and struct?
How do I use class and struct?
A C++ struct (or class) is almost like a struct in C (i.e. a set of attributes), but
has two additional kinds of members: methods and data types. A class' method has
to be used with an instance of that class. The important is that methods have always
access to their instance's attributes and data types.
struct A
{
typedef char t_byte; // type
unsigned char i; // attribute
void m() // method
{
t_byte b = 1; // m can access type t_byte
3
i = b; // m can access attribute i
}
};
void function()
{
A a;
a.m(); // an instance is required to call a method.
C++ Functions
C++ Functions
1 How do I declare, assign and call a pointer to a function?
int f(int, char)
{
// ...
}
// ...
int (*pf)(int, char); // pointer to a function
pf = f; // assignment
pf = &f; // alternative
int r = (*pf)(42, 'a');
2 How do I declare a type of a pointer to a function?
typedef int (*pf)(int, char);
3 How do I declare an array of pointers to a function?
typedef int (*pf)(int, char);
pf pfarray[10]; // array of 10 pointers to a function
How do I use references?
How do I use references?
A reference is merely an initialized pointer. This reduces signicantly zero pointer and
un-initialized pointers errors. Prefer references to pointers. Although the two following
codes look equivalent, the second implementation prevents invalid compilation.
// in C // in C++
int i; int i;
int *pi = &i; int &ti = i;
int *pj; int &rj; // g++ refuses to compile that
*pi = 0; ri = 0;
*pj = 0; // segfault rj = 0;
How do I use namespaces?
How do I use namespaces?
Namespace allow to code dierent entities without bothering about unicity of names.
A namespace is a logical entity of code. A namespace can be included into another
namespace (and so on). If a namespace is anonymous, its code is only accessible from
the compilation unit (.cpp le), thus equivalent to using static and extern in C.
// in file1.cpp
namespace name1
{
void function1() { /* ... */ }
namespace name2
{
void function2() { /* ... */ }
}
}
namespace // anonymous namespace
{
void function() { /* ... */ }
}
void function3() { function(); } // ok
// in file2.cpp
void function4a() { function1(); } // error
void function4c() { name1::function2(); } // error
void function4c() { function(); } // error
void function4b() { name1::function1(); } // ok
void function4d() { name1::name2::function2(); } // ok
using namespace name1; //makes accessible the entire namespace name1
void function4e() { function1(); } // ok
void function4f() { name2::function1(); } // ok
using name1::name2::function2; //makes accessible function2 only
void function3g() { function2(); } // ok
What is C++
What is C++?
C++ is C with classes. It was designed for one person to manage large amounts
of code, and so that a line of C++ express more thing than a line of C. The main
functionality C++ adds to C are:
- control of the accessibility to code within the source les (namespace, struct and class).
- mechanisms that make a line of code more expressive (constructors, destructors, operators, ...).
- object programming (class derivation).
- generic programming (templates).
Quick notes to C programmers
Quick notes to C programmers:
- instead of macros use
- - const or enum to de ne constants
- - inline to prevent function call overload
- - template to declare families of type and families of functions
- use new/delete instead of free/malloc (use delete[] for arrays)
- don't use void* and pointer arithmetic
- an explicit type conversion reveals an error of conception.
- avoid to use C style tables, use vectors instead.
- don't recode what is already available in the C++ standard library.
- variables can be declared anywhere: initialization can be done when variable is required.
- whenever a pointer cannot be zero, use a reference.
- when using derived class, destructors should be virtual.
Subscribe to:
Posts (Atom)