我无法弄清楚我正在阅读的书的下半部分究竟是什么.
//this function supposed to mimic the += operator
Sales_data& Sales_data::combine(const Sales_data &rhs)
{
units_sold += rhs.units_sold; // add the members of rhs into
revenue += rhs.revenue; // the members of ''this'' object
return *this; // return the object on which the function was called
}
int main()
{
//...sth sth
Sales_data total, trans;
//assuming both total and trans were also defined...
total.combine(trans);
//and here the book says:
//we do need to use this to access the object as a …Run Code Online (Sandbox Code Playgroud) 我是C++的新手.为什么我们不能迭代int*,&这里有什么用处,以及这个嵌套的基于范围的表现如何深入?
int arr[10][3];
for (auto &i : arr)
{
for (auto j : i)
{
//sth
}
}
Run Code Online (Sandbox Code Playgroud) sealed当当前类从超类继承时,为什么我们要在方法或属性上使用关键字?假设我们创建了一个类,并且倾向于将其一个或多个方法公开给对象用户,但根本不让它被继承并用于sealed解决问题。那么,为什么不呢?仅密封当前继承类的方法或属性的原因是什么?
这是什么问题copy(这是一个通用函数)?我无法运行代码.
vector<int> a(10, 2);
vector<int> b(a.size());
auto ret = copy(a.begin(), a.end(), b);
for (auto i : b) cout << i << endl;
Run Code Online (Sandbox Code Playgroud)
这是编译后的输出:
1>------ Build started: Project: Project1, Configuration: Debug Win32 ------
1> MainEx.cpp
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(2176): error C4996: 'std::_Copy_impl': Function call with parameters that may be unsafe - this call relies on the caller to check that the passed values are correct. To disable this warning, use -D_SCL_SECURE_NO_WARNINGS. See documentation on how to use …Run Code Online (Sandbox Code Playgroud) 正如我在这里和那里读到的那样,new在C#中添加或删除调用列表中的方法(有或没有委托对象的关键字)是完全相同的,并产生相同的IL.例如,请参阅此示例:在C#中使用委托的两种方式(使用new关键字和不使用)具有此代码片段有什么区别:
this.button1.Click += new System.EventHandler(this.button1_Click);
this.button1.Click += this.button1_Click;
Run Code Online (Sandbox Code Playgroud)
这两者之间没有区别.但是当我用作delegate传递方法参数时,我意外地遇到了使用此代码观察异常输出:
private delegate void TextPrinter(string text);
private static TextPrinter _handler;
static void Main(string[] args)
{
TextPrinter myPrinter = PushMessage;
RegisterHandler(PushMessage);
UnRegisterHandler(PushMessage);
InvokePrinter("hello");
}
private static void RegisterHandler(TextPrinter methods)
{
_handler += methods;
}
private static void UnRegisterHandler(TextPrinter methods)
{
/* first routine >> */_handler -= new TextPrinter(methods);
/* second routine >> */ //_handler -= methods;
}
private static void InvokePrinter(string message)
{
_handler(message);
} …Run Code Online (Sandbox Code Playgroud)