在C++中,我知道有两种方法可以重载.我们可以在内部(如类a)或外部(如类b)重载它.但是,问题是,这两者在编译时或运行时是否存在差异?
class a
{
public:
int x;
a operator+(a p) // operator is overloaded inside class
{
a temp;
temp.x = x;
temp.x = p.x;
return temp;
}
};
class b
{
public:
friend b operator+(b, b);
int x;
};
b operator+(b p1, b p2) // operator is overloaded outside class
{
p1.x += p2.x;
return p1;
}
Run Code Online (Sandbox Code Playgroud) 所以,我听说C++模板不应该分为标题(.h)和源(.cpp)文件.
例如,像这样的模板:
template <class T>
class J
{
T something;
};
Run Code Online (Sandbox Code Playgroud)
这是真的?为什么会这样?
如果因为这个我必须将声明和实现放在同一个文件中,我应该把它放在.h文件或.cpp文件中吗?
为什么std::rotate比cplusplus.com描述的等效功能快得多?
cplusplus.com的实施:
template <class ForwardIterator>
void rotate (ForwardIterator first, ForwardIterator middle, ForwardIterator last)
{
ForwardIterator next= middle;
while (first != next)
{
swap (*first++, *next++);
if(next == last)
next= middle;
else if (first==middle)
middle= next;
}
}
Run Code Online (Sandbox Code Playgroud)
我有两个完全相同的插入排序算法,除了一个使用std::rotate,一个使用cplusplus.com的等效函数.我正在设置它们用1000个int元素排序1000个向量.使用的排序std::rotate需要0.376秒,而另一个需要8.181秒.
为什么是这样?我不打算尝试做出比STL功能更好的东西,但我仍然很好奇.
std::unordered_map::emplace和std::unordered_map::insertC++有什么区别?
我有一个循环,使用以下方法读取文件中的每一行getline():
istream is;
string line;
while (!getline(is, line).eof())
{
// ...
}
Run Code Online (Sandbox Code Playgroud)
我注意到这样调用getline()似乎也有效:
while (getline(is, line))
Run Code Online (Sandbox Code Playgroud)
这里发生了什么?getline()返回流引用.是以某种方式转换为指针?这实际上是一种很好的做法还是我应该坚持第一种形式?
我理解在C++ 中使用new反对的好处malloc.但是,对于特定的情况下,如原始数据类型(非阵列) - int,float等等,是它更快地使用malloc比new?
虽然,new如果我们分配一个数组以便我们可以使用,它总是建议甚至用于原语delete[].
但对于非数组分配,我认为不会有任何构造函数调用int?因为,new运算符分配内存,检查它是否已分配,然后调用构造函数.但只为元非阵列堆分配,是它更好地使用malloc比new?
请指教.
我试图访问一个C++类并从.c文件中调用它的方法.
我谷歌这个话题,找到这个http://developers.sun.com/solaris/articles/mixing.html
它说:
您可以用
extern "C"C++ 编写访问类M对象的函数,并从C代码中调用它们.
这是一个用于调用成员函数的C++函数foo:
extern "C" int call_M_foo(M* m, int i) { return m->foo(i); }
Run Code Online (Sandbox Code Playgroud)
我的问题是我在哪里放线?在我的C++ .h文件中?还是C .h档?
它继续说:
以下是使用类的C代码示例M:
struct M; // you can supply only an incomplete declaration
int call_M_foo(struct M*, int); // declare the wrapper function
int f(struct M* p, int j) // now you can call M::foo
{
return call_M_foo(p, j);
}
Run Code Online (Sandbox Code Playgroud)
但是我如何/在哪里创建M我的C文件中的类?我在哪里放上面的代码?C .h档?C++ .h文件?还是C .c …
为什么main()用户定义了功能?
我什么时候会使用void main()和int main()?
我试图GetSection从注入的配置调用Startup.cs.值是null,而indexer具体部分值返回non-null值.在我看来这个GetSection方法背后的错误或我错了吗?
appsettings.json:
{"MyConfig":{"ConfigA":"valueA","ConfigB":"valueB"}}
Program.cs中:
public static void Main(string[] args)
{
var host = BuildWebHost(args);
host.Run();
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.Build();
Run Code Online (Sandbox Code Playgroud)
Startup.cs:
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
} …Run Code Online (Sandbox Code Playgroud) 在Modern Effective C++中,"Iterm 19:std::shared_ptr用于共享所有权资源管理.",第133-134页,它说:
std :: shared_ptr支持对单个对象有意义的派生到基指针转换,但是当应用于数组时,它会在类型系统中打开.(因此,std :: unique_ptr API禁止此类转换.)
"类型系统中的开孔"是什么意思?
为什么std::unique_ptr<T[]>API会禁止派生到基指针转换?
它怎么能禁止转换呢?