我已经定义了一个IntWrapper
类如下的类:
struct IntWrapper
{
protected:
int value;
public:
explicit IntWrapper() = default;
explicit IntWrapper(const int value) : value(value) {}
bool operator< (const IntWrapper rhs) const { return value < rhs.value; }
bool operator> (const IntWrapper rhs) const { return value > rhs.value; }
bool operator<=(const IntWrapper rhs) const { return value <= rhs.value; }
bool operator>=(const IntWrapper rhs) const { return value >= rhs.value; }
bool operator==(const IntWrapper rhs) const { return value == rhs.value; }
explicit operator …
Run Code Online (Sandbox Code Playgroud) 记下以下C++代码:
#include <iostream>
using std::cout;
int foo (const int);
int main ()
{
cout << foo(3);
}
int foo (int a)
{
a++;
return a;
}
Run Code Online (Sandbox Code Playgroud)
请注意,原型foo()
采用a const int
并且定义采用int
.这个编译没有任何错误......
为什么没有编译错误?
我正在运行 clang-tidy 8.0 并且收到警告:
constructor does not initialize these fields:
在模板化类上使用委托构造函数时。我想知道这是否是我应该压制的误报,或者我的代码是否确实是错误的。
有问题的示例代码是这样的:
template<typename T>
class A
{
public:
explicit A(const std::size_t size) :
data_(nullptr),
data_size_(size)
{
// ...
}
explicit A(const std::vector<T>& b) :
A(b.size())
{
// ...
}
private:
T* data_;
std::size_t data_size_;
};
Run Code Online (Sandbox Code Playgroud)
在此代码上运行 clang-tidy 时:
clang-tidy-8 --checks=* test.cpp
我得到,除其他外:
warning: constructor does not initialize these fields: data_ [cppcoreguidelines-pro-type-member-init]
explicit A(const std::vector<T>& b) : A(b.size()) {}
Run Code Online (Sandbox Code Playgroud)
但是,如果我从类中删除模板并使其成为普通类,则不会出现此类错误。
在模板化类上使用委托构造函数时,我是否遗漏了什么,或者这是 clang-tidy 中的错误?
谢谢!
有一个关于重载函数的问题.看看这段代码:
#include<iostream>
void fv(int){}
void fc(const int){}
void fvr(int&){}
void fcr(const int&){}
void fm(void(*fun)(const int))
{
std::cout << "Constant called" << std::endl;
}
//void fm(void(*fun)(int))
//{
// std::cout << "non Constant called" << std::endl;
//}
void fm(void(*fun)(const int&))
{
std::cout << "Constant ref called" << std::endl;
}
void fm(void(*fun)(int&))
{
std::cout << "non Constant ref called" << std::endl;
}
int main()
{
fm(&fc);
fm(&fv);
fm(&fvr);
fm(&fcr);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果取消注释void fm(void(*fun)(int))
函数,你会发现编译器不能通过函数上的指针静态地重载函数,该函数接受参数值和接受const值的函数上的指针.此外,如果您取消注释void(*fun)(const int)
并发表评论,void(*fun)(const int)
那么所有编译都是成功的.但是,如果我们使用引用它编译好.不明白为什么,你能解释一下吗?这是否意味着通过值和const值接受参数的函数指针是相同的类型?
UPD: …
尝试了stackeroverflow qn所以它让我思考为什么不重载该函数,我想出了一个稍微不同的代码,但它说该函数不能重载.我的问题是为什么?还是有另一种方式?
#include <iostream>
using std::cout;
class Test {
public:
Test(){ }
int foo (const int) const;
int foo (int );
};
int main ()
{
Test obj;
Test const obj1;
int variable=0;
do{
obj.foo(3); // Call the const function
obj.foo(variable); // Want to make it call the non const function
variable++;
usleep (2000000);
}while(1);
}
int Test::foo(int a)
{
cout<<"NON CONST"<<std::endl;
a++;
return a;
}
int Test::foo (const int a) const
{
cout<<"CONST"<<std::endl;
return a;
}
Run Code Online (Sandbox Code Playgroud) 一位C++专家告诉我,在派生类中用const改变函数参数类型会破坏虚拟调用机制.
我尝试了一个简单的程序(原谅非标准代码,纯粹是为了测试而编写),否则就证明了这一点.函数参数按const值改变不会破坏虚拟机制,
是否有任何原因和文档指出这种行为?
VS 2012编译器和最新的g ++编译器注意到了行为.
#include <iostream>
using namespace std;
class Base
{
public:
Base(){ cout<<"base"<<endl;}
virtual ~Base(){ cout<<"dest base"<<endl;}
virtual void test(const int x){ cout << "base test"<<"x = " << x<<endl;}
};
class Derived : public Base
{
public:
Derived(){ cout<<"derived"<<endl;}
virtual ~Derived(){ cout<<"dest derived"<<endl;}
virtual void test(int x){ cout << "derived test"<<"x = " << x<<endl;}
};
int main() {
Base *b = new Derived();
b->test(10);
delete b;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:
base
derived
derived testx …
Run Code Online (Sandbox Code Playgroud) void test(int& in);
void test(const int& in){
}
int main(){
int a = 5;
test(a);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
上面没有编译链接错误:undefined reference to `test(int&)'
。
我对此有3个问题:
1- 为什么我们会收到链接错误?是因为添加const
到定义中使其成为完全不同的功能吗?为什么在不使用引用时它会起作用,即这可以正常工作:
void test(int in);
void test(const int in){}
..
int a = 5;
test(a);
..
Run Code Online (Sandbox Code Playgroud)
2- 是否const
进入函数声明、定义或两者?如果使用引用,行为似乎有所不同。
3-const
参数上的关键字是否说“传递给我的参数应该是调用者中的常量”或“此参数在此函数范围内被视为常量,无论它在调用者中是否为常量”。我确定是后者,但想确认一下。