Additional Features
Additional Features
in C++
Pointer to Class
Members
Defining a Pointer of Class type
class Simple
{
public:
int a;
};
int main()
{
Simple obj;
Simple* ptr; // Pointer of class type
ptr = &obj;
int main()
{
Data d, *dp;
dp = &d; // pointer to object
d.*ptr=10;
d.print();
dp->*ptr=20;
dp->print();
}
Run-Time Type Identification
Typeid
• The function:
const typeinfo typeid(arg)
• Typeinfo:
class type_info {
public:
virtual ~type_info();
int operator==(const type_info& rhs) const;
int operator!=(const type_info& rhs) const;
int before(const type_info& rhs) const;
const char* name() const;
const char* raw_name() const;
private:
...
};
Examples of typeid() use
• class foo { … };
typeid(foo).name() returns “foo”
• foo *ptr;
typeid(ptr).name() returns “foo*”
typeid(*ptr).name() returns “foo”
• Typeid and dynamic_cast information is carried in a class’s virtual function pointer table and is intended to
be used only with polymorphic classes, e.g., those with at least one virtual function.
• Typeid works for non-polymorphic classes, but returns only static type info, e.g., based on the static pointer
type, not on the type of the object pointed to.
• You must enable run-time type information (RTTI) in your project settings (C/C++ tab, C++ language
category). Your program will crash if you use dynamic_cast or RTTI and forget to do this.
Namespaces
C++ Mutable Keyword
The mutable storage class specifier in C++ (or use of mutable keyword in C++)
auto, register, static and extern are the storage class specifiers in C. typedef is also considered as
a storage class specifier in C. C++ also supports all these storage class specifiers. In addition to
this C++, adds one important storage class specifier whose name is mutable.
using namespace std; void changePlacedOrder(string p) const const Customer c1("Pravasi Meet", "Ice Cream", 3, 100);
{ c1.display();
// Customer Class
placedorder=p; c1.changePlacedOrder("GulabJammuns");
class Customer {
} c1.changeBill(150);
// class Variables
c1.display();
string name;
// change the bill return 0;
mutable string placedorder;
void changeBill(int s) const { bill = s; } }
int tableno;
mutable int bill;
// to display Output
// member methods
void display() const Customer name is: Pravasi Meet
public:
{ Food ordered by customer is: Ice Cream
// constructor cout << "Customer name is: " << name << endl; table no is: 3
Customer(string s, string m, int a, int p) cout << "Food ordered by customer is: " Total payable amount: 100
{ << placedorder << endl; Customer name is: Pravasi Meet
name= s; cout << "table no is: " << tableno << endl; Food ordered by customer is: GulabJammuns
placedorder=m; cout << "Total payable amount: " << bill << endl; table no is: 3
tableno = a; } Total payable amount: 150
bill = p; };
}
C++ Mutable Keyword
• The keyword mutable is mainly used to allow a particular data
member of const object to be modified. When we declare a function
as const, the this pointer passed to function becomes const. Adding
mutable to a variable allows a const pointer to change members.
• mutable is particularly useful if most of the members should be
constant but a few need to be updatable. Data members declared as
mutable can be modified even though they are the part of object
declared as const. You cannot use the mutable specifier with names
declared as static or const, or reference.
Use of explicit keyword in C++
• Explicit Keyword in C++ is used to mark constructors to not implicitly
convert types in C++. It is optional for constructors that take exactly
one argument and work on constructors(with a single argument)
since those are the only constructors that can be used in typecasting.
• We can avoid such implicit conversions as these may lead to
unexpected results. We can make the constructor explicit with the
help of an explicit keyword.
• if we try the following program that uses explicit keywords with a
constructor, we get a compilation error.
explicit keyword in C++ int main()
#include <iostream>
{
using namespace std;
// a Complex object
private:
if (com1 == 3.0)
double real;
cout << "Same";
double imag;
else
double i = 0.0) :
Output
real(r), imag(i)
{
Compiler Error : no match for 'operator==' in 'com1 == 3.0e+0‘
}
imag == rhs.imag);
};