由于 的构造函数std::shared_ptr
被标记为显式构造函数,因此 like 的表达式auto p = std::make_shared<int>(1); p = new int(6);
是错误的。
我的问题是为什么std::make_shared<int>(1); p = nullptr;
编译?
这是前面提到的代码片段:
#include <memory>
#include <iostream>
int main()
{
auto p = std::make_shared<int>(1);
//p = new int(6);
p = nullptr;
if(!p)
{
std::cout << "not accessable any more" << std::endl;
}
p.reset();
}
Run Code Online (Sandbox Code Playgroud)
修改复制构造函数的“ explicit ”关键字可能会导致问题。\n作为函数参数传递的对象特别容易受到这些问题的影响。
\n这是我的代码:
\n#include <iostream>\n#include <string>\n\nclass Pig{\npublic:\n std::string _name;\npublic:\n Pig(std::string n) : _name(n) {}\n //~Pig() = default;\n explicit Pig(const Pig &other) {\n std::cout << "copy ctor!" << std::endl;\n this->_name = other._name;\n }\n};\n\nvoid show(Pig p) {\n std::cout << p._name << std::endl;\n}\n\nint main() {\n Pig pig{std::string("hello")};\n show(Pig{pig}); // no pass\n // show(Pig(pig)); // no pass\n return 0;\n}\n
Run Code Online (Sandbox Code Playgroud)\n编译器版本:g++(Ubuntu 9.4.0-1ubuntu1~20.04.1)9.4.0。
\n上面提到的代码不能用c++14 或更低版本编译,\n但用c++17 及更高版本编译成功。
\n这是编译器的错误:
\ntest.cpp: In function \xe2\x80\x98int main()\xe2\x80\x99:\ntest.cpp:22:7: error: cannot …
Run Code Online (Sandbox Code Playgroud) 我有一个有效地采用字符串的方法.但是,我想要使用的字符串子集非常有限.我想把typedef'ing std :: string作为一些类,并将函数调用为显式.但是,我不确定这是否会奏效.想法?
可能重复:
是否值得在析构函数中设置指向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
什么时候应该使用关键字?
我有一个实现接口的类,其中一个方法叫做onClick.有没有办法实现接口想要的onClick,但是将其命名为其他东西?像(我正在做的):
public void AnyMethodNameIWant() implements Interface1.onClick
Run Code Online (Sandbox Code Playgroud)
我问的三个原因是:
如果这是一个基本上"坏"的问题,我很抱歉,因为我不熟悉Java.
我不想问这样一个普遍的问题,但下面的代码是显式模板专业化的练习.我一直收到错误:
c:\ users\***\documents\visual studio 2010\projects\template array\template array\array.h(49):error C2910:'Array :: {ctor}':无法显式专门化
#ifndef ARRAY_H
#define ARRAY_H
template <typename t>`
class Array
{
public:
Array(int);
int getSize()
{
return size;
}
void setSize(int s)
{
size = s;
}
void setArray(int place, t value)
{
myArray[place] = value;
}
t getArray(int place)
{
return myArray[place];
}
private:
int size;
t *myArray;
};
template<typename t>
Array<t>::Array(int s=10)
{
setSize(s);
myArray = new t[getSize()];
}
template<>
class Array<float>
{
public:
Array();
};
template<>
Array<float>::Array()
{ …
Run Code Online (Sandbox Code Playgroud) 如果模板成员是模板的唯一成员,并且它们共享模板的名称,则可以隐式引用模板成员:
template foo(int number)
{
immutable int foo = number;
}
void main()
{
writeln(foo!(123)); // Okay.
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我想明确地引用该成员呢?
writeln(foo!(123).foo); // Error: attempts to access the foo property of int.
Run Code Online (Sandbox Code Playgroud)
我没有充分的理由,但我觉得这一定是可能的.
我在"northwind"数据库上尝试这部分代码.但是我收到了DataBind错误
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
GetSpecificCustomer();
}
}
private void GetSpecificCustomer()
{
using (var ctx = new northwindContext())
{
var query = ctx.Customers.Include("CustomerID").Take(3);
grdEmployees.DataSource = query;
grdEmployees.DataBind(); =>NotSupportedException was unhandled by user code(Data binding directly to a store query (DbSet, DbQuery, DbSqlQuery) is not supported. Instead populate a DbSet with data, for example by calling Load on the DbSet, and then bind to local data. For WPF bind to DbSet.Local. For WinForms bind to …
Run Code Online (Sandbox Code Playgroud) 我尝试使用显式加载加载多级但发生错误:
"属性路径'Channel.Posts'不能用于导航属性.属性路径只能用于访问原始或复杂属性."
这是我的代码:
var listSubs;
using (var db = new GamePortalContext())
{
listSubs = db.Subscribers.Include("Channel").Where(o => o.User.Username == username.ToLower() && o.Channel.IsActive && o.Channel.IsPublic && o.Channel.Posts.Count(p => p.PublishTime <= DateTime.Now && p.IsActive && p.IsHot) > 0);
if (listSubs.Any())
{
listSubs = listSubs.OrderByDescending(o => o.Channel.ChannelTrack.LastPublishTime);
listSubs = (num == int.MinValue) ? listSubs : listSubs.Take(num);
foreach (var item in listSubs)
{
db.Entry(item).Collection(o => o.Channel.Posts).Query().Where(i => i.IsHot && i.IsActive && i.PublishTime <= DateTime.Now).Take(numpost).Load();
}
return listSubs.ToList();
}
else
{
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的帖子和频道实体 …
我想在python函数中定义2个变量,并将它们显式定义为float.但是,当我尝试在函数参数中定义它们时,它显示语法错误.
请帮我获得所需的输出.
这是代码:
def add(float (x) , float (y)) :
z = (x+y)
return (print ("The required Sum is: ", z))
add (5, 8)
Run Code Online (Sandbox Code Playgroud) explicit ×10
c++ ×5
templates ×2
c++11 ×1
class ×1
copy-elision ×1
d ×1
destructor ×1
elision ×1
function ×1
implicit ×1
interface ×1
java ×1
null ×1
python ×1
python-3.x ×1
rename ×1
shared-ptr ×1
typedef ×1