<iostream.h>
использовать <iostream>
. <stdio.h>
, в принципе, работает, но рекомендуется брать <cstdio>
.std
. То есть: std::cout
, std::endl
, и т.д. Либо, как предложил D' Normalization, using namespace std;
. class Father {};
class Son : private Father {};
int main()
{
Son son;
Father& q = son;
}
#include <iostream>
class Callback {
public:
virtual void act(int x) = 0;
};
void generateFibonacci(int n, Callback& callback) {
int x1 = 0, x2 = 1;
for (int i = 0; i < n; ++i) {
callback.act(x2);
int x3 = x1 + x2;
x1 = x2;
x2 = x3;
}
}
class FileWriter : private Callback {
public:
FileWriter(std::ostream& aOs, int aN) : os(aOs), n(aN) {}
void run() { generateFibonacci(n, *this); }
private:
std::ostream& os;
const int n;
void act(int x) override { os << x << std::endl; }
};
using namespace std;
int main()
{
FileWriter wri(std::cerr, 10);
wri.run();
}
// HEADER
class WebMaster {
public:
WebMaster();
bool connect_open();
private:
static EthernetClient client;
}
// CPP
EthernetClient WebMaster::client;
class WebMaster {
public:
WebMaster();
bool connect_open();
private:
EthernetClient client;
}
// HEADER
class WebMaster {
public:
WebMaster(EthernetClient& aClient);
bool connect_open();
private:
EthernetClient& client;
}
// CPP
WebMaster::WebMaster(EthernetClient& aClient) : client(aClient) {}
...
EthernetClient client;
WebMaster webmaster(client);
<vector>
— что-то не закрыто (пространство имён, функция и т.д.)void x() {
#include <vector>
};
QString
и QDatabase
.const T &QVector::at(int i) const
void foo()
{
void* a[1];
a[1] = (void*)&bar;
}
-LC:\Qt\curl-7.49.0-win32-mingw\lib -lcurldll
win32|win64: LIBS += -lws2_32 -lshlwapi -lodbc32 -lpsapi -lcomdlg32 \
-L$$PWD/../../Common/cURL/lib/ -lcurldll -lole32 \
-loleaut32
data
в реализации item<Edge>
) не имеет конструктора по умолчанию. Есть три пути.class Edge : public EdgeBase
{
public:
Edge (Point* firstPoint, Point* secondPoint) { init(firstPoint, secondPoint); }
Edge () { init(NULL, NULL); }
void init (Point* firstPoint, Point* secondPoint)
{
this->firstPoint = firstPoint;
this->secondPoint = secondPoint;
}
private:
Point* firstPoint;
Point* secondPoint;
};
#include <iostream>
class ImmutableInt
{
public:
explicit ImmutableInt(int x) : fData(x) {}
int data() const { return fData; }
private:
int fData;
};
template <class Payload>
class ListItem
{
public:
Payload payload;
ListItem* next;
ListItem() : next(NULL) {}
ListItem(const Payload& x) : payload(x), next(NULL) {}
};
// Так писать нельзя — эта конструкция расшаблонивает все функции,
// а конструктора по умолчанию как не было, так и нет!
//template class ListItem<ImmutableInt>;
int main()
{
ListItem<ImmutableInt> li(ImmutableInt(42));
std::cout << li.payload.data() << std::endl;
return 0;
}
#include <iostream>
class ImmutableInt
{
public:
explicit ImmutableInt(int x) : fData(x) {}
int data() const { return fData; }
private:
int fData;
};
template <class Payload>
class ListItem
{
public:
Payload payload;
ListItem* next = nullptr;
template<class... Args>ListItem(Args... args) : payload(args...) {}
};
int main()
{
ListItem<ImmutableInt> li(42);
std::cout << li.payload.data() << std::endl;
return 0;
}
explicit
не надо, всё-таки отметил — просто чтобы показать, что во втором случае мы передаём параметром ImmutableInt<42>, а в третьем — 42.template<typename type_t>
struct item
{
item* next;
type_t data;
};
bool IsEnded(const Query &aVar) { return (aVar.id == NO_ID); }
class Object {
public:
virtual ~Object() {}
};
class List { // interface
protected:
virtual Object& getAt(size_t i) = 0;
public:
virtual size_t size() const = 0;
inline Object& at(size_t i) { return getAt(i); }
inline const Object& at(size_t i) const
{ return const_cast<List*>(this)->getAt(i); }
virtual ~List() {}
};
class ListCallback { // interface
public:
virtual void act(size_t index, Object& object) = 0;
virtual ~ListCallback() {}
};
class ListConstCallback { // interface
public:
virtual void act(size_t index, const Object& object) = 0;
virtual ~ListConstCallback() {}
};
class List2 { // interface
public:
virtual size_t size() const = 0;
virtual void enumerate(ListCallback& body) = 0;
virtual void enumerate(ListConstCallback& body) const = 0;
};
void DynArray::enumerate(ListConstCallback& body) const {
size_t sz = size();
for (size_t i = 0; i < sz; ++i)
body(i, operator[](i));
}
class VirtualListIterator { // interface
public:
virtual bool next() = 0;
virtual Object& value() = 0;
virtual ~VirtualListIterator() {}
};
class ListIterator { // Назван так для красоты, по сути это умный указатель
private:
VirtualListIterator* ptr;
// Запретим копирование и op=, но при желании можно реализовать и их
ListIterator(ListIterator&) {}
void operator=(const ListIterator&) {}
public:
ListIterator() : ptr(NULL) {}
ListIterator(VirtualListIterator* value) : ptr(value) {}
~ListIterator() { delete ptr; }
bool next() { return ptr->next(); }
Object* operator->() const { return &ptr->value(); }
Object& operator*() const { return ptr->value(); }
};
class List3 { // interface
public:
virtual size_t size() const = 0;
virtual VirtualListIterator* enumerate() = 0;
};
class Number : public Object {
public:
int value;
};
// И пользоваться этим так...
void doSmth(List3& aList) {
ListIterator it(aList.enumerate());
while (it.next()) {
std::cout << dynamic_cast<Number&>(*it).value << std::endl;
}
}
#include <iostream>
#include <vector>
class Test {
int k;
public:
Test(int _i) {k = _i; }
void put_k(int i) {k = i; }
int get_k() {return k; }
};
struct TestWrapper {
Test payload;
TestWrapper() : payload(0) {}
TestWrapper(int i) : payload(i) {}
TestWrapper(const Test& i) : payload(i) {}
};
int main()
{
Test test(10);
std::vector<TestWrapper> v;
v.push_back(test);
std::cout << v[0].payload.get_k() << std::endl;
return 0;
}
#include <iostream>
#include <vector>
#include <memory>
class Test {
int k;
public:
Test(int _i) {k = _i; }
Test& operator=(const Test&) = delete;
Test& operator=(Test&&) = delete;
void put_k(int i) {k = i; }
int get_k() {return k; }
};
int main()
{
std::vector<std::unique_ptr<Test>> v;
v.push_back(std::unique_ptr<Test>(new Test(10)));
std::cout << v[0]->get_k() << std::endl;
return 0;
}
(*root)->next.push_back(node);
bool Syntax::On_BG(Node * root)
bool Syntax::On_BG(Node & root)