我无法弄清楚,为什么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) 从cpp参考:https : //en.cppreference.com/w/cpp/language/if,看来我不能做到这一点:
if (cond)
{}
else if (init; cond) // <<--- init not allowed with "else if"
{}
Run Code Online (Sandbox Code Playgroud)
我以一种相当愚蠢的方式解决了它:
if (cond)
{}
else if ([]() -> bool
{
init;
if (cond)
{
// Do something in the same scope as 'init'
return true;
}
return false;
}())
{}
Run Code Online (Sandbox Code Playgroud)
我是否在这里缺少有关使用C ++ 17“正确”执行此操作的明显方法?
我正在开发一个程序来帮助我进行世界建设,该计划会根据用户选择的国家(德语,拉丁语,东部)随机生成定居点(村庄,村庄,城镇,城市)。我已经集成了一种定居者生成系统,以在定居点中创建定居者,每个定居者使用构造函数并将结果作为对象保存在向量中,每个定居者均具有名称,年龄,性别和财富。不幸的是,该程序创建了一个完整的克隆种群,用相同名称,年龄等的移居者填充了载体。
我曾尝试在for循环中初始化Settler类的构造函数,但除了在您每次请求其中一个有关定居者的信息时,会导致获得一组不同的定居者之外,它没有做任何更改。
class Settler {
public:
int settlerAge;
string settlerName;
string settlerGender;
string settlerWealth;
Settler(int type, int nation, int quantity) {
int result{};
string givenName{};
string surName{};
// Latin Male First Name
string latinMaleName[15] = {"Faustus", "Mamercus", "Mettius", "Appius", "Hostus", "Quintus", "Cossus", "Secundus", "Servius", "Gallio", "Tettienus", "Petronius", "Paesentius", "Pescunnius", "Clodius"};
// Latin Surname
string latinSurname[30] = {"Natalinus", "Lucilianus", "Crispian", "Laetinianus", "Falco", "Otho", "Plautius", "Pascentius", "Lepidus", "Moderatus", "Caeparius", "Caetronius", "Hostilius", "Aedinius", "Papius", "Gennadia", "Triaria", "Planta","Amantia", "Mico", "Opilio", "Augusta", "Laevina", "Longina", …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用该std::size()函数确定数组的大小,但我的编译器带来了此错误:
/root/Desktop/practise.cpp:9:34: 错误:“size”不是“std”的成员;你的意思是“size_t”?
这是第一个代码:
#include <iostream>
#include <array>
using namespace std;
int main()
{
int values [] {2,3,4,5,6,7,8,9,10};
cout <<"The array size is:"<< std::size(values);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在改变语句std::size(values)来std::size_t(values),似乎并不准确弹出一个庞大的数字。输出的值为:
140725039324624
包括像“IV”这样的字符串的其余部分尚未完成,但现在的问题是对于“I”,程序返回 8 而不是 1;
#include <bits/stdc++.h> //header file
using namespace std;
int romantoint(char str[],int &n)
{
int total=0;
unordered_map <char,int> m;
m['I'] = 1;
m['II'] = 2;
m['III'] = 3;
m['IV'] = 4;
m['V'] = 5;
m['VI'] = 6;
m['VII'] = 7;
m['VIII'] = 8;
m['IX'] = 9;
m['X'] = 10;
for(int i=0;i<n;i++)
{
total = total+m[str[i]];
}
return total;
}
int main()
{
char str[] = "I";
int n = strlen(str);
cout<<romantoint(str,n);
}
Run Code Online (Sandbox Code Playgroud) 我在 C++17 中制作了基于嵌套范围的 for 循环程序。
#include <iostream>
#include <vector>
int main()
{
std::vector<int> v = {0, 1, 2, 3, 4, 5};
for (int i : v)
{
for (int a : i)
std::cout << a << ' ';
}
}
Run Code Online (Sandbox Code Playgroud)
GCC 产生错误:
main.cpp: In function 'int main()':
main.cpp:10:22: error: 'begin' was not declared in this scope
for (int a : i)
Run Code Online (Sandbox Code Playgroud)
所以,
C++ 中的向量是表示可以更改大小的数组的序列容器。它们对其元素使用连续的存储位置,这意味着也可以使用指向其元素的常规指针上的偏移量来访问它们的元素,并且与数组中一样有效。
谁能用通俗易懂的语言向我解释一下 C++ 中的 是什么意思vector<int> dummy1(rows,-1);?具体来说是什么dummy1(rows,-1)意思呢?
c++ ×7
c++17 ×7
c++11 ×1
constructor ×1
for-loop ×1
if-statement ×1
lambda ×1
overloading ×1
vector ×1