Skip to content

Instantly share code, notes, and snippets.

@vasalf
Created December 6, 2019 22:21
Show Gist options
  • Save vasalf/b686a94eb06334a5e8f10eb072b6df13 to your computer and use it in GitHub Desktop.
Save vasalf/b686a94eb06334a5e8f10eb072b6df13 to your computer and use it in GitHub Desktop.
Практика 11
#include <iostream>
struct point {
int x, y;
point() {
std::cout << "Construct " << this << std::endl;
}
~point() {
std::cout << "Destruct " << this << std::endl;
}
};
class auto_ptr {
public:
auto_ptr()
: ptr_(new point)
{}
auto_ptr(const auto_ptr&) = delete;
~auto_ptr() {
delete ptr_;
}
// auto_ptr p;
// *p = point {1, 2};
point& operator*() {
return *ptr_;
}
point* operator->() {
return ptr_;
}
private:
point *ptr_;
};
void foo(auto_ptr& p) {
p->x = 1;
}
int main() {
auto_ptr au;
foo(au);
}
#include <cstdint>
#include <cstddef>
#include <iostream>
#include <iomanip>
class Bitset {
public:
class BitReference {
public:
BitReference(std::uint64_t& where, int i)
: where_(where)
, i_(i)
{}
// Bitset::BitReference b = bs[i];
// std::cout << (bool) b;
operator bool() const {
return (where_ >> i_) & 1;
}
BitReference& operator=(bool b) {
where_ &= ~(1LL << i_);
where_ |= (std::uint64_t)b << i_;
return *this;
}
private:
std::uint64_t& where_;
int i_;
};
Bitset() : value_(0) {}
bool operator[](std::size_t i) const {
return (value_ >> i) & 1;
}
// b[i] = true;
Bitset::BitReference operator[](std::size_t i) {
return BitReference(value_, i);
}
private:
std::uint64_t value_;
};
int main() {
Bitset b;
b[1] = true;
std::cout << std::boolalpha << b[0] << " " << b[1] << std::endl;
}
#include <iostream>
class Shape {
public:
Shape(int x, int y)
: x_(x)
, y_(y)
{
std::cout << "Construct Shape " << this << std::endl;
}
~Shape() {
std::cout << "Destruct Shape " << this << std::endl;
}
int get_x() {
return x_;
}
int get_y() {
return y_;
}
private:
int x_, y_;
};
class Circle : public Shape {
public:
Circle(int x, int y, int r)
: Shape(x, y)
, r_(r)
{
std::cout << "Construct Circle " << this << std::endl;
}
~Circle() {
std::cout << "Desturct Circle " << this << std::endl;
}
private:
int r_;
};
int main() {
Circle c(1, 2, 3);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment