小编son*_*yao的帖子

为什么使用字符串字面量的class_copy初始化初始化不适用于string_view?

我有以下代码:

#include <string_view>

class Foo
{
public:
    Foo(std::string_view) {}
};
Run Code Online (Sandbox Code Playgroud)

当我这样做时,一切都可以正常编译(使用clang v8,C ++ 17):

Foo f("testing");
Run Code Online (Sandbox Code Playgroud)

但是,如果我使用副本初始化,它将失败:

Foo f = "testing";
Run Code Online (Sandbox Code Playgroud)

诊断:

prog.cc:15:9: error: no viable conversion from 'const char [8]' to 'Foo'
    Foo f = "testing";
        ^   ~~~~~~~~~
prog.cc:7:7: note: candidate constructor (the implicit copy constructor) not viable: no known conversion from 'const char [8]' to 'const Foo &' for 1st argument
class Foo
      ^
prog.cc:7:7: note: candidate constructor (the implicit move constructor) not viable: no known conversion from …
Run Code Online (Sandbox Code Playgroud)

c++ initialization copy-initialization

3
推荐指数
1
解决办法
73
查看次数

类之外的c ++,可变声明的顺序是否被保证是构造的顺序?

可以说我有代码:

main.cpp:

my_obj1 obj1("hello obj1");
my_obj2 obj2("hello obj2");

int main()
{
    :
    :
}
Run Code Online (Sandbox Code Playgroud)

我想知道是否obj1总是被保证创建之前obj2

如果我认为这两个对象在课堂上都可以,那将是正确的。

c++ initialization

3
推荐指数
1
解决办法
69
查看次数

如何在结构上使用std :: unique_ptr?

标题说明了大部分内容,我该怎么做?我已经在Google上搜索了一下,没有任何东西告诉我它无法完成,但是也没有任何说明如何做到这一点。

在此处获取以下代码段:

#include <cstdio>
#include <memory>

int main(void)
{
    struct a_struct
    {
        char first;
        int second;
        float third;
    };

    std::unique_ptr<a_struct> my_ptr(new a_struct);

    my_ptr.first = "A";
    my_ptr.second = 2;
    my_ptr.third = 3.00;

    printf("%c\n%i\n%f\n",my_ptr.first, my_ptr.second, my_ptr.third);

    return(0);
}
Run Code Online (Sandbox Code Playgroud)

正如能够回答这个问题的人已经知道的那样,这行不通,甚至无法编译。

我的问题是我该如何做类似的工作?

编译错误(使用g ++-7)如下所示

baduniqueptr6.cpp: In function ‘int main()’:
baduniqueptr6.cpp:15:12: error: ‘class std::unique_ptr<main()::a_struct>’ has no member named ‘first’
     my_ptr.first = "A";
            ^~~~~
baduniqueptr6.cpp:16:12: error: ‘class std::unique_ptr<main()::a_struct>’ has no member named ‘second’
     my_ptr.second = 2;
            ^~~~~~
baduniqueptr6.cpp:17:12: error: ‘class std::unique_ptr<main()::a_struct>’ has no member named ‘third’ …
Run Code Online (Sandbox Code Playgroud)

c++ struct unique-ptr c++11

3
推荐指数
1
解决办法
136
查看次数

我可以防止将参数隐式转换为赋值运算符

我正在实现一个类型(TParameter),该类型必须是布尔值(以指示该值是否有效)和任意类型的数据值。

想法是,如果方法采用某种类型的参数,则可以将其设置为false,以指示该值无效。

像这样:

someVariable = 123;   // use the value 123
someVariable = false; // mark variable as invalid/to-be-ignored
Run Code Online (Sandbox Code Playgroud)

我的代码的简化版本:

template <class T>

class TParameter
{
  public:
    TParameter()
    : m_value(),
      m_valid(false)
    {}

    // assignment operators
    TParameter& operator= (const T& value)
    {
      m_value = value;
      m_valid = true;
      return *this;
    }

    TParameter& operator= (bool valid)
    {
      m_valid = valid;
      return *this;
    }

  private:
    T m_value;
    bool m_valid;
};

  void test()
  {
    TParameter<int16_t> param;

    param = false;
    param …
Run Code Online (Sandbox Code Playgroud)

c++ templates implicit-conversion

3
推荐指数
1
解决办法
92
查看次数

当我们具有纯虚函数租用时,如何初始化一个对象?

class A{
    public: virtual void getName() = 0;
};

class B: public A{
    public:
        B(int a){
            cout<< "B Name : " << a <<endl;
        }
        void getName() override { cout << "Class B" << endl; }
};

class C: public B{
    public:
      C(int a){
            cout<< "C Name : " << a <<endl;
        }
        void getName() override { cout << "Class C" << endl; }
};

int main()
{
    B *b = new B(10);
    b->getName();

    C *c = new C(20);
    c->getName(); …
Run Code Online (Sandbox Code Playgroud)

c++ virtual initialization function

3
推荐指数
1
解决办法
66
查看次数

为什么可以将ifstream文件置于if条件中?

我正在尝试检查文件是否成功打开。我发现这种方法可以打开要读取的文件:

char path[]="data/configuration.txt";
std::ifstream configFile(path, std::ifstream::in);
if(configFile) {
  std::cout<<"Successfully opened file: "<<path<<std::endl;
} else {
  std::cout<<"Error, Could not open file: "<<path<<std::endl;
}
Run Code Online (Sandbox Code Playgroud)

问题是究竟要if检查什么?

因为我还发现了以下检查文件是否打开的方法:

char path[]="data/configuration.txt";
std::ifstream configFile;
configFile.open(path, std::ifstream::in);
if(configFile.is_open()) {
  std::cout<<"Successfully opened file: "<<path<<std::endl;
} else {
  std::cout<<"Error, Could not open file: "<<path<<std::endl;
}
Run Code Online (Sandbox Code Playgroud)

我还有其他问题。例如,两种打开文件的方法有什么区别?另外,这两个if条件有什么区别?

我认为这些是相似的方法,最终会得到相同的结果,因为我可以使用两种打开方法std::ifstream一样的is_open方法:

std::ifstream configFile(path, std::ifstream::in);
configFile.open(path, std::ifstream::in);
Run Code Online (Sandbox Code Playgroud)

c++ implicit-conversion

3
推荐指数
1
解决办法
56
查看次数

如果删除不带虚拟dtor的类,为什么可以删除,如果它继承了带虚拟dtor的类

考虑下面的代码

#include <cstdio>
#include <memory>

struct Base1
{
    Base1() = default;
    virtual ~Base1() = default;
    //~Base1() = default;
};

struct Base2 : public Base1
{
    Base2()
    {
        printf("%s:%d:%s\n", __FILE__, __LINE__, __func__);
    }
    ~Base2() // non-virtual destructor
    {
        printf("%s:%d:%s\n", __FILE__, __LINE__, __func__);
    }
};

struct Derive : public Base2
{
    Derive()
    {
        printf("%s:%d:%s\n", __FILE__, __LINE__, __func__);
    }
    ~Derive()
    {
        printf("%s:%d:%s\n", __FILE__, __LINE__, __func__);
    }
};

int main()
{
    std::unique_ptr<Base2> d = std::make_unique<Derive>();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

该类Base2没有虚拟析构函数,但它继承Base1了具有虚拟析构函数的类。

代码 …

c++ virtual destructor

3
推荐指数
2
解决办法
75
查看次数

为什么 cout 不打印这个字符串?

所以,我是 C++ 的新手,我无法弄清楚为什么会发生这种情况。我有一个包含所有字母的字符串,我将 10 个字符从它复制到一个新字符串中s2,使用 for 循环逐个字符复制,如下所示,当我执行它时,该cout函数正在打印一个空行。

#include <iostream>

using namespace std;

int main(){
    string s = "abcdefghijklmnopqrstuvwxyz";
    string s2;
    for(int i=0; i<10; i++){
        s2[i] = s[i];
    }
    cout << s2 << endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

但是当我s2逐个字符打印这个字符串时,我得到了正确的输出

#include <iostream>

using namespace std;

int main(){
    string s = "abcdefghijklmnopqrstuvwxyz";
    string s2;
    for(int i=0; i<10; i++){
        s2[i] = s[i];
    }
    
    for(int i=0; i<10; i++){
        cout << s2[i];
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

任何帮助,将不胜感激!

c++ stdstring c++17

3
推荐指数
1
解决办法
161
查看次数

Const方法返回向量元素的非const引用

我很难弄清楚如何从const类方法返回non-const对元素的引用std::vector.我想要的一个简单例子是,

template<class T>
class MyClass
{
 public:
 MyClass : myVec(3)
 {
 }

 T& x() const
 {
    return *(myVec.data())[0]
 }

 std::vector<T> myVec;
}
Run Code Online (Sandbox Code Playgroud)

我想要的行为是,我希望能够做到以下几点,

MyClass obj<double>;
obj.x() = 3.3;
assert(obj.x()==3.3)
Run Code Online (Sandbox Code Playgroud)

Eigen给出了相同类型的行为,但我无法弄清楚如何让它工作.

c++ const reference vector const-method

2
推荐指数
1
解决办法
882
查看次数

C++ 访问静态 constexpr 数组

我试图从另一个类中声明的数组中获取一些值。该数组具有固定长度和常量元素(我将 100% 从不修改其值,所以这就是我将其设为常量的原因)。

但是,当我尝试访问main函数中的第一个元素时,出现编译错误:

basavyr@Roberts-MacBook-Pro src % g++ -std=c++11 main.cc
Undefined symbols for architecture x86_64:
  "Vectors::vec1", referenced from:
      _main in main-c29f22.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1
Run Code Online (Sandbox Code Playgroud)

如您所见,我正在使用 clang(最新版本)在 macOS Catalina 上进行编译。

[问]:可能是什么问题?先感谢您。

这是代码:

#include <iostream>

class Dimension
{
public:
    static constexpr int dim1 = 2;
    static constexpr int dim2 = 1;
};

class Vectors
{
public:
    static constexpr double vec1[2] = {4.20, 6.9};
};

int main()
{ …
Run Code Online (Sandbox Code Playgroud)

c++ class constexpr c++11

2
推荐指数
1
解决办法
491
查看次数