甲派生类对象可以被分配到一个基类对象在C++中.
Derived d;
Base b = d; // It's Ok
Run Code Online (Sandbox Code Playgroud)
但为什么不能将基类对象分配给派生类对象?
Base b;
Derived d = b; //Not Ok. Compiler give an error
Run Code Online (Sandbox Code Playgroud)
编辑:
对不起,这个问题实际上是在面试时提出来的.
这是我正在使用的代码:
#include <stdio.h>
#include <stdlib.h>
int main() {
int *arr;
int sz = 100000;
arr = (int *)malloc(sz * sizeof(int));
int i;
for (i = 0; i < sz; ++i) {
if (arr[i] != 0) {
printf("OK\n");
break;
}
}
free(arr);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
该程序不打印OK.malloc不应该将分配的内存初始化为零.为什么会这样?
C #pragma和_Pragma()C 之间有什么区别?
句法:
#pragma arg
Run Code Online (Sandbox Code Playgroud)
和
_Pragma(arg)
Run Code Online (Sandbox Code Playgroud)
我应该什么时候使用_Pragma(arg)?
我已经阅读了有关std::unordered_map使用cppreference的 C++ 17中的演绎指南.
然后尝试运行以下示例,该示例是从cppreference复制的.
#include <unordered_map>
int main() {
// std::unordered_map m1 = {{"foo", 1}, {"bar", 2}}; // Error: braced-init-list has no type
// cannot deduce pair<const Key, T> from
// {"foo", 1} or {"bar", 2}
std::unordered_map m1 = std::initializer_list<
std::pair<char const* const, int>>({{"foo", 2}, {"bar", 3}}); // guide #2
std::unordered_map m2(m1.begin(), m1.end()); // guide #1
}
Run Code Online (Sandbox Code Playgroud)
但是,编译器会出错.
main.cpp: In function 'int main()':
main.cpp:7:84: error: class template argument deduction failed:
std::pair<char const* const, int>>({{"foo", 2}, {"bar", 3}}); …Run Code Online (Sandbox Code Playgroud) 我有一小段代码分配nullptr给bool类型.
#include <iostream>
int main()
{
bool b = nullptr;
std::cout << b;
}
Run Code Online (Sandbox Code Playgroud)
在clang 3.8.0工作正常.它给出了一个输出0.Clang Demo
但是g ++ 5.4.0给出了一个错误:
source_file.cpp: In function ‘int main()’:
source_file.cpp:5:18: error: converting to ‘bool’ from ‘std::nullptr_t’ requires direct-initialization [-fpermissive]
bool b = nullptr;
Run Code Online (Sandbox Code Playgroud)
哪个编译器正确?
根据我的理解,我将变量的地址传递a给函数int ffx1.
在那之后,这条线究竟p = (int[2]){*p};意味着什么?
int ffx1(int * p)
{
p = (int[2]){*p};
return(p[1]);
}
int main()
{
int a = 1;
a = ffx1(&a);
printf("%d", a);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我std::is_pointer在C++中读到过.
然后我写了程序并检查是否T是指针类型使用std::is_pointer.
#include <iostream>
int main()
{
std::cout<<std::boolalpha;
std::cout<<"char : " <<std::is_pointer<char>::value<<std::endl; // OK
std::cout<<"char * : "<<std::is_pointer<char *>::value<<std::endl; // OK
std::cout<<"char ** : "<<std::is_pointer<char **>::value<<std::endl; // OK
std::cout<<"char *** : "<<std::is_pointer<char ***>::value<<std::endl; // OK
std::cout<<"std::nullptr_t : "<<std::is_pointer<std::nullptr_t>::value<<std::endl; // Not ok, Why false??
}
Run Code Online (Sandbox Code Playgroud)
输出:[Wandbox演示]
char : false
char * : true
char ** : true
char *** : true
std::nullptr_t : false
Run Code Online (Sandbox Code Playgroud)
为什么std::is_pointer<std::nullptr_t>::value等于false?
在C语言中,如果我们写这个:
for(int i = 0; i < 7; i++)
{
// for loop Body
}
Run Code Online (Sandbox Code Playgroud)
变量的范围i在for循环体内.没关系.
但是,如果我这样写:
for(int i = 0; i < 7; i++)
{
long int i = 1; // Redeclaration of i
}
Run Code Online (Sandbox Code Playgroud)
这里,变量i在循环体内再次声明,但它成功编译并在C中运行.
但是,在C++中,编译器会"重新声明'long int i'"错误.
那么,为什么C编译器没有给出重新声明错误?这是编译器错误吗?
even after cleaning with apt-get clean it says there are broken packages. sudo dpkg -l | grep ^..r as mentioned in [1] returns nothing.
$ sudo apt-get install build-essential
Reading package lists... Done
Building dependency tree
Reading state information... Done
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The …Run Code Online (Sandbox Code Playgroud) c++ ×5
c ×4
c++11 ×2
g++ ×2
nullptr ×2
pointers ×2
apt ×1
boolean ×1
c++17 ×1
clang ×1
declaration ×1
for-loop ×1
function ×1
gcc ×1
git ×1
git-bash ×1
git-checkout ×1
inheritance ×1
installation ×1
macros ×1
malloc ×1
object ×1
template-argument-deduction ×1
ubuntu ×1
ubuntu-20.04 ×1