class Context
{
private:
StrategyInterface * strategy_;
public:
explicit Context(StrategyInterface *strategy):strategy_(strategy)
{
}
void set_strategy(StrategyInterface *strategy)
{
strategy_ = strategy;
}
void execute() const
{
strategy_->execute();
}
};
Run Code Online (Sandbox Code Playgroud)
为什么在Context的构造函数中使用explicit是一个好习惯?
谢谢
可能重复:
是否值得在析构函数中设置指向NULL的指针?
NULL在析构函数中设置指针(分配堆内存)是没有意义的吗?
class SampleClass
{
public:
SampleClass( int Init = 0 )
{
Value = new int( Init );
}
~SampleClass( void )
{
delete Value;
Value = NULL; // Is this pointless?
}
int *Value;
};
Run Code Online (Sandbox Code Playgroud)
关于课程的主题,我explicit什么时候应该使用关键字?
假设我们有一个类String:
class String {
public:
String (int n);
String(const char *p);
}
Run Code Online (Sandbox Code Playgroud)
如果我们尝试将会发生什么:
String mystring='x';
Run Code Online (Sandbox Code Playgroud)
这里写的是char'x'将转换为int并将调用String(int)构造函数.但是,我不明白.
首先,'x'如何转换为int?我可以想象"3"将被转换为3"x"将被转换为什么?其次,我们在课堂上有两个构造函数.第一个构造函数接受一个int类型的参数,另一个构造函数接受一个指向char变量的指针作为参数.现在我们尝试调用不存在将char作为参数的构造函数.所以,我们将char转换为整数,但为什么我们不尝试将char转换为指向char的指针然后使用第二个构造函数?
我做了一些关于定义显式构造函数的研究(link1,link2,link3,link4,link5).
但对我来说,为什么std :: list和std :: iterator单个参数构造函数定义为显式并且在实际情况下这个定义可能有用的原因仍然不明显.能否请一些例子来说明这个定义有助于避免错误.谢谢
explicit list(const _Alloc& _Al)
: _Mybase(_Al)
{ // construct empty list, allocator
}
explicit back_insert_iterator(_Container& _Cont)
: container(_STD addressof(_Cont))
{ // construct with container
}
Run Code Online (Sandbox Code Playgroud) 我正在阅读Effective C++(Scott Meyers),并在尝试编译本书中的以下代码时得到错误"与运算符*不匹配":
rational.h
class rational
{
private:
int num;
int den;
public:
rational(int n = 0, int d = 1);
int getNum() const {return num;}
int getDen() const {return den;}
};
Run Code Online (Sandbox Code Playgroud)
rational.cpp
#include "rational.h"
rational::rational(int n,
int d)
:num(n),
den(d)
{}
const rational operator*(const rational &lhs,
const rational &rhs)
{
return rational( lhs.getNum()*rhs.getNum(),
lhs.getDen()*rhs.getDen() );
}
Run Code Online (Sandbox Code Playgroud)
main.cpp中
#include "rational.h"
int main()
{
rational r1(1,2);
rational r2;
r2 = 2*r1;
r2 = r1*3;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
有人可以解释为什么会这样吗?
我从另一个关于碎片化的问题中借用了这个,但我并不为此烦恼.我更担心的是我根本不理解这个功能.在类型和数据生命周期方面:(
相同的数据由std :: vector(具有内部元数据的动态aray类型),指向其中的字符串数据的指针(返回参数)和声明的返回类型(std:string)表示.
问题:当std :: vector将被销毁时,数据如何安全地从函数中移出?它被隐式复制了吗?向量中的char的动态数组是否已从向量'分离'并作为std :: string类型返回,因此不需要批量复制?有时,我认为C++和std库试图让我...
我已经使用C++了一段时间,但像这样的东西我的'ed in.
std::string TestFragmentation()
{
std::vector<char> buffer(500);
SomeCApiFunction( &buffer[0], buffer.size() ); // Sets buffer to null-terminated string data
return &buffer[0];
}
Run Code Online (Sandbox Code Playgroud) 由于各种无聊的原因,我需要一个盒装的int类,它主要作为一个int,但是是一个继承自base的类,因此它可以与对象层次结构的其他部分一起工作.我包含了一个构造函数,它接受一个int,以及一个int cast,所以我可以很容易地将我的盒装int与代码中的常规int混合.但是,我看到一个非常奇怪的行为,我无法弄清楚:当我从函数返回我的盒装int时,我希望它使用我的复制构造函数来引用另一个BoxedInt.但是,它将我的盒装int转换为int,然后使用我的int构造函数.这会导致问题,因为在我的实际代码库中,在这种情况下我还想要复制其他基类属性,并且通过获取此强制转换/构造函数路径它们会丢失.这是有问题的代码:
class BoxedInt
{
private:
int m_int;
public:
BoxedInt():m_int(0)
{
trace(L"Constructed with nothing");
}
BoxedInt(int val):m_int(val)
{
trace(L"Constructed with int");
}
BoxedInt(BoxedInt& val)
{
trace(L"Constructed with reference");
m_int = val.m_int;
}
operator int()
{
trace(L"Cast to int");
return m_int;
}
};
BoxedInt funky()
{
BoxedInt TempInt = 1;
return TempInt;
}
int main(int argc, char* argv[])
{
trace(L"Start");
BoxedInt test1 = 1;
trace(L"Copying");
BoxedInt test2 = test1;
trace(L"Assigning from return value");
BoxedInt test3 = funky();
trace(L"Done");
return 0;
} …Run Code Online (Sandbox Code Playgroud) 我使用vector的length和value构造函数创建了一个名为xor_funcs的bitpacked向量的向量.这是失败的测试:
TEST(vectorInit, size3) {
const xor_func temp{false, {0,0,0}};
vector<xor_func> arr{3, temp};
for(xor_func& f : arr) {
EXPECT_EQ(3, f.size()) << f;
}
for(int i = 0; i < 3; i++) {
ASSERT_EQ(3, arr[i].size()) << "for i=" << i;
arr[i].set(i);
}
}
Run Code Online (Sandbox Code Playgroud)
似乎size()调用正在访问未初始化的内存,对于长度为3或更长的向量,而不是大小为2的向量.Valgrind确认内存最近没有堆栈,malloc'd或free'd.
在xor_func被定义为这样的:
class xor_func {
private:
boost::dynamic_bitset<> bitset;
bool negated;
public:
xor_func(const bool neg, const std::initializer_list<int> lst);
// That's defined in cpp
xor_func(const size_t size) : bitset(size), negated(false) {}
// Disallow the trivial constructor, since I …Run Code Online (Sandbox Code Playgroud) 有没有办法让一个零大小的类型只能隐式构造?
用例是通过大括号语法来防止结构的某些公共成员被初始化:
class Barrier { ... };
struct Foo {
int user_sets;
int* this_to;
Barrier _bar;
int *must_be_zero_init_by_linker;
};
Foo foo = {1}; // ok
Foo bar = {1, nullptr}; // ok
Foo baz = {1, nullptr, {}}; // must error
Run Code Online (Sandbox Code Playgroud)
编辑:另一个约束:Foo对象必须是链接器初始化的,因此它不能定义构造函数或私有成员.
我想使用此代码(源代码)在opencv中查找基本矩阵。
\n\nint point_count = 100;\n vector<Point2f> points1(point_count);\n vector<Point2f> points2(point_count);\n\n // initialize the points here ... */\n for( int i = 0; i < point_count; i++ )\n {\n points1[i] = ...;\n points2[i] = ...;\n }\n\n Mat fundamental_matrix =\n findFundamentalMat(points1, points2, FM_RANSAC, 3, 0.99);\nRun Code Online (Sandbox Code Playgroud)\n\n但我不知道如何在for循环中初始化points1、point2。\n我尝试过使用:
\n\npoints1[i] = 10.0;\npoints1[i] = (10.0, 20.0);\npoints1[i] = {10.0, 20.0};\nRun Code Online (Sandbox Code Playgroud)\n\n但出现这样的错误
\n\nno match for \xe2\x80\x98operator=\xe2\x80\x99 (operand types are \xe2\x80\x98cv::Point_<float>\xe2\x80\x99 and \xe2\x80\x98double\xe2\x80\x99)\nRun Code Online (Sandbox Code Playgroud)\n\n在所有这些中。
\n