这是代码:
struct Biology
{
Biology() { cout << "Biology CTOR" << endl; }
};
struct Human : Biology
{
Human() { cout << "Human CTOR" << endl; }
};
struct Animal : virtual Biology
{
Animal() { cout << "Animal CTOR" << endl; }
};
struct Centaur : Human, Animal
{
Centaur() { cout << "Centaur CTOR" << endl; }
};
int main()
{
Centaur c;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
此代码打印:
Biology CTOR
Biology CTOR
Human CTOR
Animal CTOR
Centaur CTOR …Run Code Online (Sandbox Code Playgroud) c++ inheritance constructor multiple-inheritance virtual-inheritance
看看这段代码:
class Foo
{
public:
string name;
Foo(string n) : name{n}
{
cout << "CTOR (" << name << ")" << endl;
}
Foo(Foo&& moved)
{
cout << "MOVE CTOR (moving " << moved.name << " into -> " << name << ")" << endl;
name = moved.name + " ###";
}
~Foo()
{
cout << "DTOR of " << name << endl;
}
};
Foo f()
{
return Foo("Hello");
}
int main()
{
Foo myObject = f();
cout << …Run Code Online (Sandbox Code Playgroud) 基于此代码
struct Foo
{
Foo()
{
cout << "default ctor" << endl;
}
Foo(std::initializer_list<Foo> ilist)
{
cout << "initializer list" << endl;
}
Foo(const Foo& copy)
{
cout << "copy ctor" << endl;
}
};
int main()
{
Foo a;
Foo b(a);
// This calls the copy constructor again!
//Shouldn't this call the initializer_list constructor?
Foo c{b};
_getch();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出是:
默认ctor
复制ctor
复制ctor
在第三种情况下,我将b放入大括号初始化,它应该调用initializer_list <>构造函数.
相反,复制构造函数起带头作用.
你们有人会告诉我这是如何运作的,为什么?
让我们有一个名为Y的函数重载:
void Y(int& lvalue)
{ cout << "lvalue!" << endl; }
void Y(int&& rvalue)
{ cout << "rvalue!" << endl; }
Run Code Online (Sandbox Code Playgroud)
现在,让我们定义一个像std :: forward一样的模板函数
template<class T>
void f(T&& x)
{
Y( static_cast<T&&>(x) ); // Using static_cast<T&&>(x) like in std::forward
}
Run Code Online (Sandbox Code Playgroud)
现在看看main()
int main()
{
int i = 10;
f(i); // lvalue >> T = int&
f(10); // rvalue >> T = int&&
}
Run Code Online (Sandbox Code Playgroud)
正如预期的那样,输出是
lvalue!
rvalue!
Run Code Online (Sandbox Code Playgroud)
现在回到模板功能f()并替换static_cast<T&&>(x)为static_cast<T>(x).让我们看看输出:
lvalue!
rvalue!
Run Code Online (Sandbox Code Playgroud)
一样的!为什么?如果它们是相同的,那么为什么std::forward<>从返回一个投x来 …
我一直试图这么做很多时间,但我做不到.这是我的问题:我有一个这样的网页:
<html>
<body>
<div id="my_div">
<span id="txt">HELLO WORLD</span>
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
现在我希望div"my_div"每隔X秒刷新一次,同时刷新其内容.如何使用AJAX或JQUERY或JAVASCRIPT执行此操作?
注意:我不需要刷新整个页面的脚本.注意(2):我刚试图通过谷歌找到一些东西,但我一无所获.
请帮我.我一直有这个问题.
先感谢您!
我这里有两段代码给你看.它们是两个类,每个类都提供一个Move Constructor和一个返回临时函数的函数.
我很困惑:在这两种情况下,我都定义了一个Move Constructor和一个返回临时的随机成员函数.但行为改变了,我的问题就是原因.
请注意,在以下示例中,运算符<<被重载以便打印列表(在第一种情况下)和双数据成员(在第二种情况下).
移动构造者得到了认可
template<typename T>
class GList
{
public:
GList() : il{ nullptr } {}
GList(const T& val) : il{ new Link<T>{ val,nullptr } } {}
GList(const GList<T>& copy) {}
GList(GList<T>&& move)
{
std::cout << "[List] Move constructor called" << std::endl;
// ... code ...
}
// HERE IS THE FUNCTION WHICH RETURNS A TEMPORARY!
GList<T> Reverse()
{
GList<T> result;
if (result.il == nullptr)
return *this;
...
...
...
return …Run Code Online (Sandbox Code Playgroud) cout << std::is_assignable<int*, std::nullptr_t>::value << endl;
cout << std::is_assignable<int*&, std::nullptr_t>::value << endl;
Run Code Online (Sandbox Code Playgroud)
输出为:0 1
我不明白为什么第一次检查返回false
我可以将nullptr分配给对指针的引用,但是我不能将它分配给原始指针?
这是相反的!
int* p = nullptr;
int*& pref = nullptr;
Run Code Online (Sandbox Code Playgroud)
正如预期的那样,第二个赋值标记了一个错误:
错误:不能非const结合左值参考类型的INT*到 右值类型的INT*
有人可以解释一下发生了什么吗?
这些之间有什么区别:
这个工作:
char* pEmpty = new char;
*pEmpty = 'x';
Run Code Online (Sandbox Code Playgroud)
但是,如果我尝试做:
char* pEmpty = NULL;
*pEmpty = 'x'; // <---- doesn't work!
Run Code Online (Sandbox Code Playgroud)
和:
char* pEmpty = "x"; // putting in double quotes works! why??
Run Code Online (Sandbox Code Playgroud)
编辑:谢谢你的所有意见:我纠正了它.它应该是pEmpty ='x',所以,这行甚至不编译:char pEmpty ='x'; 这行有效:char*pEmpty ="x"; //双引号.
看看这段代码
template<class T>
void print(T var)
{
std::cout << var << " ";
}
template<class... Args>
void Variadic(Args... args)
{
print(args...);
}
int main()
{
Variadic();
}
Run Code Online (Sandbox Code Playgroud)
当我编译它时说:
候选人:模板无效打印(T)
候选人期望1个参数,0提供
他是对的.实际上,我没有在参数包中提供任何参数.
但是,为什么这段代码会编译?
template<class T>
void print(T var)
{
std::cout << var << " ";
}
template<class... Args>
void Variadic(Args... args)
{
auto x = {0, (print(args), 0)...};
}
int main()
{
Variadic();
}
Run Code Online (Sandbox Code Playgroud)
我要做的第一件事就是将第一个0推入initializer_list <>
好的,现在让我们继续:编译器看到
(print(args), 0)...
Run Code Online (Sandbox Code Playgroud)
它试图调用print()...哦等待... 参数包是空的,print()函数有1个参数.
为什么它会评估auto x = {0};呢?
为什么编译器没有给我与以前完全相同的错误?
c++ templates metaprogramming template-function variadic-templates
根据这个问题Unable to print the value of nullptr on screen
我无法打印 nullptr 的值,因为它的类型为 nullptr_t,并且 std::cout 没有此类重载。
但是,如果你看看这个:
int* f()
{
return nullptr;
}
int main()
{
std::cout << f();
}
Run Code Online (Sandbox Code Playgroud)
输出是:
00000000
在这个问题Why does std::cout output gone完全消失后 NULL is sent to it他们讨论了一个不同的问题。
我只是想理解为什么std::cout 不能打印 nullptr,但当 nullptr由函数返回时它实际上可以。
我正在制作一个包含模板和类的动态数组.
这是我遇到问题的代码:
template<typename GType>
class GArray
{
GType* array_type = nullptr;
int size = 0;
public:
GArray(GType Size)
{
size = Size;
array_type = new GType[size];
for (int i = 0; i < size; i++)
array_type[i] = NULL;
}
void Push(GType Item)
{
size++;
GType* temp = new GType[size];
for (int i = 0; i < size-1; i++)
temp[i] = array_type[i];
temp[size] = Item;
delete[] array_type;
array_type = temp;
temp = nullptr;
}
GType& operator[] (int Index)
{
if (Index …Run Code Online (Sandbox Code Playgroud) c++ ×9
c++11 ×2
move ×2
nullptr ×2
templates ×2
ajax ×1
char ×1
constructor ×1
copy-elision ×1
cout ×1
deep-copy ×1
inheritance ×1
jquery ×1
pointers ×1
refresh ×1
rvalue ×1
static-cast ×1
type-traits ×1