class Base
{
public:
virtual ~Base()
{
std::cout << "Hello from ~Base()" << std::endl;
}
};
class Derived : public Base
{
public:
virtual ~Derived()
{
//
std::cout << "Hello from ~Derived()" << std::endl;
}
};
Base *obj = new Derived();
delete obj;
Hello from ~Derived()
Hello from ~Base()
Hello from ~Base()
class Base
{
private:
HANDLE m_hFile;
public:
Base()
{
std::cout << "Hello from Base()" << std::endl;
m_hFile = ::CreateFileA(...);
// ,
SomeLib.SomeFunc(...);
}
virtual ~Base()
{
std::cout << "Hello from ~Base()" << std::endl;
//
::CloseHandle(m_hFile);
}
};
try
{
Base b;
}
catch(const std::exception &e)
{
std::cout << "Exception message: " << e.what() << std::endl;
}
Hello from Base()
Exception message: Something failed
class Base
{
private:
class CHandle
{
public:
~CHandle()
{
::CloseHandle(m_handle);
}
private:
HANDLE m_handle;
public:
// smart pointer'
// ,
void operator = (const HANDLE &handle)
{
m_handle = handle;
}
};
CHandle m_hFile;
public:
Base()
{
std::cout << "Hello from Base()" << std::endl;
m_hFile = ::CreateFileA(...);
// ,
SomeLib.SomeFunc(...);
}
virtual ~Base()
{
std::cout << "Hello from ~Base()" << std::endl;
}
...
const int i = 1;
i = 2; // error C3892: 'i' : you cannot assign to a variable that is const
int i = 1;
int* const j(&i);
int k = 2;
*j = k; // Ok
j = &k; // error C3892: 'j' : you cannot assign to a variable that is const
class Foo
{
private:
int i;
public:
void func() const
{
i = 1; // error C3490: 'i' cannot be modified because it is being accessed through a const object
}
};
template <typename T >
void bubble_sort( T &a )
{
for( T::size_type i = 0; a.size() && i < a.size() - 1; ++i )
{
for( T::size_type j = i; j + 1 > 0; --j )
{
if( a[j] > a[j+1] )
std::swap( a[j], a[j+1] );
}
}
}
std::vector<int> v;
v.push_back( 7 );
v.push_back( 1000 );
v.push_back( 134 );
v.push_back( 23 );
v.push_back( 1 );
bubble_sort( v );
template <typename T >
void invert_string( T &a )
{
T::size_type length = a.size();
for( T::size_type i = 0; i < (length/2); ++i )
{
std::swap( a[i], a[length - i - 1] );
}
}
std::string str = "abcdefg";
invert_string(str);
class NonCopyable
{
public:
NonCopyable(){}
private:
NonCopyable(NonCopyable&){}
private:
void operator=(const NonCopyable&){}
};
NonCopyable a;
NonCopyable b = a; // error C2248: 'NonCopyable::NonCopyable' : cannot access private member
a = b; // error C2248: 'NonCopyable::operator =' : cannot access private member
struct Foo
{
int i;
};
class Bar
{
int i;
};
Foo a;
a.i = 1; // Ok
Bar b;
b.i = 1; // error C2248: 'Bar::i' : cannot access private member declared in class 'Bar'
struct Foo
{
int i;
};
inline bool operator < (const Foo & lhs, const Foo & rhs)
{
return lhs.i < rhs.i;
}
std::set<Foo> data;
Foo a, b, c;
a.i = 7;
b.i = 1;
c.i = 6;
data.insert( a );
data.insert( b );
data.insert( c );
// data 1 6 7
struct Foo
{
int i;
char a;
};
int size = sizeof(Foo); // 8 , int + char = 5. 4, .. 4 .
// 1
#pragma pack(push, 1)
struct Foo
{
int i;
char a;
};
#pragma pack(pop)
int size = sizeof(Foo); // 5
class Base
{
public:
Base()
{
base_func();
}
void base_func()
{
func(); // pure virtual function call exception
}
virtual void func() = 0;
};
class Derived : public Base
{
public:
virtual void func()
{
}
};
class Base
{
public:
int i;
};
class Derived : private Base
{
// i private
};
class Derived2 : private Derived
{
public:
Derived2()
{
i = 2; // error C2247: 'Base::i' not accessible because 'Derived' uses 'private' to inherit from 'Base'
}
};
Derived d;
d.i; // error C2247
class Figure
{
...
void Draw() const;
...
};
class Square : public Figure
{
...
void Draw() const;
...
};
class Circle : public Figure
{
...
void Draw() const;
...
};
//
class Foo
{
public:
//
virtual void func() = 0;
};
class Bar : public Foo
{
public:
virtual void func()
{
}
};
Foo f; // error C2259: 'Foo' : cannot instantiate abstract class
Bar b; // Ok
try
{
//....
try
{
// Call something
}
catch(const std::exception& )
{
// Make/Check something..
throw; //
}
//...
}
catch(const std::exception& e)
{
std::cout << e.what() << std::endl;
}
class Foo
{
};
Foo *pFoo = new Foo();
delete pFoo;
Foo *pFooArray = new Foo[10]();
delete[] pFoo;
std::auto_ptr<Foo> a(new Foo);
std::auto_ptr<Foo> b;
b = a; // a Foo
std::vector<std::auto_ptr<Foo>> v;
v.push_back(b); // error C2558: class 'std::auto_ptr<_Ty>' : no copy constructor available or copy constructor is declared 'explicit'
volatile int i = 1; // , .
while (1)
{
if(i == 1)
{
// - i
}
}
// volatile , - :
if(i == 1) // i , ,
{
while (1)
{
// - i
}
}
Source: https://habr.com/ru/post/117996/
All Articles