我有一个B有两个方法的类,其中一个返回指向成员变量的指针,另一个返回对该变量的const引用.
我试着打电话给那些方法.在调用期间,我将返回值存储到相应的返回类型.
我期待适当的返回类型最终会调用适当的方法,但我得到一个编译错误说:
error: invalid conversion from ‘int*’ to ‘int’ [-fpermissive] const int& refval2 = b.Get(); `
这是我的代码:
#include <iostream>
class B{
public:
int* Get(){
return &x_;
}
const int & Get() const{
return x_;
}
private:
int x_ = 0;
};
int main(){
B b;
const int& refval2 = b.Get();
int* pval2 = b.Get();
}
Run Code Online (Sandbox Code Playgroud) 我有一个简单的问题:
C或C++中的函数类型是什么
因为我们可以指向C或C++中的函数,这意味着函数应该具有特定类型,否则在指向函数创建的过程中进行类型检查没有任何意义.
有人可以解释我,我是否正确的道路?
如果我走在正确的道路上,我怎样才能找到功能的类型?
为什么以下尝试重载构造函数Foo::Foo失败?此外,我很欣赏替代方案/解决方法
#include <vector>
#include <type_traits>
namespace xyz
{
struct MemoryManager{};
template<typename T , typename Alloc=MemoryManager>
class vector
{
};
}
template<typename T, template<typename,typename> class V , typename A>
struct Foo
{
template<typename U = T ,
typename Dummy = typename std::enable_if<
std::is_same<std::vector<U>, V<U,A> >::value >::type >
Foo() // when instantiated with the std vector
{
}
template<typename U = T ,
typename Dummy = typename std::enable_if<
std::is_same<xyz::vector<U>, V<U,A> >::value >::type >
Foo() // when instantiated with …Run Code Online (Sandbox Code Playgroud) 这是我的头文件,auto vs static.h
#include "stdafx.h"
#include <iostream>
void IncrementAndPrint()
{
using namespace std;
int nValue = 1; // automatic duration by default
++nValue;
cout << nValue << endl;
} // nValue is destroyed here
void print_auto()
{
IncrementAndPrint();
IncrementAndPrint();
IncrementAndPrint();
}
void IncrementAndPrint()
{
using namespace std;
static int s_nValue = 1; // fixed duration
++s_nValue;
cout << s_nValue << endl;
} // s_nValue is not destroyed here, but becomes inaccessible
void print_static()
{
IncrementAndPrint();
IncrementAndPrint();
IncrementAndPrint();
}
Run Code Online (Sandbox Code Playgroud)
这是我的主文件,namearray.cpp
#include …Run Code Online (Sandbox Code Playgroud) 我试图理解这在我的编译器调试输出中意味着什么:
std::_Bind<std::_Mem_fn<void (ClassName::*)()>(ClassName*)>
Run Code Online (Sandbox Code Playgroud)
如果重要,它将被用作模板变量的值,该模板变量被用作类成员变量的类型.
我知道的这一点我不确定"ClassName::*"(当您搜索的重要位是非字母数字位时,谷歌真的很 糟糕)
可能重复:
返回类型是函数签名的一部分吗?
关注一个相关但相切的问题(如何消除仅由返回类型不同的函数模板?),我想问一个与函数的返回类型不被认为是签名的一部分有关的问题一个功能.
请考虑以下代码:
#include <iostream>
int foo()
{
return 0;
}
int main()
{
long n = static_cast<long(&)()>(foo)(); // Error: incorrect return type
int p = static_cast<int(&)()>(foo)(); // Compiles just fine
}
Run Code Online (Sandbox Code Playgroud)
的代码行上面提到编译错误的结果,因为该返回功能类型的类型,其foo被强制转换不匹配返回功能的类型foo.
但我认为函数的返回类型在函数的签名中不起作用!
根据某种思路,由于函数签名long(&)()与签名匹配foo,foo对此类函数的强制转换应该成功.
但是,演员阵容没有成功.推理在哪里出错了?如果演员阵容因功能签名而无法失败,那么为什么演员阵容失败?
我很惊讶地发现这是C中的有效函数声明:
f() {
return 10;
}
Run Code Online (Sandbox Code Playgroud)
未指定函数的返回类型.它不仅编译,而且实际上返回10.
void main() {
int i = f();
printf("i = %d\n", i);
}
Run Code Online (Sandbox Code Playgroud)
此代码导致以下输出:
i = 10
Run Code Online (Sandbox Code Playgroud)
这是用gcc 4.8.4编译的.为什么没有明确指定的返回类型的函数声明编译而没有错误/警告?
编辑:作为解释这里的返回类型不是函数签名的一部分,你不能超载根据不同的返回类型的功能,但问题是,需要返回类型函数声明不回答.
我无法弄清楚,为什么C ++不允许根据返回类型进行重载,因为在以下情况下,三个member(getter)函数具有不同的函数签名,即使在存储指向成员函数的指针时,我们也需要不同的mem-函数指针类型如下:
for instance T = std::string
using constRefPtr = const std::string&(MyStruct::*)() const;
using constValuePtr = const std::string(MyStruct::*)() const;
using valuePtr = std::string(MyStruct::*)() const;
Run Code Online (Sandbox Code Playgroud)
我已经阅读过这篇类似的文章,建议使用const和非成本成员函数。
问题:如何在不删除const每个成员函数的本质的情况下使以下(getter)重载工作(如果可以通过标准C ++实现)?
我正在使用C ++ 17。
#include <iostream>
#include <string>
template<typename T> class MyStruct
{
T m_val;
public:
explicit MyStruct(const T& value)
: m_val(value)
{}
const T& getVal() const { return m_val; } // get val as const ref(no copy of member)
const T getVal() const { return m_val; } // get …Run Code Online (Sandbox Code Playgroud) c++ ×7
c ×2
c++11 ×2
function ×2
templates ×2
c++17 ×1
overloading ×1
pointers ×1
sfinae ×1
typechecking ×1