我试图理解这种行为,但似乎我没有.请看这段代码:
#include <iostream>
using namespace std;
class Base
{
public:
void operator=(const Base& rf)
{
cout << "base operator=" << endl;
this->y = rf.y;
}
int y;
Base() : y(100) { }
};
class Derived : public Base
{
public:
int x;
Derived() : x(100) { }
};
int main()
{
Derived test;
Derived test2;
test2.x = 0;
test2.y = 0;
test.operator=(test2); // operator auto-generated for derived class but...
cout << test.x << endl << test.y << endl;
cin.ignore();
return …Run Code Online (Sandbox Code Playgroud) 很清楚编译器会隐式创建一个构造函数,但是如果我们有这样的代码:
class A
{
public:
A(int = 0) {}
};
Run Code Online (Sandbox Code Playgroud)
此构造函数是默认值和转换运算符.
题:
编译器还会生成"空"默认构造函数A() {}吗?
int arr[5] = {0,1,2,3,4};
vector<int> vec;
Run Code Online (Sandbox Code Playgroud)
通常我们这样做:
vector<int> vec(arr, arr + sizeof(arr) / sizeof(int));
Run Code Online (Sandbox Code Playgroud)
但是如何仅使用arr的前3个值初始化矢量vec?另外我如何用中间3值初始化它?
我必须马上初始化它,不要push_back多行...
当我运行这个代码只是挂起for循环,你能解释为什么?
#include<iostream>
#include<random>
#include<ctime>
int main()
{
using std::cout;
using std::endl;
using std::cin;
using std::mt19937;
using std::minstd_rand;
using std::uniform_int;
using std::normal_distribution;
// engines
mt19937 rng;
minstd_rand gen;
// distributions
uniform_int<int> dist(0, 37);
normal_distribution<short> norm(4, 3);
// initializaiton
rng.seed(static_cast<unsigned int>(time(false)));
gen.seed(static_cast<unsigned short>(time(false)));
// generate numbers
for(int i = 0; i < 10; ++i)
std::cout << dist(rng) << " " << norm(gen) << endl; // This is as far as this code goes
cin.get();
return 0;
}
Run Code Online (Sandbox Code Playgroud) 假设这是函数之前的预处理器定义f():
#define write std::cout << "test";
write
void f()
{
//...
}
Run Code Online (Sandbox Code Playgroud)
这是该宏的结果:
std::cout << "test"
void f()
{
//...
}
Run Code Online (Sandbox Code Playgroud)
如何编写该宏以便它将跳过函数并在函数后面插入一些代码,以便结果如下所示:
std::cout << "test";
void f()
{
//...
}
std::cout << "test";
Run Code Online (Sandbox Code Playgroud)
你知道我的意思:跳过一些代码并插入多行的宏(或其他东西).
这是我项目的代码片段:
template<typename Second, typename First = const UINT64>
class Event : virtual public id_manager<>
{
friend class EventHandler;
typedef std::map<First, EventHandler> eventMap;
static eventMap mapper;
static eventMap StartMapping()
{
eventMap temp;
return temp;
}
public:
Event(){}
void operator+=(EventHandler _handler)
{
mapper[this->getID()] = _handler;
}
};
// INITIALIZATION FAILED HERE:
template<typename Second, typename First = const UINT64>
Event<Second, First>::eventMap Event<Second, First>::mapper(Event<Second, First>::StartMapping());
Run Code Online (Sandbox Code Playgroud)
这是Visual Studio 2010的输出错误:
警告1警告C4346:'Event :: eventMap':依赖名称不是类型c:\ users\admin\documents\visual studio 2010\projects\cppsystem\cppsystem\main.cpp 67错误2错误C2143:语法错误:缺失';' 在'Event :: mapper'c:\ users\admin\documents\visual studio 2010\projects\cppsystem\cppsystem\main.cpp之前67错误3错误C4430:缺少类型说明符 - 假定为int.注意:C++不支持default-int c:\ …
我希望有一个托管指针(唯一或共享),并能够使用新的内存重新分配它,并确保使用托管指针删除旧内存(因为它应该是).
struct MyStruct
{
MyStruct(signed int) { }
MyStruct(unsigned float) { }
};
std::unique_ptr<MyStruct> y(new MyStruct(-4)); // int instance
std::shared_ptr<MyStruct> x(new MyStruct(-5));
// do something with x or y
//...
// until now pointers worked as "int" next we want to change it to work as float
x = new MyStruct(4.443);
y = new MyStruct(3.22); // does not work
Run Code Online (Sandbox Code Playgroud)
我怎样才能以最干净的方式实现这一目标?
请原谅,但我不知道在短时间内给这个标题命名.
为什么我需要在标头中声明一个重载的运算符,以使其在此示例中起作用:
HEAD.H
#pragma once
namespace test {
class A {
public:
A() : x(0) {}
int x;
};
A& operator++(A& obj); //this is my question
}
Run Code Online (Sandbox Code Playgroud)
HEAD.CPP
#include "head.h"
namespace test {
A& operator++(A& obj) {
++obj.x;
return obj;
}
}
Run Code Online (Sandbox Code Playgroud)
main.cpp中
#include <iostream>
#include "head.h"
using namespace std;
using namespace test;
int main() {
A object;
++object; //this won't work if we delete declaration in a header
return 0;
}
Run Code Online (Sandbox Code Playgroud)
operator ++是在"head.cpp"中的命名空间中定义和声明的,那么为什么我需要在头文件中再次声明它呢?谢谢.
这是我的问题:
template<typename T>
class Outer
{
public:
template<typename U>
class Inner
{
private:
static int count;
};
static int code;
void print() const
{
std::cout << "generic";
}
};
template<>
template<>
class Outer<bool>::Inner<bool>
{
static int count;
};
template<>
template<>
int Outer<bool>::Inner<bool>::count = 4; // ERROR
Run Code Online (Sandbox Code Playgroud)
如何正确初始化?
我不了解有关如何使用make_shared和allocate_shared初始化共享数组和指针的boost文档:
shared_ptr<int> p_int(new int); // OK
shared_ptr<int> p_int2 = make_shared<int>(); // OK
shared_ptr<int> p_int3 = allocate_shared(int); // ??
shared_array<int> sh_arr(new int[30]); // OK
shared_array<int> sh_arr2 = make_shared<int[]>(30); // ??
shared_array<int> sh_arr3 = allocate_shared<int[]>(30); // ??
Run Code Online (Sandbox Code Playgroud)
我正在尝试学习正确的语法来初始化上述注释为的变量 // ??
c++ ×10
static ×2
templates ×2
boost ×1
c++11 ×1
class ×1
constructor ×1
inheritance ×1
macros ×1
namespaces ×1
random ×1
shared-ptr ×1
unique-ptr ×1
vector ×1