#include<iostream>
#include <string>
#include <memory>
#include <functional>
#include <regex>
class Quote {
public:
virtual void sayHello() { std::cout << "Hello from base" << std::endl; }
virtual void sayHi() { std::cout << "Hi from base" << std::endl; }
virtual void sayHappy() { std::cout << "Happy from base" << std::endl; }
void sayHello(int i, int j) { std::cout << "i and j" << std::endl; }
//std::string isbn() const;
//virtual double net_price(std::size_t n) const;
};
class DD : public Quote {
public:
using Quote::sayHello;
//implictly overide corresponding virtual functions of base class, though not explicitly them as virtual functions
void sayHappy(int i) { std::cout << "Happy from derived class" << std::endl; }
virtual void sayHello(int i) { std::cout << "Hello from derived class" << std::endl; }
void sayHi() { std::cout << "Hi from derived class" << std::endl; }
void sayHello(int i,int j, int h, int k) { std::cout << "I am no-virtual function" << std::endl; }
};
class Ddd : public DD {
virtual void sayHello() { std::cout << "Hello from Ddd class" << std::endl; }
};
class testRvalue {
public:
void sayVirtual();
testRvalue(const std::string &sr) : st(sr) { std::cout << "constructing from " << std::endl; }
testRvalue() {
std::cout << "default initialization" << std::endl;
}
testRvalue(const testRvalue &tr) : st(tr.st) {
std::cout << "constructing from copy constructor" << std::endl;
}
testRvalue(testRvalue &&tr) :st(std::move(tr.st)) {
std::cout << "construting from move construtor\n" << "value of the moved form obj" <<
tr.st << std::endl;
std::cout << "this value is " << st << std::endl;
}
private:
std::string st = "Hello world";
};
class test : public testRvalue {
public:
test() : j(po()), i(pop()), testRvalue("Hello dda") {}
static int pop() {
std::cout << "Hello pop" << std::endl;
return 0;
}
static int po() {
std::cout << "Hello po" << std::endl;
return 0;
}
private:
int i;
int j = (pop());
static int ij;
};
int test::ij = 0;
template <typename T>
bool compare(const T &a, const T &b);
int main() {
DD *pd;
pd = new Ddd;
pd->sayHello();
test te;
typedef void (**ppf)(void);
typedef void(*pf)(void);
DD dd;
dd.sayHello();
int *vTableAddress = (int*)(&dd);
std::cout << "sizeof vTable" << sizeof(*vTableAddress) << std::endl;
ppf vTable = ((ppf)(*vTableAddress));
pf say_hello = *vTable;
pf say_hi = *(vTable + 1);
pf say_happy = *(vTable + 2);
for (int i = 0; i < 10; i++) {
std::cout << *(vTable + i) << std::endl;
}
//依次调用 sayHello(), sayHi(), sayHappy()
say_hello();
say_hi();
say_happy();
return 0;
}